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



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rexml/source.rb', line 87

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

Instance Attribute Details

#encodingObject

Returns the value of attribute encoding.



65
66
67
# File 'lib/rexml/source.rb', line 65

def encoding
  @encoding
end

#lineObject (readonly)

The line number of the last consumed text



64
65
66
# File 'lib/rexml/source.rb', line 64

def line
  @line
end

Instance Method Details

#bufferObject

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



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

def buffer
  @scanner.rest
end

#buffer_encoding=(encoding) ⇒ Object



110
111
112
# File 'lib/rexml/source.rb', line 110

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



175
176
177
178
179
180
# File 'lib/rexml/source.rb', line 175

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

#drop_parsed_contentObject



104
105
106
107
108
# File 'lib/rexml/source.rb', line 104

def drop_parsed_content
  if @scanner.pos > Private::SCANNER_RESET_SIZE
    @scanner.string = @scanner.rest
  end
end

#empty?Boolean

Returns true if the Source is exhausted.

Returns:

  • (Boolean)

    true if the Source is exhausted



170
171
172
# File 'lib/rexml/source.rb', line 170

def empty?
  @scanner.eos?
end

#ensure_bufferObject



134
135
# File 'lib/rexml/source.rb', line 134

def ensure_buffer
end

#match(pattern, cons = false) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/rexml/source.rb', line 137

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

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

Returns:

  • (Boolean)


145
146
147
148
149
150
151
# File 'lib/rexml/source.rb', line 145

def match?(pattern, cons=false)
  if cons
    !@scanner.skip(pattern).nil?
  else
    !@scanner.match?(pattern).nil?
  end
end

#peek_byteObject



161
162
163
# File 'lib/rexml/source.rb', line 161

def peek_byte
  @scanner.peek_byte
end

#positionObject



153
154
155
# File 'lib/rexml/source.rb', line 153

def position
  @scanner.pos
end

#position=(pos) ⇒ Object



157
158
159
# File 'lib/rexml/source.rb', line 157

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

#read(term = nil) ⇒ Object



121
122
# File 'lib/rexml/source.rb', line 121

def read(term = nil)
end

#read_until(term) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/rexml/source.rb', line 124

def read_until(term)
  pattern = Private::PRE_DEFINED_TERM_PATTERNS[term] || /#{Regexp.escape(term)}/
  data = @scanner.scan_until(pattern)
  unless data
    data = @scanner.rest
    @scanner.pos = @scanner.string.bytesize
  end
  data
end

#scan_byteObject



165
166
167
# File 'lib/rexml/source.rb', line 165

def scan_byte
  @scanner.scan_byte
end