From ae748c0236b562437f0703c435a28bfedf33bab0 Mon Sep 17 00:00:00 2001 From: Ben Bleything Date: Sat, 24 Feb 2007 04:54:34 +0000 Subject: * parser now accepts strings or anything that responds to #read --- CHANGELOG | 3 +++ lib/plist/parser.rb | 21 ++++++++++----------- test/test_parser.rb | 10 ++++++++++ 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__ -- cgit v1.2.1