In Files

  • rexml/source.rb

Class/Module Index [+]

Quicksearch

REXML::IOSource

A Source that wraps an IO. See the Source class for method documentation

Public Class Methods

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

block_size has been deprecated

 
               # File rexml/source.rb, line 138
def initialize(arg, block_size=500, encoding=nil)
  @er_source = @source = arg
  @to_utf = false

  # Determining the encoding is a deceptively difficult issue to resolve.
  # First, we check the first two bytes for UTF-16.  Then we
  # assume that the encoding is at least ASCII enough for the '>', and
  # we read until we get one of those.  This gives us the XML declaration,
  # if there is one.  If there isn't one, the file MUST be UTF-8, as per
  # the XML spec.  If there is one, we can determine the encoding from
  # it.
  @buffer = ""
  str = @source.read( 2 ) || ''
  if encoding
    self.encoding = encoding
  elsif str[0,2] == "\xfe\xff"
    @line_break = "\000>"
  elsif str[0,2] == "\xff\xfe"
    @line_break = ">\000"
  elsif str[0,2] == "\xef\xbb"
    str += @source.read(1)
    str = '' if (str[2,1] == "\xBF")
    @line_break = ">"
  else
    @line_break = ">"
  end
  super( @source.eof? ? str : str+@source.readline( @line_break ) )
end
            

Public Instance Methods

consume( pattern ) click to toggle source
 
               # File rexml/source.rb, line 207
def consume( pattern )
  match( pattern, true )
end
            
current_line() click to toggle source

@return the current line in the source

 
               # File rexml/source.rb, line 238
def current_line
  begin
    pos = @er_source.pos        # The byte position in the source
    lineno = @er_source.lineno  # The XML < position in the source
    @er_source.rewind
    line = 0                    # The \r\n position in the source
    begin
      while @er_source.pos < pos
        @er_source.readline
        line += 1
      end
    rescue
    end
  rescue IOError
    pos = -1
    line = -1
  end
  [pos, lineno, line]
end
            
empty?() click to toggle source
 
               # File rexml/source.rb, line 229
def empty?
  super and ( @source.nil? || @source.eof? )
end
            
match( pattern, cons=false ) click to toggle source
 
               # File rexml/source.rb, line 211
def match( pattern, cons=false )
  rv = pattern.match(@buffer)
  @buffer = $' if cons and rv
  while !rv and @source
    begin
      str = @source.readline(@line_break)
      str = decode(str) if @to_utf and str
      @buffer << str
      rv = pattern.match(@buffer)
      @buffer = $' if cons and rv
    rescue
      @source = nil
    end
  end
  rv.taint
  rv
end
            
position() click to toggle source
 
               # File rexml/source.rb, line 233
def position
  @er_source.pos rescue 0
end
            
read() click to toggle source
 
               # File rexml/source.rb, line 194
def read
  begin
    str = @source.readline(@line_break)
    str = decode(str) if @to_utf and str 
    @buffer << str
    if not @to_utf and @buffer.respond_to? :force_encoding
      @buffer.force_encoding Encoding::UTF_8
    end
  rescue Exception, NameError
    @source = nil
  end
end
            
scan(pattern, cons=false) click to toggle source
 
               # File rexml/source.rb, line 167
def scan(pattern, cons=false)
  rv = super
  # You'll notice that this next section is very similar to the same
  # section in match(), but just a liiittle different.  This is
  # because it is a touch faster to do it this way with scan()
  # than the way match() does it; enough faster to warrent duplicating
  # some code
  if rv.size == 0
    until @buffer =~ pattern or @source.nil?
      begin
        # READLINE OPT
        #str = @source.read(@block_size)
        str = @source.readline(@line_break)
        str = decode(str) if @to_utf and str
        @buffer << str
      rescue Iconv::IllegalSequence
        raise
      rescue
        @source = nil
      end
    end
    rv = super
  end
  rv.taint
  rv
end