Class REXML::Source
In: lib/rexml/source.rb
Parent: Object

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

Methods

consume   current_line   empty?   encoding=   match   match_to   match_to_consume   new   position   read   scan  

Included Modules

Encoding

Attributes

buffer  [R]  The current buffer (what we‘re going to read next)
encoding  [R] 
line  [R]  The line number of the last consumed text

Public Class methods

Constructor @param arg must be a String, and should be a valid XML document

[Source]

    # File lib/rexml/source.rb, line 38
38:                 def initialize(arg)
39:                         @orig = @buffer = arg
40:                         self.encoding = check_encoding( @buffer )
41:                         @line = 0
42:                 end

Public Instance methods

[Source]

    # File lib/rexml/source.rb, line 83
83:                 def consume( pattern )
84:                         @buffer = $' if pattern.match( @buffer )
85:                 end

@return the current line in the source

[Source]

     # File lib/rexml/source.rb, line 113
113:                 def current_line
114:                         lines = @orig.split
115:                         res = lines.grep @buffer[0..30]
116:                         res = res[-1] if res.kind_of? Array
117:                         lines.index( res ) if res
118:                 end

@return true if the Source is exhausted

[Source]

     # File lib/rexml/source.rb, line 104
104:                 def empty?
105:                         @buffer == ""
106:                 end

Inherited from Encoding Overridden to support optimized en/decoding

[Source]

    # File lib/rexml/source.rb, line 46
46:                 def encoding=(enc)
47:                         super
48:                         @line_break = encode( '>' )
49:                         if enc != UTF_8
50:                                 @buffer = decode(@buffer)
51:                                 @to_utf = true
52:                         else
53:                                 @to_utf = false
54:                         end
55:                 end

[Source]

     # File lib/rexml/source.rb, line 97
 97:                 def match(pattern, cons=false)
 98:                         md = pattern.match(@buffer)
 99:                         @buffer = $' if cons and md
100:                         return md
101:                 end

[Source]

    # File lib/rexml/source.rb, line 87
87:                 def match_to( char, pattern )
88:                         return pattern.match(@buffer)
89:                 end

[Source]

    # File lib/rexml/source.rb, line 91
91:                 def match_to_consume( char, pattern )
92:                         md = pattern.match(@buffer)
93:                         @buffer = $'
94:                         return md
95:                 end

[Source]

     # File lib/rexml/source.rb, line 108
108:     def position
109:       @orig.index( @buffer )
110:     end

[Source]

    # File lib/rexml/source.rb, line 80
80:                 def read
81:                 end

Scans the source for a given pattern. Note, that this is not your usual scan() method. For one thing, the pattern argument has some requirements; for another, the source can be consumed. You can easily confuse this method. Originally, the patterns were easier to construct and this method more robust, because this method generated search regexes on the fly; however, this was computationally expensive and slowed down the entire REXML package considerably, since this is by far the most commonly called method. @param pattern must be a Regexp, and must be in the form of /^\s*(#{your pattern, with no groups})(.*)/. The first group will be returned; the second group is used if the consume flag is set. @param consume if true, the pattern returned will be consumed, leaving everything after it in the Source. @return the pattern, if found, or nil if the Source is empty or the pattern is not found.

[Source]

    # File lib/rexml/source.rb, line 73
73:                 def scan(pattern, cons=false)
74:                         return nil if @buffer.nil?
75:                         rv = @buffer.scan(pattern)
76:                         @buffer = $' if cons and rv.size>0
77:                         rv
78:                 end

[Validate]