# REXML::Source (Class)

In: | rexml/source.rb
Parent: | Object

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

## Methods

consume current_line empty? encoding= match match_to match_to_consume new read scan

## Attributes

buffer | [R] | The current buffer (what we’re going to read next)
encoding | [R] |
line | [R] | The line number of the last consumed text

## Included Modules

Encoding

## Public Class methods

new(arg)

Constructor @param arg must be a String, and should be a valid XML document

[Source]

    # File rexml/source.rb, line 31
                    def initialize(arg)
                            @orig = @buffer = arg
                            self.encoding = check_encoding( @buffer )
                            #@buffer = decode(@buffer) unless @encoding == UTF_8

                            @line = 0
                    end

## Public Instance methods

encoding=(enc)

Inherited from Encoding Overridden to support optimized en/decoding

[Source]

    # File rexml/source.rb, line 40
                    def encoding=(enc)
                            super
                            @line_break = encode( '>' )
                            if enc != UTF_8
                                    @buffer = decode(@buffer)
                                    @to_utf = true
                            else
                                    @to_utf = false
                            end
                    end

scan(pattern, cons=false)

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.

[Source]

    # File rexml/source.rb, line 67
                    def scan(pattern, cons=false)
                            return nil if @buffer.nil?
                            rv = @buffer.scan(pattern)
                            @buffer = $' if cons and rv.size>0
                            rv
                    end

read()

[Source]

    # File rexml/source.rb, line 74
                    def read
                    end

consume( pattern )

[Source]

    # File rexml/source.rb, line 77
                    def consume( pattern )
                            @buffer = $' if pattern.match( @buffer )
                    end

match_to( char, pattern )

[Source]

    # File rexml/source.rb, line 81
                    def match_to( char, pattern )
                            return pattern.match(@buffer)
                    end

match_to_consume( char, pattern )

[Source]

    # File rexml/source.rb, line 85
                    def match_to_consume( char, pattern )
                            md = pattern.match(@buffer)
                            @buffer = $'
                            return md
                    end

match(pattern, cons=false)

[Source]

    # File rexml/source.rb, line 91
                    def match(pattern, cons=false)
                            md = pattern.match(@buffer)
                            @buffer = $' if cons and md
                            return md
                    end

empty?()

@return true if the Source is exhausted

[Source]

    # File rexml/source.rb, line 98
                    def empty?
                            @buffer.nil?
                    end

current_line()

@return the current line in the source

[Source]

    # File rexml/source.rb, line 103
                    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

[Validate]