lib/plist/parser.rb in plist-3.0.0 vs lib/plist/parser.rb in plist-3.1.0
- old
+ new
@@ -1,12 +1,13 @@
-#--###########################################################
-# Copyright 2006, Ben Bleything <ben@bleything.net> and #
-# Patrick May <patrick@hexane.org> #
-# #
-# Distributed under the MIT license. #
-##############################################################
-#++
+#!/usr/bin/env ruby
+#
+# = plist
+#
+# Copyright 2006-2010 Ben Bleything and Patrick May
+# Distributed under the MIT License
+#
+
# Plist parses Mac OS X xml property list files into ruby data structures.
#
# === Load a plist file
# This is the main point of the library:
#
@@ -57,12 +58,19 @@
end
end
end
class StreamParser
- def initialize( filename_or_xml, listener )
- @filename_or_xml = filename_or_xml
+ def initialize( plist_data_or_file, listener )
+ if plist_data_or_file.respond_to? :read
+ @xml = plist_data_or_file.read
+ elsif File.exists? plist_data_or_file
+ @xml = File.read( plist_data_or_file )
+ else
+ @xml = plist_data_or_file
+ end
+
@listener = listener
end
TEXT = /([^<]+)/
XMLDECL_PATTERN = /<\?xml\s+(.*?)\?>*/um
@@ -76,18 +84,10 @@
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 )
+ @scanner = StringScanner.new( @xml )
until @scanner.eos?
if @scanner.scan(COMMENT_START)
@scanner.scan(COMMENT_END)
elsif @scanner.scan(XMLDECL_PATTERN)
elsif @scanner.scan(DOCTYPE_PATTERN)