Class: Plist::StreamParser

Inherits:
Object
  • Object
show all
Defined in:
lib/facter/util/plist/parser.rb

Constant Summary

TEXT =
/([^<]+)/
XMLDECL_PATTERN =
/<\?xml\s+(.*?)\?>*/um
DOCTYPE_PATTERN =
/\s*<!DOCTYPE\s+(.*?)(\[|>)/um
COMMENT_START =
/\A<!--/u
COMMENT_END =
/.*?-->/um

Instance Method Summary (collapse)

Constructor Details

- (StreamParser) initialize(filename_or_xml, listener)

Returns a new instance of StreamParser



62
63
64
65
# File 'lib/facter/util/plist/parser.rb', line 62

def initialize( filename_or_xml, listener )
    @filename_or_xml = filename_or_xml
    @listener = listener
end

Instance Method Details

- (Object) parse



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/facter/util/plist/parser.rb', line 73

def parse
    plist_tags = PTag::mappings.keys.join('|')
    start_tag  = /<(#{plist_tags})([^>]*)>/i
    end_tag    = /<\/(#{plist_tags})[^>]*>/i

    require 'strscan'

    contents = (
        if (File.exists? @filename_or_xml)
            File.open(@filename_or_xml) {|f| f.read}
        else
            @filename_or_xml
        end
    )

    @scanner = StringScanner.new( contents )
    until @scanner.eos?
        if @scanner.scan(COMMENT_START)
            @scanner.scan(COMMENT_END)
        elsif @scanner.scan(XMLDECL_PATTERN)
        elsif @scanner.scan(DOCTYPE_PATTERN)
        elsif @scanner.scan(start_tag)
            @listener.tag_start(@scanner[1], nil)
            if (@scanner[2] =~ /\/$/)
                @listener.tag_end(@scanner[1])
            end
        elsif @scanner.scan(TEXT)
            @listener.text(@scanner[1])
        elsif @scanner.scan(end_tag)
            @listener.tag_end(@scanner[1])
        else
            raise "Unimplemented element"
        end
    end
end