From 964d56a0f75b4901cf94df4bf656ac15729fa57b Mon Sep 17 00:00:00 2001 From: Matthias Neeracher Date: Sun, 23 Dec 2007 12:44:31 +0000 Subject: [PATCH] Write plist without REXML --- Filters/plistWriter.rb | 57 ++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/Filters/plistWriter.rb b/Filters/plistWriter.rb index 8c24953..fd048ab 100644 --- a/Filters/plistWriter.rb +++ b/Filters/plistWriter.rb @@ -15,58 +15,49 @@ class PlistData end end -def _encodePlist(object) - e = nil +def _encodePlist(destination, object, indent) + destination.print " "*indent case object when false then - e = REXML::Element.new("false") + destination.print "\n" when true then - e = REXML::Element.new("true") + destination.print "\n" when String then - e = REXML::Element.new("string") - e.add_text(object) + destination.print "#{object}\n" when PlistData then - e = REXML::Element.new("data") - e.add_text(object.to_s) + destination.print "#{object}\n" when Integer then - e = REXML::Element.new("integer") - e.add_text(object.to_s) + destination.print "#{object}\n" when Float then - e = REXML::Element.new("real") - e.add_text(object.to_s) + destination.print "#{object}\n" when Time then - e = REXML::Element.new("date") - e.add_text(object.utc.xmlschema) + destination.print "#{object.utc.xmlschema}\n" when Array then - e = REXML::Element.new("array") + destination.print "\n" object.each do |elt| - e.add_element(_encodePlist(elt)) + _encodePlist(destination, elt, indent+2) end + destination.print "#{" "*indent}\n" when Hash then - e = REXML::Element.new("dict") + destination.print "\n" object.keys.sort.each do |key| - k = REXML::Element.new("key") - k.add_text(key) - e.add_element(k) - e.add_element(_encodePlist(object[key])) + destination.print "#{" "*indent} #{key}\n" + _encodePlist(destination, object[key], indent+2) end + destination.print "#{" "*indent}\n" else raise "plistWriter can't encode objects of type `#{object.class}'" end - - return e end def writePlist(destination, object) - doc = REXML::Document.new - doc.add REXML::XMLDecl.new("1.0", "UTF-8") - doc.add REXML::DocType.new(["plist", "PUBLIC", - "\"-//Apple//DTD PLIST 1.0//EN\"", - "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\""]) - contents = REXML::Element.new("plist") - contents.add_attribute("version", "1.0") - contents.add_element(_encodePlist(object)) + destination.print <<'HEADER' + + + +HEADER - doc.add_element(contents) - doc.write(destination, 0) + _encodePlist(destination, object, 2) + + destination.print "\n" end