Class REXML::IOSource
In: rexml/source.rb
Parent: Source

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

Methods

consume   current_line   empty?   match   new   position   read   scan  

Public Class methods

block_size has been deprecated

[Source]

# File rexml/source.rb, line 120
                def initialize(arg, block_size=500)
                        @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.
      str = @source.read( 2 )
      if (str[0] == 254 && str[1] == 255) || (str[0] == 255 && str[1] == 254)
        @encoding = check_encoding( str )
        @line_break = encode( '>' )
      else
        @line_break = '>'
      end
      super str+@source.readline( @line_break )
                end

Public Instance methods

[Source]

# File rexml/source.rb, line 175
                def consume( pattern )
                        match( pattern, true )
                end

@return the current line in the source

[Source]

# File rexml/source.rb, line 206
                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

[Source]

# File rexml/source.rb, line 197
                def empty?
                        super and ( @source.nil? || @source.eof? )
                end

[Source]

# File rexml/source.rb, line 179
                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

[Source]

# File rexml/source.rb, line 201
    def position
      @er_source.pos
    end

[Source]

# File rexml/source.rb, line 165
                def read
                        begin
        str = @source.readline(@line_break)
                                str = decode(str) if @to_utf and str 
                                @buffer << str
                        rescue Exception, NameError
                                @source = nil
                        end
                end

[Source]

# File rexml/source.rb, line 140
                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
                                                @source = nil
                                        end
                                end
                                rv = super
                        end
                        rv.taint
                        rv
                end

[Validate]