class REXML::Source

A Source can be searched for patterns, and wraps buffers and other objects and provides consumption of text

Attributes

encoding[R]
line[R]

The line number of the last consumed text

Public Class Methods

new(arg, encoding=nil) click to toggle source

Constructor @param arg must be a String, and should be a valid XML document @param encoding if non-null, sets the encoding of the source to this value, overriding all encoding detection

# File rexml-3.3.6/lib/rexml/source.rb, line 71
def initialize(arg, encoding=nil)
  @orig = arg
  @scanner = StringScanner.new(@orig)
  if encoding
    self.encoding = encoding
  else
    detect_encoding
  end
  @line = 0
end

Public Instance Methods

buffer() click to toggle source

The current buffer (what we’re going to read next)

# File rexml-3.3.6/lib/rexml/source.rb, line 83
def buffer
  @scanner.rest
end
buffer_encoding=(encoding) click to toggle source
# File rexml-3.3.6/lib/rexml/source.rb, line 93
def buffer_encoding=(encoding)
  @scanner.string.force_encoding(encoding)
end
current_line() click to toggle source

@return the current line in the source

# File rexml-3.3.6/lib/rexml/source.rb, line 142
def current_line
  lines = @orig.split
  res = lines.grep @scanner.rest[0..30]
  res = res[-1] if res.kind_of? Array
  lines.index( res ) if res
end
drop_parsed_content() click to toggle source
# File rexml-3.3.6/lib/rexml/source.rb, line 87
def drop_parsed_content
  if @scanner.pos > Private::SCANNER_RESET_SIZE
    @scanner.string = @scanner.rest
  end
end
empty?() click to toggle source

@return true if the Source is exhausted

# File rexml-3.3.6/lib/rexml/source.rb, line 137
def empty?
  @scanner.eos?
end
encoding=(enc) click to toggle source

Inherited from Encoding Overridden to support optimized en/decoding

Calls superclass method REXML::Encoding#encoding=
# File rexml-3.3.6/lib/rexml/source.rb, line 99
def encoding=(enc)
  return unless super
  encoding_updated
end
ensure_buffer() click to toggle source
# File rexml-3.3.6/lib/rexml/source.rb, line 117
def ensure_buffer
end
match(pattern, cons=false) click to toggle source
# File rexml-3.3.6/lib/rexml/source.rb, line 120
def match(pattern, cons=false)
  if cons
    @scanner.scan(pattern).nil? ? nil : @scanner
  else
    @scanner.check(pattern).nil? ? nil : @scanner
  end
end
position() click to toggle source
# File rexml-3.3.6/lib/rexml/source.rb, line 128
def position
  @scanner.pos
end
position=(pos) click to toggle source
# File rexml-3.3.6/lib/rexml/source.rb, line 132
def position=(pos)
  @scanner.pos = pos
end
read(term = nil) click to toggle source
# File rexml-3.3.6/lib/rexml/source.rb, line 104
def read(term = nil)
end
read_until(term) click to toggle source
# File rexml-3.3.6/lib/rexml/source.rb, line 107
def read_until(term)
  pattern = Private::PRE_DEFINED_TERM_PATTERNS[term] || /#{Regexp.escape(term)}/
  data = @scanner.scan_until(pattern)
  unless data
    data = @scanner.rest
    @scanner.pos = @scanner.string.bytesize
  end
  data
end

Private Instance Methods

detect_encoding() click to toggle source
# File rexml-3.3.6/lib/rexml/source.rb, line 151
def detect_encoding
  scanner_encoding = @scanner.rest.encoding
  detected_encoding = "UTF-8"
  begin
    @scanner.string.force_encoding("ASCII-8BIT")
    if @scanner.scan(/\xfe\xff/n)
      detected_encoding = "UTF-16BE"
    elsif @scanner.scan(/\xff\xfe/n)
      detected_encoding = "UTF-16LE"
    elsif @scanner.scan(/\xef\xbb\xbf/n)
      detected_encoding = "UTF-8"
    end
  ensure
    @scanner.string.force_encoding(scanner_encoding)
  end
  self.encoding = detected_encoding
end
encoding_updated() click to toggle source
# File rexml-3.3.6/lib/rexml/source.rb, line 169
def encoding_updated
  if @encoding != 'UTF-8'
    @scanner.string = decode(@scanner.rest)
    @to_utf = true
  else
    @to_utf = false
    @scanner.string.force_encoding(::Encoding::UTF_8)
  end
end