123456789_123456789_123456789_123456789_123456789_

Class: REXML::Source

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Encoding
Inherits: Object
Defined in: lib/rexml/source.rb

Overview

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

Class Method Summary

Instance Attribute Summary

Encoding - Included

#encoding

ID —> Encoding name.

#encoding=

Instance Method Summary

Constructor Details

.new(arg, encoding = nil) ⇒ Source

Constructor value, overriding all encoding detection

Parameters:

  • arg

    must be a String, and should be a valid XML document

  • encoding (defaults to: nil)

    if non-null, sets the encoding of the source to this

[ GitHub ]

  
# File '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
  @term_encord = {}
end

Instance Attribute Details

#buffer_encoding=(encoding) (writeonly)

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 94

def buffer_encoding=(encoding)
  @scanner.string.force_encoding(encoding)
end

#empty?Boolean (readonly)

Returns:

  • (Boolean)

    true if the Source is exhausted

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 138

def empty?
  @scanner.eos?
end

#encoding (rw)

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 55

attr_reader :encoding

#encoding=(enc) (rw)

Inherited from Encoding Overridden to support optimized en/decoding

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 100

def encoding=(enc)
  return unless super
  encoding_updated
end

#line (readonly)

The line number of the last consumed text

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 54

attr_reader :line

#position (rw)

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 129

def position
  @scanner.pos
end

#position=(pos) (rw)

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 133

def position=(pos)
  @scanner.pos = pos
end

Instance Method Details

#buffer

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

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 84

def buffer
  @scanner.rest
end

#current_lineObject

Returns:

  • the current line in the source

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 143

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

#detect_encoding (private)

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 152

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

#drop_parsed_content

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 88

def drop_parsed_content
  if @scanner.pos > Private::SCANNER_RESET_SIZE
    @scanner.string = @scanner.rest
  end
end

#encoding_updated (private)

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 170

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

#ensure_buffer

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 118

def ensure_buffer
end

#match(pattern, cons = false)

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 121

def match(pattern, cons=false)
  if cons
    @scanner.scan(pattern).nil? ? nil : @scanner
  else
    @scanner.check(pattern).nil? ? nil : @scanner
  end
end

#read(term = nil)

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 105

def read(term = nil)
end

#read_until(term)

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 108

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