summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG3
-rw-r--r--lib/plist/parser.rb21
-rw-r--r--test/test_parser.rb10
3 files changed, 23 insertions, 11 deletions
diff --git a/CHANGELOG b/CHANGELOG
index ea80aed..3a0fff4 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,5 +1,8 @@
= plist - All-purpose Property List manipulation library
+2007-02-22 (r81):
+ * make the plist parser accept strings contain XML or any object that responds to #read (File and StringIO being the intended targets here). Test and idea contributed by Chuck Remes.
+
2006-09-20 (r80):
* tweak a comment in generator.rb to make it clear that we're not using Base64.b64encode because it's broken.
diff --git a/lib/plist/parser.rb b/lib/plist/parser.rb
index 7d1de9f..e0623ae 100644
--- a/lib/plist/parser.rb
+++ b/lib/plist/parser.rb
@@ -59,8 +59,15 @@ module Plist
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
@@ -78,15 +85,7 @@ module Plist
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)
diff --git a/test/test_parser.rb b/test/test_parser.rb
index e3f8cdb..b6313c0 100644
--- a/test/test_parser.rb
+++ b/test/test_parser.rb
@@ -85,6 +85,16 @@ class TestParser < Test::Unit::TestCase
assert_nil( Plist::parse_xml( File.read('test/assets/commented.plist') ) )
end
end
+
+ def test_filename_or_xml_is_stringio
+ require 'stringio'
+
+ str = StringIO.new
+ data = Plist::parse_xml(str)
+
+ assert_nil data
+ end
+
end
__END__