2003-06-09 21:31:01 -04:00
|
|
|
require 'rexml/encoding'
|
|
|
|
|
|
|
|
module REXML
|
|
|
|
# Generates Source-s. USE THIS CLASS.
|
|
|
|
class SourceFactory
|
|
|
|
# Generates a Source object
|
|
|
|
# @param arg Either a String, or an IO
|
|
|
|
# @return a Source, or nil if a bad argument was given
|
|
|
|
def SourceFactory::create_from arg#, slurp=true
|
|
|
|
if arg.kind_of? String
|
|
|
|
source = Source.new(arg)
|
|
|
|
elsif arg.kind_of? IO
|
|
|
|
source = IOSource.new(arg)
|
|
|
|
end
|
|
|
|
source
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# A Source can be searched for patterns, and wraps buffers and other
|
|
|
|
# objects and provides consumption of text
|
|
|
|
class Source
|
|
|
|
include Encoding
|
|
|
|
# The current buffer (what we're going to read next)
|
|
|
|
attr_reader :buffer
|
|
|
|
# The line number of the last consumed text
|
|
|
|
attr_reader :line
|
|
|
|
attr_reader :encoding
|
|
|
|
|
|
|
|
# Constructor
|
|
|
|
# @param arg must be a String, and should be a valid XML document
|
2003-12-08 21:41:33 -05:00
|
|
|
def initialize(arg)
|
2003-06-09 21:31:01 -04:00
|
|
|
@orig = @buffer = arg
|
|
|
|
self.encoding = check_encoding( @buffer )
|
|
|
|
@line = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
# Inherited from Encoding
|
|
|
|
# Overridden to support optimized en/decoding
|
|
|
|
def encoding=(enc)
|
|
|
|
super
|
|
|
|
@line_break = encode( '>' )
|
|
|
|
if enc != UTF_8
|
|
|
|
@buffer = decode(@buffer)
|
|
|
|
@to_utf = true
|
|
|
|
else
|
|
|
|
@to_utf = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
# @param pattern must be a Regexp, and must be in the form of
|
|
|
|
# /^\s*(#{your pattern, with no groups})(.*)/. The first group
|
|
|
|
# will be returned; the second group is used if the consume flag is
|
|
|
|
# set.
|
|
|
|
# @param consume if true, the pattern returned will be consumed, leaving
|
|
|
|
# everything after it in the Source.
|
|
|
|
# @return the pattern, if found, or nil if the Source is empty or the
|
|
|
|
# pattern is not found.
|
2003-12-08 21:41:33 -05:00
|
|
|
def scan(pattern, cons=false)
|
2003-06-09 21:31:01 -04:00
|
|
|
return nil if @buffer.nil?
|
|
|
|
rv = @buffer.scan(pattern)
|
2003-12-08 21:41:33 -05:00
|
|
|
@buffer = $' if cons and rv.size>0
|
2003-06-09 21:31:01 -04:00
|
|
|
rv
|
|
|
|
end
|
|
|
|
|
|
|
|
def read
|
|
|
|
end
|
|
|
|
|
2003-10-10 08:54:46 -04:00
|
|
|
def consume( pattern )
|
|
|
|
@buffer = $' if pattern.match( @buffer )
|
|
|
|
end
|
|
|
|
|
|
|
|
def match_to( char, pattern )
|
|
|
|
return pattern.match(@buffer)
|
|
|
|
end
|
|
|
|
|
|
|
|
def match_to_consume( char, pattern )
|
|
|
|
md = pattern.match(@buffer)
|
|
|
|
@buffer = $'
|
|
|
|
return md
|
|
|
|
end
|
|
|
|
|
2003-12-08 21:41:33 -05:00
|
|
|
def match(pattern, cons=false)
|
2003-10-10 08:54:46 -04:00
|
|
|
md = pattern.match(@buffer)
|
2003-12-08 21:41:33 -05:00
|
|
|
@buffer = $' if cons and md
|
2003-06-09 21:31:01 -04:00
|
|
|
return md
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return true if the Source is exhausted
|
|
|
|
def empty?
|
2004-02-13 17:40:14 -05:00
|
|
|
@buffer == ""
|
2003-06-09 21:31:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# @return the current line in the source
|
|
|
|
def current_line
|
|
|
|
lines = @orig.split
|
2003-12-08 21:41:33 -05:00
|
|
|
res = lines.grep @buffer[0..30]
|
2003-06-09 21:31:01 -04:00
|
|
|
res = res[-1] if res.kind_of? Array
|
|
|
|
lines.index( res ) if res
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# A Source that wraps an IO. See the Source class for method
|
|
|
|
# documentation
|
|
|
|
class IOSource < Source
|
|
|
|
#attr_reader :block_size
|
|
|
|
|
2004-02-13 17:40:14 -05:00
|
|
|
# block_size has been deprecated
|
2003-12-08 21:41:33 -05:00
|
|
|
def initialize(arg, block_size=500)
|
2003-06-09 21:31:01 -04:00
|
|
|
@er_source = @source = arg
|
|
|
|
@to_utf = false
|
2004-02-13 17:40:14 -05:00
|
|
|
# FIXME
|
|
|
|
# This is broken. If the user puts in enough carriage returns, this can fail
|
|
|
|
# to calculate the correct encoding.
|
|
|
|
super @source.read( 100 )
|
2003-10-10 08:54:46 -04:00
|
|
|
@line_break = encode( '>' )
|
2003-06-09 21:31:01 -04:00
|
|
|
end
|
|
|
|
|
2003-12-08 21:41:33 -05:00
|
|
|
def scan(pattern, cons=false)
|
2003-06-09 21:31:01 -04:00
|
|
|
rv = super
|
|
|
|
# You'll notice that this next section is very similar to the same
|
|
|
|
# section in match(), but just a liiittle different. This is
|
|
|
|
# because it is a touch faster to do it this way with scan()
|
|
|
|
# than the way match() does it; enough faster to warrent duplicating
|
|
|
|
# some code
|
|
|
|
if rv.size == 0
|
|
|
|
until @buffer =~ pattern or @source.nil?
|
|
|
|
begin
|
|
|
|
# READLINE OPT
|
|
|
|
#str = @source.read(@block_size)
|
|
|
|
str = @source.readline(@line_break)
|
|
|
|
str = decode(str) if @to_utf and str
|
|
|
|
@buffer << str
|
|
|
|
rescue
|
|
|
|
@source = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rv = super
|
|
|
|
end
|
|
|
|
rv.taint
|
|
|
|
rv
|
|
|
|
end
|
|
|
|
|
|
|
|
def read
|
|
|
|
begin
|
|
|
|
str = @source.readline('>')
|
|
|
|
str = decode(str) if @to_utf and str
|
|
|
|
@buffer << str
|
2003-10-10 08:54:46 -04:00
|
|
|
rescue Exception, NameError
|
2003-06-09 21:31:01 -04:00
|
|
|
@source = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-10-10 08:54:46 -04:00
|
|
|
def consume( pattern )
|
|
|
|
match( pattern, true )
|
|
|
|
end
|
|
|
|
|
2003-12-08 21:41:33 -05:00
|
|
|
def match( pattern, cons=false )
|
2003-06-09 21:31:01 -04:00
|
|
|
rv = pattern.match(@buffer)
|
2003-12-08 21:41:33 -05:00
|
|
|
@buffer = $' if cons and rv
|
2003-06-09 21:31:01 -04:00
|
|
|
while !rv and @source
|
|
|
|
begin
|
|
|
|
str = @source.readline('>')
|
|
|
|
str = decode(str) if @to_utf and str
|
|
|
|
@buffer << str
|
|
|
|
rv = pattern.match(@buffer)
|
2003-12-08 21:41:33 -05:00
|
|
|
@buffer = $' if cons and rv
|
2003-06-09 21:31:01 -04:00
|
|
|
rescue
|
|
|
|
@source = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rv.taint
|
|
|
|
rv
|
|
|
|
end
|
|
|
|
|
|
|
|
def empty?
|
|
|
|
super and ( @source.nil? || @source.eof? )
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return the current line in the source
|
|
|
|
def current_line
|
|
|
|
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
|
|
|
|
[pos, lineno, line]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|