123456789_123456789_123456789_123456789_123456789_

Class: REXML::IOSource

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Source
Instance Chain:
self, Source, Encoding
Inherits: REXML::Source
Defined in: lib/rexml/source.rb

Overview

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

Class Method Summary

Source - Inherited

.new

Constructor value, overriding all encoding detection.

Instance Attribute Summary

Source - Inherited

#buffer_encoding=, #empty?, #encoding,
#encoding=

Inherited from Encoding Overridden to support optimized en/decoding.

#line

The line number of the last consumed text.

#position, #position=

Encoding - Included

#encoding

ID —> Encoding name.

#encoding=

Instance Method Summary

Constructor Details

.new(arg, block_size = 500, encoding = nil) ⇒ IOSource

block_size has been deprecated

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 187

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 Attribute Details

#empty?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 270

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

Instance Method Details

#current_lineObject

Returns:

  • the current line in the source

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 275

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
    pos = -1
    line = -1
  end
  [pos, lineno, line]
end

#encoding_updated (private)

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 322

def encoding_updated
  case @encoding
  when "UTF-16BE", "UTF-16LE"
    @source.binmode
    @source.set_encoding(@encoding, @encoding)
  end
  @line_break = encode(">")
  @pending_buffer, @scanner.string = @scanner.rest, ""
  @pending_buffer.force_encoding(@encoding)
  super
end

#ensure_buffer

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 247

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

#match(pattern, cons = false)

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 251

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

#read(term = nil, min_bytes = 1)

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 208

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)

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 229

def read_until(term)
  pattern = Private::PRE_DEFINED_TERM_PATTERNS[term] || /#{Regexp.escape(term)}/
  term = @term_encord[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

#readline(term = nil) (private)

[ GitHub ]

  
# File 'lib/rexml/source.rb', line 297

def readline(term = nil)
  if @pending_buffer
    begin
      str = @source.readline(term || @line_break)
    rescue IOError
    end
    if str.nil?
      str = @pending_buffer
    else
      str = @pending_buffer + str
    end
    @pending_buffer = nil
  else
    str = @source.readline(term || @line_break)
  end
  return nil if str.nil?

  if @to_utf
    decode(str)
  else
    str.force_encoding(::Encoding::UTF_8) if @force_utf8
    str
  end
end