Write plist without REXML

This commit is contained in:
Matthias Neeracher 2007-12-23 12:44:31 +00:00
parent 909201cf5d
commit 964d56a0f7

View File

@ -15,58 +15,49 @@ class PlistData
end end
end end
def _encodePlist(object) def _encodePlist(destination, object, indent)
e = nil destination.print " "*indent
case object case object
when false then when false then
e = REXML::Element.new("false") destination.print "<false/>\n"
when true then when true then
e = REXML::Element.new("true") destination.print "<true/>\n"
when String then when String then
e = REXML::Element.new("string") destination.print "<string>#{object}</string>\n"
e.add_text(object)
when PlistData then when PlistData then
e = REXML::Element.new("data") destination.print "<data>#{object}</data>\n"
e.add_text(object.to_s)
when Integer then when Integer then
e = REXML::Element.new("integer") destination.print "<integer>#{object}</integer>\n"
e.add_text(object.to_s)
when Float then when Float then
e = REXML::Element.new("real") destination.print "<real>#{object}</real>\n"
e.add_text(object.to_s)
when Time then when Time then
e = REXML::Element.new("date") destination.print "<date>#{object.utc.xmlschema}</date>\n"
e.add_text(object.utc.xmlschema)
when Array then when Array then
e = REXML::Element.new("array") destination.print "<array>\n"
object.each do |elt| object.each do |elt|
e.add_element(_encodePlist(elt)) _encodePlist(destination, elt, indent+2)
end end
destination.print "#{" "*indent}</array>\n"
when Hash then when Hash then
e = REXML::Element.new("dict") destination.print "<dict>\n"
object.keys.sort.each do |key| object.keys.sort.each do |key|
k = REXML::Element.new("key") destination.print "#{" "*indent} <key>#{key}</key>\n"
k.add_text(key) _encodePlist(destination, object[key], indent+2)
e.add_element(k)
e.add_element(_encodePlist(object[key]))
end end
destination.print "#{" "*indent}</dict>\n"
else else
raise "plistWriter can't encode objects of type `#{object.class}'" raise "plistWriter can't encode objects of type `#{object.class}'"
end end
return e
end end
def writePlist(destination, object) def writePlist(destination, object)
doc = REXML::Document.new destination.print <<'HEADER'
doc.add REXML::XMLDecl.new("1.0", "UTF-8") <?xml version='1.0' encoding='UTF-8'?>
doc.add REXML::DocType.new(["plist", "PUBLIC", <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
"\"-//Apple//DTD PLIST 1.0//EN\"", <plist version='1.0'>
"\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\""]) HEADER
contents = REXML::Element.new("plist")
contents.add_attribute("version", "1.0")
contents.add_element(_encodePlist(object))
doc.add_element(contents) _encodePlist(destination, object, 2)
doc.write(destination, 0)
destination.print "</plist>\n"
end end