Class: REXML::Source

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

Overview

A Source can be searched for patterns, and wraps buffers and other objects and provides consumption of text

Direct Known Subclasses

IOSource

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Encoding

#decode, #encode

Constructor Details

#initialize(arg, encoding = nil) ⇒ Source

Constructor value, overriding all encoding detection

Parameters:

  • arg

    must be a String, and should be a valid XML document

  • encoding (defaults to: nil)

    if non-null, sets the encoding of the source to this



41
42
43
44
45
46
47
48
49
50
# File 'lib/rexml/source.rb', line 41

def initialize(arg, encoding=nil)
  @orig = arg
  @scanner = StringScanner.new(@orig)
  if encoding
    self.encoding = encoding
  else
    detect_encoding
  end
  @line = 0
end

Instance Attribute Details

#encodingObject

Returns the value of attribute encoding.



35
36
37
# File 'lib/rexml/source.rb', line 35

def encoding
  @encoding
end

#lineObject (readonly)

The line number of the last consumed text



34
35
36
# File 'lib/rexml/source.rb', line 34

def line
  @line
end

Instance Method Details

#bufferObject

The current buffer (what we’re going to read next)



53
54
55
# File 'lib/rexml/source.rb', line 53

def buffer
  @scanner.rest
end

#buffer_encoding=(encoding) ⇒ Object



57
58
59
# File 'lib/rexml/source.rb', line 57

def buffer_encoding=(encoding)
  @scanner.string.force_encoding(encoding)
end

#current_lineObject

Returns the current line in the source.

Returns:

  • the current line in the source



100
101
102
103
104
105
# File 'lib/rexml/source.rb', line 100

def current_line
  lines = @orig.split
  res = lines.grep @scanner.rest[0..30]
  res = res[-1] if res.kind_of? Array
  lines.index( res ) if res
end

#empty?Boolean

Returns true if the Source is exhausted.

Returns:

  • (Boolean)

    true if the Source is exhausted



95
96
97
# File 'lib/rexml/source.rb', line 95

def empty?
  @scanner.eos?
end

#ensure_bufferObject



75
76
# File 'lib/rexml/source.rb', line 75

def ensure_buffer
end

#match(pattern, cons = false) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/rexml/source.rb', line 78

def match(pattern, cons=false)
  if cons
    @scanner.scan(pattern).nil? ? nil : @scanner
  else
    @scanner.check(pattern).nil? ? nil : @scanner
  end
end

#positionObject



86
87
88
# File 'lib/rexml/source.rb', line 86

def position
  @scanner.pos
end

#position=(pos) ⇒ Object



90
91
92
# File 'lib/rexml/source.rb', line 90

def position=(pos)
  @scanner.pos = pos
end

#read(term = nil) ⇒ Object



68
69
# File 'lib/rexml/source.rb', line 68

def read(term = nil)
end

#read_until(term) ⇒ Object



71
72
73
# File 'lib/rexml/source.rb', line 71

def read_until(term)
  @scanner.scan_until(Regexp.union(term)) or @scanner.rest
end