lib/plist/generator.rb in plist-2.1.2 vs lib/plist/generator.rb in plist-3.0.0

- old
+ new

@@ -1,167 +1,224 @@ -############################################################## +#--########################################################### # Copyright 2006, Ben Bleything <ben@bleything.net> and # # Patrick May <patrick@hexane.org> # # # # Distributed under the MIT license. # ############################################################## - -# === Save a plist -# You can turn the variables back into a plist string: -# -# r.to_plist -# -# There is a convenience method for saving a variable to a file: -# -# r.save_plist(filename) -# -# Only these ruby types can be converted into a plist: -# -# String -# Float -# DateTime -# Integer -# FalseClass -# TrueClass -# Array -# Hash -# -# Notes: -# -# + Array and Hash are recursive -- the elements of an Array and the values of a Hash -# must convert to a plist. -# + The keys of the Hash must be strings. -# + The contents of data elements are returned as a Tempfile. -# + Data elements can be set with to an open IO or a StringIO -# -# If you have suggestions for mapping other Ruby types to the plist types, send a note to: -# -# mailto:plist@hexane.org -# -# I'll take a look and probably add it, I'm just reticent to create too many -# "convenience" methods without at least agreeing with someone :-) +#++ +# See Plist::Emit. module Plist + # === Create a plist + # You can dump an object to a plist in one of two ways: + # + # * <tt>Plist::Emit.dump(obj)</tt> + # * <tt>obj.to_plist</tt> + # * This requires that you mixin the <tt>Plist::Emit</tt> module, which is already done for +Array+ and +Hash+. + # + # The following Ruby classes are converted into native plist types: + # Array, Bignum, Date, DateTime, Fixnum, Float, Hash, Integer, String, Symbol, Time, true, false + # * +Array+ and +Hash+ are both recursive; their elements will be converted into plist nodes inside the <array> and <dict> containers (respectively). + # * +IO+ (and its descendants) and +StringIO+ objects are read from and their contents placed in a <data> element. + # * User classes may implement +to_plist_node+ to dictate how they should be serialized; otherwise the object will be passed to <tt>Marshal.dump</tt> and the result placed in a <data> element. + # + # For detailed usage instructions, refer to USAGE[link:files/docs/USAGE.html] and the methods documented below. module Emit + # Helper method for injecting into classes. Calls <tt>Plist::Emit.dump</tt> with +self+. + def to_plist(envelope = true) + return Plist::Emit.dump(self, envelope) + end + + # Helper method for injecting into classes. Calls <tt>Plist::Emit.save_plist</tt> with +self+. def save_plist(filename) + Plist::Emit.save_plist(self, filename) + end + + # The following Ruby classes are converted into native plist types: + # Array, Bignum, Date, DateTime, Fixnum, Float, Hash, Integer, String, Symbol, Time + # + # Write us (via RubyForge) if you think another class can be coerced safely into one of the expected plist classes. + # + # +IO+ and +StringIO+ objects are encoded and placed in <data> elements; other objects are <tt>Marshal.dump</tt>'ed unless they implement +to_plist_node+. + # + # The +envelope+ parameters dictates whether or not the resultant plist fragment is wrapped in the normal XML/plist header and footer. Set it to false if you only want the fragment. + def self.dump(obj, envelope = true) + output = plist_node(obj) + + output = wrap(output) if envelope + + return output + end + + # Writes the serialized object's plist to the specified filename. + def self.save_plist(obj, filename) File.open(filename, 'wb') do |f| - f.write(self.to_plist) + f.write(obj.to_plist) end end - # Only the expected classes can be emitted as a plist: - # String, Float, DateTime, Integer, TrueClass, FalseClass, Array, Hash - # - # Write me if you think another class can be coerced safely into one of the - # expected plist classes (plist@hexane.org) - def to_plist( header = true ) - if (header) - Plist::_xml(self.to_plist_node) + private + def self.plist_node(element) + output = '' + + if element.respond_to? :to_plist_node + output << element.to_plist_node else - self.to_plist_node + case element + when Array + if element.empty? + output << "<array/>\n" + else + output << tag('array') { + element.collect {|e| plist_node(e)} + } + end + when Hash + if element.empty? + output << "<dict/>\n" + else + inner_tags = [] + + element.keys.sort.each do |k| + v = element[k] + inner_tags << tag('key', CGI::escapeHTML(k.to_s)) + inner_tags << plist_node(v) + end + + output << tag('dict') { + inner_tags + } + end + when true, false + output << "<#{element}/>\n" + when Time + output << tag('date', element.utc.strftime('%Y-%m-%dT%H:%M:%SZ')) + when Date # also catches DateTime + output << tag('date', element.strftime('%Y-%m-%dT%H:%M:%SZ')) + when String, Symbol, Fixnum, Bignum, Integer, Float + output << tag(element_type(element), CGI::escapeHTML(element.to_s)) + when IO, StringIO + element.rewind + contents = element.read + # note that apple plists are wrapped at a different length then + # what ruby's base64 wraps by default. + # I used #encode64 instead of #b64encode (which allows a length arg) + # because b64encode is b0rked and ignores the length arg. + data = "\n" + Base64::encode64(contents).gsub(/\s+/, '').scan(/.{1,68}/o) { data << $& << "\n" } + output << tag('data', data) + else + output << comment( 'The <data> element below contains a Ruby object which has been serialized with Marshal.dump.' ) + data = "\n" + Base64::encode64(Marshal.dump(element)).gsub(/\s+/, '').scan(/.{1,68}/o) { data << $& << "\n" } + output << tag('data', data ) + end end + + return output end - end -end -class String - include Plist::Emit - def to_plist_node - "<string>#{CGI::escapeHTML(self)}</string>" - end -end + def self.comment(content) + return "<!-- #{content} -->\n" + end -class Symbol - include Plist::Emit - def to_plist_node - "<string>#{CGI::escapeHTML(self.to_s)}</string>" - end -end + def self.tag(type, contents = '', &block) + out = nil -class Float - include Plist::Emit - def to_plist_node - "<real>#{self}</real>" - end -end + if block_given? + out = IndentedString.new + out << "<#{type}>" + out.raise_indent -class Time - include Plist::Emit - def to_plist_node - "<date>#{self.utc.strftime('%Y-%m-%dT%H:%M:%SZ')}</date>" - end -end + out << block.call -class Date - include Plist::Emit - def to_plist_node - "<date>#{self.strftime('%Y-%m-%dT%H:%M:%SZ')}</date>" - end -end + out.lower_indent + out << "</#{type}>" + else + out = "<#{type}>#{contents.to_s}</#{type}>\n" + end -class Integer - include Plist::Emit - def to_plist_node - "<integer>#{self}</integer>" - end -end + return out.to_s + end -class FalseClass - include Plist::Emit - def to_plist_node - "<false/>" - end -end + def self.wrap(contents) + output = '' -class TrueClass - include Plist::Emit - def to_plist_node - "<true/>" - end -end + output << '<?xml version="1.0" encoding="UTF-8"?>' + "\n" + output << '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' + "\n" + output << '<plist version="1.0">' + "\n" -class Array - include Plist::Emit - def to_plist_node - fragment = "<array>\n" - self.each do |e| - element_plist = e.to_plist_node - element_plist.each do |l| - fragment += "\t#{l.chomp}\n" + output << contents + + output << '</plist>' + "\n" + + return output + end + + def self.element_type(item) + return case item + when String, Symbol: 'string' + when Fixnum, Bignum, Integer: 'integer' + when Float: 'real' + else + raise "Don't know about this data type... something must be wrong!" end end - fragment += "</array>" - fragment - end -end + private + class IndentedString #:nodoc: + attr_accessor :indent_string -class Hash - include Plist::Emit - def to_plist_node - fragment = "<dict>\n" - self.keys.sort.each do |k| - fragment += "\t<key>#{CGI::escapeHTML(k)}</key>\n" - element_plist = self[k].to_plist_node - element_plist.each do |l| - fragment += "\t#{l.chomp}\n" + @@indent_level = 0 + + def initialize(str = "\t") + @indent_string = str + @contents = '' end + + def to_s + return @contents + end + + def raise_indent + @@indent_level += 1 + end + + def lower_indent + @@indent_level -= 1 if @@indent_level > 0 + end + + def <<(val) + if val.is_a? Array + val.each do |f| + self << f + end + else + # if it's already indented, don't bother indenting further + unless val =~ /\A#{@indent_string}/ + indent = @indent_string * @@indent_level + + @contents << val.gsub(/^/, indent) + else + @contents << val + end + + # it already has a newline, don't add another + @contents << "\n" unless val =~ /\n$/ + end + end end - fragment += "</dict>" - fragment end end -require 'stringio' -[ IO, StringIO ].each do |io_class| - io_class.module_eval do - include Plist::Emit - def to_plist_node - self.rewind - data = self.read +# we need to add this so sorting hash keys works properly +class Symbol #:nodoc: + def <=> (other) + self.to_s <=> other.to_s + end +end - output = "<data>\n" - Base64::encode64(data).gsub(/\s+/, '').scan(/.{1,68}/o) { output << $& << "\n" } - output << "</data>" +class Array #:nodoc: + include Plist::Emit +end - output - end - end +class Hash #:nodoc: + include Plist::Emit end