Class: Plist::StreamParser
- Inherits:
-
Object
- Object
- Plist::StreamParser
- Defined in:
- lib/plist/parser.rb
Constant Summary collapse
- TEXT =
/([^<]+)/
- XMLDECL_PATTERN =
/<\?xml\s+(.*?)\?>*/m
- DOCTYPE_PATTERN =
/\s*<!DOCTYPE\s+(.*?)(\[|>)/m
- COMMENT_START =
/\A<!--/
- COMMENT_END =
/.*?-->/m
Instance Method Summary collapse
-
#initialize(plist_data_or_file, listener) ⇒ StreamParser
constructor
A new instance of StreamParser.
- #parse ⇒ Object
Constructor Details
#initialize(plist_data_or_file, listener) ⇒ StreamParser
Returns a new instance of StreamParser.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/plist/parser.rb', line 63 def initialize(plist_data_or_file, listener) if plist_data_or_file.respond_to? :read @xml = plist_data_or_file.read elsif File.exist? plist_data_or_file @xml = File.read(plist_data_or_file) else @xml = plist_data_or_file end @listener = listener end |
Instance Method Details
#parse ⇒ Object
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 108 109 110 111 112 113 114 |
# File 'lib/plist/parser.rb', line 81 def parse = PTag.mappings.keys.join('|') start_tag = /<(#{})([^>]*)>/i end_tag = /<\/(#{})[^>]*>/i require 'strscan' @scanner = StringScanner.new(@xml) until @scanner.eos? if @scanner.scan(COMMENT_START) @scanner.scan(COMMENT_END) elsif @scanner.scan(XMLDECL_PATTERN) encoding = parse_encoding_from_xml_declaration(@scanner[1]) next if encoding.nil? # use the specified encoding for the rest of the file next unless String.method_defined?(:force_encoding) @scanner.string = @scanner.rest.force_encoding(encoding) elsif @scanner.scan(DOCTYPE_PATTERN) next 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 |