Class: REXML::IOSource
Overview
A Source that wraps an IO. See the Source class for method documentation
Constant Summary
Constants included from Source::Private
Source::Private::PRE_DEFINED_TERM_PATTERNS
Instance Attribute Summary
Attributes inherited from Source
Attributes included from Encoding
Instance Method Summary collapse
-
#current_line ⇒ Object
The current line in the source.
- #empty? ⇒ Boolean
- #ensure_buffer ⇒ Object
-
#initialize(arg, block_size = 500, encoding = nil) ⇒ IOSource
constructor
block_size has been deprecated.
-
#match(pattern, cons = false) ⇒ Object
Note: When specifying a string for ‘pattern’, it must not include ‘>’ except in the following formats: - “>” - “XXX>” (X is any string excluding ‘>’).
- #read(term = nil) ⇒ Object
- #read_until(term) ⇒ Object
Methods inherited from Source
#buffer, #buffer_encoding=, #position, #position=
Methods included from Encoding
Constructor Details
#initialize(arg, block_size = 500, encoding = nil) ⇒ IOSource
block_size has been deprecated
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/rexml/source.rb', line 180 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_line ⇒ Object
Returns the current line in the source.
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/rexml/source.rb', line 258 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 |
#empty? ⇒ Boolean
253 254 255 |
# File 'lib/rexml/source.rb', line 253 def empty? super and ( @source.nil? || @source.eof? ) end |
#ensure_buffer ⇒ Object
230 231 232 |
# File 'lib/rexml/source.rb', line 230 def ensure_buffer read if @scanner.eos? && @source end |
#match(pattern, cons = false) ⇒ Object
Note: When specifying a string for ‘pattern’, it must not include ‘>’ except in the following formats:
-
“>”
-
“XXX>” (X is any string excluding ‘>’)
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/rexml/source.rb', line 237 def match( pattern, cons=false ) 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 end md.nil? ? nil : @scanner end |
#read(term = nil) ⇒ Object
201 202 203 204 205 206 207 208 209 210 |
# File 'lib/rexml/source.rb', line 201 def read(term = nil) term = encode(term) if term begin @scanner << readline(term) true rescue Exception, NameError @source = nil false end end |
#read_until(term) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/rexml/source.rb', line 212 def read_until(term) pattern = Private::PRE_DEFINED_TERM_PATTERNS[term] || /#{Regexp.escape(term)}/ 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 |