From nobody at rubyforge.org Fri Feb 23 23:54:34 2007 From: nobody at rubyforge.org (nobody at rubyforge.org) Date: Fri, 23 Feb 2007 23:54:34 -0500 (EST) Subject: [plist-commits] [82] trunk: * parser now accepts strings or anything that responds to #read Message-ID: <20070224045435.1E9BB5242156@rubyforge.org> Revision: 82 Author: bleything Date: 2007-02-23 23:54:34 -0500 (Fri, 23 Feb 2007) Log Message: ----------- * parser now accepts strings or anything that responds to #read Modified Paths: -------------- trunk/CHANGELOG trunk/lib/plist/parser.rb trunk/test/test_parser.rb Modified: trunk/CHANGELOG =================================================================== --- trunk/CHANGELOG 2006-10-30 21:33:49 UTC (rev 81) +++ trunk/CHANGELOG 2007-02-24 04:54:34 UTC (rev 82) @@ -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. Modified: trunk/lib/plist/parser.rb =================================================================== --- trunk/lib/plist/parser.rb 2006-10-30 21:33:49 UTC (rev 81) +++ trunk/lib/plist/parser.rb 2007-02-24 04:54:34 UTC (rev 82) @@ -59,8 +59,15 @@ 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 @@ 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) Modified: trunk/test/test_parser.rb =================================================================== --- trunk/test/test_parser.rb 2006-10-30 21:33:49 UTC (rev 81) +++ trunk/test/test_parser.rb 2007-02-24 04:54:34 UTC (rev 82) @@ -85,6 +85,16 @@ 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__