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 134
134:                 def initialize(arg, block_size=500, encoding=nil)
135:                         @er_source = @source = arg
136:                         @to_utf = false
137:       # Determining the encoding is a deceptively difficult issue to resolve.
138:       # First, we check the first two bytes for UTF-16.  Then we
139:       # assume that the encoding is at least ASCII enough for the '>', and
140:       # we read until we get one of those.  This gives us the XML declaration,
141:       # if there is one.  If there isn't one, the file MUST be UTF-8, as per
142:       # the XML spec.  If there is one, we can determine the encoding from
143:       # it.
144:       @buffer = ""
145:       str = @source.read( 2 )
146:       if encoding
147:         self.encoding = encoding
148:       elsif /\A(?:\xfe\xff|\xff\xfe)/n =~ str
149:         self.encoding = check_encoding( str )
150:       else
151:         @line_break = '>'
152:       end
153:       super str+@source.readline( @line_break )
154:     end

Public Instance methods

[Source]

     # File lib/rexml/source.rb, line 193
193:                 def consume( pattern )
194:                         match( pattern, true )
195:                 end

@return the current line in the source

[Source]

     # File lib/rexml/source.rb, line 224
224:                 def current_line
225:       begin
226:         pos = @er_source.pos                            # The byte position in the source
227:         lineno = @er_source.lineno      # The XML < position in the source
228:         @er_source.rewind
229:         line = 0                                                                                # The \r\n position in the source
230:         begin
231:           while @er_source.pos < pos
232:             @er_source.readline
233:             line += 1
234:           end
235:         rescue
236:         end
237:       rescue IOError
238:         pos = -1
239:         line = -1
240:       end
241:                         [pos, lineno, line]
242:                 end

[Source]

     # File lib/rexml/source.rb, line 215
215:                 def empty?
216:                         super and ( @source.nil? || @source.eof? )
217:                 end

[Source]

     # File lib/rexml/source.rb, line 197
197:                 def match( pattern, cons=false )
198:                         rv = pattern.match(@buffer)
199:                         @buffer = $' if cons and rv
200:                         while !rv and @source
201:                                 begin
202:           str = @source.readline(@line_break)
203:                                         str = decode(str) if @to_utf and str
204:                                         @buffer << str
205:                                         rv = pattern.match(@buffer)
206:                                         @buffer = $' if cons and rv
207:                                 rescue
208:                                         @source = nil
209:                                 end
210:                         end
211:                         rv.taint
212:                         rv
213:                 end

[Source]

     # File lib/rexml/source.rb, line 219
219:     def position
220:       @er_source.stat.pipe? ? 0 : @er_source.pos
221:     end

[Source]

     # File lib/rexml/source.rb, line 183
183:                 def read
184:                         begin
185:         str = @source.readline(@line_break)
186:                                 str = decode(str) if @to_utf and str 
187:                                 @buffer << str
188:                         rescue Exception, NameError
189:                                 @source = nil
190:                         end
191:                 end

[Source]

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

[Validate]