Class REXML::IOSource
In: lib/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 lib/rexml/source.rb, line 127
127:                 def initialize(arg, block_size=500)
128:                         @er_source = @source = arg
129:                         @to_utf = false
130:       # Determining the encoding is a deceptively difficult issue to resolve.
131:       # First, we check the first two bytes for UTF-16.  Then we
132:       # assume that the encoding is at least ASCII enough for the '>', and
133:       # we read until we get one of those.  This gives us the XML declaration,
134:       # if there is one.  If there isn't one, the file MUST be UTF-8, as per
135:       # the XML spec.  If there is one, we can determine the encoding from
136:       # it.
137:       str = @source.read( 2 )
138:       if (str[0] == 254 && str[1] == 255) || (str[0] == 255 && str[1] == 254)
139:         @encoding = check_encoding( str )
140:         @line_break = encode( '>' )
141:       else
142:         @line_break = '>'
143:       end
144:       super str+@source.readline( @line_break )
145:                 end

Public Instance methods

[Source]

     # File lib/rexml/source.rb, line 182
182:                 def consume( pattern )
183:                         match( pattern, true )
184:                 end

@return the current line in the source

[Source]

     # File lib/rexml/source.rb, line 213
213:                 def current_line
214:       begin
215:         pos = @er_source.pos                            # The byte position in the source
216:         lineno = @er_source.lineno      # The XML < position in the source
217:         @er_source.rewind
218:         line = 0                                                                                # The \r\n position in the source
219:         begin
220:           while @er_source.pos < pos
221:             @er_source.readline
222:             line += 1
223:           end
224:         rescue
225:         end
226:       rescue IOError
227:         pos = -1
228:         line = -1
229:       end
230:                         [pos, lineno, line]
231:                 end

[Source]

     # File lib/rexml/source.rb, line 204
204:                 def empty?
205:                         super and ( @source.nil? || @source.eof? )
206:                 end

[Source]

     # File lib/rexml/source.rb, line 186
186:                 def match( pattern, cons=false )
187:                         rv = pattern.match(@buffer)
188:                         @buffer = $' if cons and rv
189:                         while !rv and @source
190:                                 begin
191:           str = @source.readline(@line_break)
192:                                         str = decode(str) if @to_utf and str
193:                                         @buffer << str
194:                                         rv = pattern.match(@buffer)
195:                                         @buffer = $' if cons and rv
196:                                 rescue
197:                                         @source = nil
198:                                 end
199:                         end
200:                         rv.taint
201:                         rv
202:                 end

[Source]

     # File lib/rexml/source.rb, line 208
208:     def position
209:       @er_source.stat.pipe? ? 0 : @er_source.pos
210:     end

[Source]

     # File lib/rexml/source.rb, line 172
172:                 def read
173:                         begin
174:         str = @source.readline(@line_break)
175:                                 str = decode(str) if @to_utf and str 
176:                                 @buffer << str
177:                         rescue Exception, NameError
178:                                 @source = nil
179:                         end
180:                 end

[Source]

     # File lib/rexml/source.rb, line 147
147:                 def scan(pattern, cons=false)
148:                         rv = super
149:                         # You'll notice that this next section is very similar to the same
150:                         # section in match(), but just a liiittle different.  This is
151:                         # because it is a touch faster to do it this way with scan()
152:                         # than the way match() does it; enough faster to warrent duplicating
153:                         # some code
154:                         if rv.size == 0
155:                                 until @buffer =~ pattern or @source.nil?
156:                                         begin
157:                                                 # READLINE OPT
158:                                                 #str = @source.read(@block_size)
159:                                                 str = @source.readline(@line_break)
160:                                                 str = decode(str) if @to_utf and str
161:                                                 @buffer << str
162:                                         rescue
163:                                                 @source = nil
164:                                         end
165:                                 end
166:                                 rv = super
167:                         end
168:                         rv.taint
169:                         rv
170:                 end

[Validate]