Class Plist::StreamParser
In: lib/facter/util/plist/parser.rb
Parent: Object

Methods

new   parse  

Constants

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

Public Class methods

[Source]

    # File lib/facter/util/plist/parser.rb, line 62
62:     def initialize( filename_or_xml, listener )
63:       @filename_or_xml = filename_or_xml
64:       @listener = listener
65:     end

Public Instance methods

[Source]

     # File lib/facter/util/plist/parser.rb, line 74
 74:     def parse
 75:       plist_tags = PTag::mappings.keys.join('|')
 76:       start_tag  = /<(#{plist_tags})([^>]*)>/i
 77:       end_tag    = /<\/(#{plist_tags})[^>]*>/i
 78: 
 79:       require 'strscan'
 80: 
 81:       contents = (
 82:         if (File.exists? @filename_or_xml)
 83:           File.open(@filename_or_xml) {|f| f.read}
 84:         else
 85:           @filename_or_xml
 86:         end
 87:       )
 88: 
 89:       @scanner = StringScanner.new( contents )
 90:       until @scanner.eos?
 91:         if @scanner.scan(COMMENT_START)
 92:           @scanner.scan(COMMENT_END)
 93:         elsif @scanner.scan(XMLDECL_PATTERN)
 94:         elsif @scanner.scan(DOCTYPE_PATTERN)
 95:         elsif @scanner.scan(start_tag)
 96:           @listener.tag_start(@scanner[1], nil)
 97:           if (@scanner[2] =~ /\/$/)
 98:             @listener.tag_end(@scanner[1])
 99:           end
100:         elsif @scanner.scan(TEXT)
101:           @listener.text(@scanner[1])
102:         elsif @scanner.scan(end_tag)
103:           @listener.tag_end(@scanner[1])
104:         else
105:           raise "Unimplemented element"
106:         end
107:       end
108:     end

[Validate]