Class: REXML::IOSource

Inherits:
Source
  • Object
show all
Defined in:
lib/rexml/source.rb

Overview

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

Instance Attribute Summary

Attributes inherited from Source

#encoding, #line

Attributes included from Encoding

#encoding

Instance Method Summary collapse

Methods inherited from Source

#buffer, #buffer_encoding=, #drop_parsed_content, #position, #position=

Methods included from Encoding

#decode, #encode

Constructor Details

#initialize(arg, block_size = 500, encoding = nil) ⇒ IOSource

block_size has been deprecated



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/rexml/source.rb', line 205

def initialize(arg, block_size=500, encoding=nil)
  @er_source = @source = arg
  @to_utf = false
  @pending_buffer = nil

  if encoding
    super("", encoding)
  else
    super(@source.read(3) || "")
  end

  if !@to_utf and
      @orig.respond_to?(:force_encoding) and
      @source.respond_to?(:external_encoding) and
      @source.external_encoding != ::Encoding::UTF_8
    @force_utf8 = true
  else
    @force_utf8 = false
  end
end

Instance Method Details

#current_lineObject

Returns the current line in the source.

Returns:

  • the current line in the source



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/rexml/source.rb', line 310

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
    @er_source.seek(pos)
  rescue IOError, SystemCallError
    pos = -1
    line = -1
  end
  [pos, lineno, line]
end

#empty?Boolean

Returns:

  • (Boolean)


305
306
307
# File 'lib/rexml/source.rb', line 305

def empty?
  super and ( @source.nil? || @source.eof? )
end

#ensure_bufferObject



265
266
267
# File 'lib/rexml/source.rb', line 265

def ensure_buffer
  read if @scanner.eos? && @source
end

#match(pattern, cons = false) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/rexml/source.rb', line 269

def match( pattern, cons=false )
  # To avoid performance issue, we need to increase bytes to read per scan
  min_bytes = 1
  while true
    if cons
      md = @scanner.scan(pattern)
    else
      md = @scanner.check(pattern)
    end
    break if md
    return nil if pattern.is_a?(String)
    return nil if @source.nil?
    return nil unless read(nil, min_bytes)
    min_bytes *= 2
  end

  md.nil? ? nil : @scanner
end

#match?(pattern, cons = false) ⇒ Boolean

Returns:

  • (Boolean)


288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/rexml/source.rb', line 288

def match?( pattern, cons=false )
  # To avoid performance issue, we need to increase bytes to read per scan
  min_bytes = 1
  while true
    if cons
      n_matched_bytes = @scanner.skip(pattern)
    else
      n_matched_bytes = @scanner.match?(pattern)
    end
    return true if n_matched_bytes
    return false if pattern.is_a?(String)
    return false if @source.nil?
    return false unless read(nil, min_bytes)
    min_bytes *= 2
  end
end

#read(term = nil, min_bytes = 1) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/rexml/source.rb', line 226

def read(term = nil, min_bytes = 1)
  term = encode(term) if term
  begin
    str = readline(term)
    @scanner << str
    read_bytes = str.bytesize
    begin
      while read_bytes < min_bytes
        str = readline(term)
        @scanner << str
        read_bytes += str.bytesize
      end
    rescue IOError
    end
    true
  rescue Exception, NameError
    @source = nil
    false
  end
end

#read_until(term) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/rexml/source.rb', line 247

def read_until(term)
  pattern = Private::PRE_DEFINED_TERM_PATTERNS[term] || /#{Regexp.escape(term)}/
  term = @encoded_terms[term] ||= encode(term)
  until str = @scanner.scan_until(pattern)
    break if @source.nil?
    break if @source.eof?
    @scanner << readline(term)
  end
  if str
    read if @scanner.eos? and !@source.eof?
    str
  else
    rest = @scanner.rest
    @scanner.pos = @scanner.string.bytesize
    rest
  end
end