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
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 "<false/>\n"
when true then
e = REXML::Element.new("true")
destination.print "<true/>\n"
when String then
e = REXML::Element.new("string")
e.add_text(object)
destination.print "<string>#{object}</string>\n"
when PlistData then
e = REXML::Element.new("data")
e.add_text(object.to_s)
destination.print "<data>#{object}</data>\n"
when Integer then
e = REXML::Element.new("integer")
e.add_text(object.to_s)
destination.print "<integer>#{object}</integer>\n"
when Float then
e = REXML::Element.new("real")
e.add_text(object.to_s)
destination.print "<real>#{object}</real>\n"
when Time then
e = REXML::Element.new("date")
e.add_text(object.utc.xmlschema)
destination.print "<date>#{object.utc.xmlschema}</date>\n"
when Array then
e = REXML::Element.new("array")
destination.print "<array>\n"
object.each do |elt|
e.add_element(_encodePlist(elt))
_encodePlist(destination, elt, indent+2)
end
destination.print "#{" "*indent}</array>\n"
when Hash then
e = REXML::Element.new("dict")
destination.print "<dict>\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>#{key}</key>\n"
_encodePlist(destination, object[key], indent+2)
end
destination.print "#{" "*indent}</dict>\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'
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version='1.0'>
HEADER
doc.add_element(contents)
doc.write(destination, 0)
_encodePlist(destination, object, 2)
destination.print "</plist>\n"
end