Class: REXML::Source

Inherits:
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

#check_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
# File 'lib/rexml/source.rb', line 41

def initialize(arg, encoding=nil)
  @orig = @buffer = arg
  if encoding
    self.encoding = encoding
  else
    self.encoding = check_encoding( @buffer )
  end
  @line = 0
end

Instance Attribute Details

#bufferObject (readonly)

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



32
33
34
# File 'lib/rexml/source.rb', line 32

def buffer
  @buffer
end

#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

#consume(pattern) ⇒ Object



92
93
94
# File 'lib/rexml/source.rb', line 92

def consume( pattern )
  @buffer = $' if pattern.match( @buffer )
end

#current_lineObject

Returns the current line in the source.

Returns:

  • the current line in the source



122
123
124
125
126
127
# File 'lib/rexml/source.rb', line 122

def current_line
  lines = @orig.split
  res = lines.grep @buffer[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



113
114
115
# File 'lib/rexml/source.rb', line 113

def empty?
  @buffer == ""
end

#match(pattern, cons = false) ⇒ Object



106
107
108
109
110
# File 'lib/rexml/source.rb', line 106

def match(pattern, cons=false)
  md = pattern.match(@buffer)
  @buffer = $' if cons and md
  return md
end

#match_to(char, pattern) ⇒ Object



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

def match_to( char, pattern )
  return pattern.match(@buffer)
end

#match_to_consume(char, pattern) ⇒ Object



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

def match_to_consume( char, pattern )
  md = pattern.match(@buffer)
  @buffer = $'
  return md
end

#positionObject



117
118
119
# File 'lib/rexml/source.rb', line 117

def position
  @orig.index( @buffer )
end

#readObject



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

def read
end

#scan(pattern, cons = false) ⇒ Object

Scans the source for a given pattern. Note, that this is not your usual scan() method. For one thing, the pattern argument has some requirements; for another, the source can be consumed. You can easily confuse this method. Originally, the patterns were easier to construct and this method more robust, because this method generated search regexes on the fly; however, this was computationally expensive and slowed down the entire REXML package considerably, since this is by far the most commonly called method. /^s*(#pattern, with no groups)(.*)/. The first group will be returned; the second group is used if the consume flag is set. everything after it in the Source. pattern is not found.

Parameters:

  • pattern

    must be a Regexp, and must be in the form of

  • consume

    if true, the pattern returned will be consumed, leaving

Returns:

  • the pattern, if found, or nil if the Source is empty or the



82
83
84
85
86
87
# File 'lib/rexml/source.rb', line 82

def scan(pattern, cons=false)
  return nil if @buffer.nil?
  rv = @buffer.scan(pattern)
  @buffer = $' if cons and rv.size>0
  rv
end