class EimXML::Parser

Attributes

scanner[R]

Public Class Methods

new(src) click to toggle source
# File lib/eim_xml/parser.rb, line 24
def initialize(src)
        @scanner = StringScanner.new(src)
        @scanner.scan(/\s*<\?.*?\?>\s*/)
end

Public Instance Methods

parse() click to toggle source
# File lib/eim_xml/parser.rb, line 29
def parse
        if @scanner.scan(RE::EMPTY_ELEMENT)
                parse_empty_element
        elsif @scanner.scan(RE::START_TAG)
                parse_start_tag
        elsif @scanner.scan(RE::STRING)
                parse_string
        else
                nil
        end
end

Protected Instance Methods

parse_empty_element() click to toggle source
# File lib/eim_xml/parser.rb, line 49
def parse_empty_element
        parse_tag
end
parse_start_tag() click to toggle source
# File lib/eim_xml/parser.rb, line 54
def parse_start_tag
        e = parse_tag

        until @scanner.scan(RE::END_TAG)
                c = parse
                raise ParseError.new("Syntax error.") unless c
                e << c
        end
        raise ParseError.new("End tag mismatched.") unless @scanner[1].to_sym==e.name
        e
end
parse_string() click to toggle source
# File lib/eim_xml/parser.rb, line 67
def parse_string
        s = @scanner[0]
        s = s.gsub(/&(amp|quot|apos|lt|gt);/) do
                case $1
                when "amp"
                        "&"
                when "quot"
                        '"'
                when "apos"
                        "'"
                when "lt"
                        "<"
                when "gt"
                        ">"
                end
        end
        PCString.new(s)
end
parse_tag() click to toggle source
# File lib/eim_xml/parser.rb, line 41
def parse_tag
        s = StringScanner.new(@scanner[1])
        e = Element.new(s.scan(/\S+/))
        e[s[1]] = s[3] ? s[3] : s[4] while s.scan(RE::ATTRIBUTE)
        e
end