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: 
138:       # Determining the encoding is a deceptively difficult issue to resolve.
139:       # First, we check the first two bytes for UTF-16.  Then we
140:       # assume that the encoding is at least ASCII enough for the '>', and
141:       # we read until we get one of those.  This gives us the XML declaration,
142:       # if there is one.  If there isn't one, the file MUST be UTF-8, as per
143:       # the XML spec.  If there is one, we can determine the encoding from
144:       # it.
145:       @buffer = ""
146:       str = @source.read( 2 )
147:       if encoding
148:         self.encoding = encoding
149:       elsif 0xfe == str[0] && 0xff == str[1]
150:         @line_break = "\000>"
151:       elsif 0xff == str[0] && 0xfe == str[1]
152:         @line_break = ">\000"
153:       elsif 0xef == str[0] && 0xbb == str[1]
154:         str += @source.read(1)
155:         str = '' if (0xbf == str[2])
156:         @line_break = ">"
157:       else
158:         @line_break = ">"
159:       end
160:       super str+@source.readline( @line_break )
161:     end

Public Instance methods

[Source]

     # File lib/rexml/source.rb, line 200
200:     def consume( pattern )
201:       match( pattern, true )
202:     end

@return the current line in the source

[Source]

     # File lib/rexml/source.rb, line 231
231:     def current_line
232:       begin
233:         pos = @er_source.pos        # The byte position in the source
234:         lineno = @er_source.lineno  # The XML < position in the source
235:         @er_source.rewind
236:         line = 0                    # The \r\n position in the source
237:         begin
238:           while @er_source.pos < pos
239:             @er_source.readline
240:             line += 1
241:           end
242:         rescue
243:         end
244:       rescue IOError
245:         pos = -1
246:         line = -1
247:       end
248:       [pos, lineno, line]
249:     end

[Source]

     # File lib/rexml/source.rb, line 222
222:     def empty?
223:       super and ( @source.nil? || @source.eof? )
224:     end

[Source]

     # File lib/rexml/source.rb, line 204
204:     def match( pattern, cons=false )
205:       rv = pattern.match(@buffer)
206:       @buffer = $' if cons and rv
207:       while !rv and @source
208:         begin
209:           str = @source.readline(@line_break)
210:           str = decode(str) if @to_utf and str
211:           @buffer << str
212:           rv = pattern.match(@buffer)
213:           @buffer = $' if cons and rv
214:         rescue
215:           @source = nil
216:         end
217:       end
218:       rv.taint
219:       rv
220:     end

[Source]

     # File lib/rexml/source.rb, line 226
226:     def position
227:       @er_source.stat.pipe? ? 0 : @er_source.pos
228:     end

[Source]

     # File lib/rexml/source.rb, line 190
190:     def read
191:       begin
192:         str = @source.readline(@line_break)
193:         str = decode(str) if @to_utf and str 
194:         @buffer << str
195:       rescue Exception, NameError
196:         @source = nil
197:       end
198:     end

[Source]

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

[Validate]