diff options
author | Ben Bleything <ben@bleything.net> | 2007-02-24 04:54:34 +0000 |
---|---|---|
committer | Ben Bleything <ben@bleything.net> | 2007-02-24 04:54:34 +0000 |
commit | ae748c0236b562437f0703c435a28bfedf33bab0 (patch) | |
tree | 31d6111d56031679e8302320255e7b103927d418 | |
parent | 6b001518d24c1af88527c28e5eac31d664df0369 (diff) | |
download | plist-ae748c0236b562437f0703c435a28bfedf33bab0.tar.gz |
* parser now accepts strings or anything that responds to #read
-rw-r--r-- | CHANGELOG | 3 | ||||
-rw-r--r-- | lib/plist/parser.rb | 21 | ||||
-rw-r--r-- | test/test_parser.rb | 10 |
3 files changed, 23 insertions, 11 deletions
@@ -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__ |