plist.utf8 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7ca6fe236f2c4df09de6841dc0fc755e92711186
4
+ data.tar.gz: 3b54a51ca5cdd9bad9573f051e9ecc829f393516
5
+ SHA512:
6
+ metadata.gz: 073665ea04596a959761632fc7c93dee2f280ab56182a6aee8be24f862905ad723b2e1717140d654208984f1f38f9f4976b571341ae1ab6c92b178450c81e174
7
+ data.tar.gz: 8369a4e9c0da6a1b45ce640cbee89fdb08388fbf38d5497bfbcfbc922477ec204afc62480753ab4976befd91a7e5f7abc3605a7119e3207b4ed096e5978875eb
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ rdoc
2
+ coverage
3
+ pkg
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - jruby-18mode
6
+ - jruby-19mode
7
+ - rbx-18mode
8
+ - rbx-19mode
9
+ - ruby-head
10
+ - jruby-head
11
+ - 1.8.7
12
+ - ree
data/CHANGELOG ADDED
@@ -0,0 +1,103 @@
1
+ = plist - All-purpose Property List manipulation library
2
+
3
+ === Release version 3.0.0!
4
+
5
+ 2010-02-23:
6
+ * Ruby 1.9.x compatibility!
7
+
8
+ 2010-02-16:
9
+ * excise a bunch of unnecessary @@ variables
10
+ * fix up some tests for cross-version compatibility
11
+
12
+ 2010-02-14:
13
+ * generalized cleanup:
14
+ * fix old file headers
15
+ * modernize rakefile
16
+ * clean up rdoc
17
+
18
+ 2010-01-08:
19
+ * move from RubyForge Subversion to GitHub
20
+
21
+ 2007-02-22 (r81):
22
+ * 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.
23
+
24
+ 2006-09-20 (r80):
25
+ * tweak a comment in generator.rb to make it clear that we're not using Base64.b64encode because it's broken.
26
+
27
+ === Release version 3.0.0!
28
+
29
+ 2006-09-20 (r77 - r79):
30
+ * move IndentedString inside Plist::Emit and :nodoc: it
31
+ * Tag 3.0.0! (from rev 78)
32
+
33
+ 2006-09-19 (r73 - r75):
34
+ * Really fix the rakefile this time (apparently I deleted some code that I needed...)
35
+ * alter the fix_whitespace rake task to ignore the assets directory
36
+ * cleanup whitespace
37
+
38
+ 2006-09-18 (r70 - r72):
39
+ * Update this file ;)
40
+ * Fix Rakefile
41
+ * gem install -t now works correctly
42
+ * Remove super-sekr1t rdoc staging area from rdoc publishing task
43
+
44
+ 2006-09-15 (r64 - r69):
45
+ * Change behavior of empty collection elements to match What Apple Does
46
+ * Fix some gem packaging infrastructure
47
+
48
+ 2006-09-13 (r61 - r63):
49
+ * Merge generator injection removal branch into trunk!
50
+
51
+ 2006-09-13 (r52 - r60):
52
+ * Fix indentation/newlines in generator (finally!)
53
+ * Refix indentation to be more faithful to the way Apple emits their plists
54
+ * Remove horrific regex and replace it with proper comment parsing
55
+ * Empty plists return nil when parsed
56
+ * Sort hash keys before emitting (now we can test multi-element hashes!)
57
+ * Inject #<=> into Symbol so that sorting Symbol-keyed hashes won't freak out
58
+
59
+ 2006-09-12 (r47 - r51):
60
+ * More test rejiggering
61
+ * New tests to expose some bugs
62
+
63
+ 2006-09-10 (r33 - r46):
64
+ * Update tests for new generator code
65
+ * Rejigger some tests
66
+ * Make the generator try to call #to_plist_node on any object it tries to serialize, thus allowing class authors to define how their objects will be serialized
67
+ * Marshal.dump unrecognized objects into <data> elements
68
+ * Make the parser strip out comments and Marshal.load <data> elements if possible
69
+ * Update some rdoc
70
+
71
+ === Release version 2.1.1!
72
+
73
+ 2006-09-10 (r31 - r32):
74
+ * Added encoding / decoding for entities (&amp; etc)
75
+ * Changed parsing of <data> elements to return StringIO objects
76
+ * Fixed bug with empty <key> tags
77
+
78
+ 2006-08-24 (r25 - r30):
79
+ * Invert ownership of methods in the generator, allowing us to remove the self.extend(self)
80
+ * New branch to remove method inject from parser
81
+
82
+ 2006-08-23 (r22 - r24):
83
+ * Add rcov task to Rakefile
84
+ * Add some tests
85
+
86
+ 2006-08-20 (r9 - r21):
87
+ * Add a bunch of rdoc and rdoc infrastructure
88
+ * Add rake task to clean up errant whitespace
89
+ * Spin off a branch to remove a bunch of method injection in the generator code
90
+ * Rename some tests for clarity's sake
91
+ * Replace NARF generation code with Ben's generation code
92
+ * Update tests
93
+ * This broke indentation (will be fixed later)
94
+ * Add Plist::Emit.dump, so you can dump objects which don't include Plist::Emit, update tests to match
95
+ * Fix a bug with the method that wraps output in the plist header/footer
96
+
97
+ 2006-08-19 (r1 - r8):
98
+ * The beginnings of merging the plist project into the NARF plist library (under the plist project's name)
99
+ * fancier project infrastructure (more tests, Rakefile, the like)
100
+ * Add/update copyright notices in the source files
101
+ * Move a bunch of documentation out to README
102
+ * Split library into chunks
103
+ * Properly delete files when cleaning up from tests
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://ruby.taobao.org'
2
+
data/Gemfile.lock ADDED
@@ -0,0 +1,8 @@
1
+ GEM
2
+ remote: https://ruby.taobao.org/
3
+ specs:
4
+
5
+ PLATFORMS
6
+ ruby
7
+
8
+ DEPENDENCIES
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006-2010, Ben Bleything and Patrick May
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included
12
+ in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
15
+ KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
16
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,158 @@
1
+ = All-purpose Property List manipulation library
2
+
3
+ Plist is a library to manipulate Property List files, also known as plists. It can parse plist files into native Ruby data structures as well as generating new plist files from your Ruby objects.
4
+
5
+ == Usage
6
+
7
+ === Parsing
8
+
9
+ result = Plist::parse_xml('path/to/example.plist')
10
+
11
+ result.class
12
+ => Hash
13
+
14
+ "#{result['FirstName']} #{result['LastName']}"
15
+ => "John Public"
16
+
17
+ result['ZipPostal']
18
+ => "12345"
19
+
20
+ ==== Example Property List
21
+
22
+ <?xml version="1.0" encoding="UTF-8"?>
23
+ <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
24
+ <plist version="1.0">
25
+ <dict>
26
+ <key>FirstName</key>
27
+ <string>John</string>
28
+
29
+ <key>LastName</key>
30
+ <string>Public</string>
31
+
32
+ <key>StreetAddr1</key>
33
+ <string>123 Anywhere St.</string>
34
+
35
+ <key>StateProv</key>
36
+ <string>CA</string>
37
+
38
+ <key>City</key>
39
+ <string>Some Town</string>
40
+
41
+ <key>CountryName</key>
42
+ <string>United States</string>
43
+
44
+ <key>AreaCode</key>
45
+ <string>555</string>
46
+
47
+ <key>LocalPhoneNumber</key>
48
+ <string>5551212</string>
49
+
50
+ <key>ZipPostal</key>
51
+ <string>12345</string>
52
+ </dict>
53
+ </plist>
54
+
55
+ === Generation
56
+
57
+ plist also provides the ability to generate plists from Ruby objects. The following Ruby classes are converted into native plist types:
58
+ Array, Bignum, Date, DateTime, Fixnum, Float, Hash, Integer, String, Symbol, Time, true, false
59
+
60
+ * +Array+ and +Hash+ are both recursive; their elements will be converted into plist nodes inside the <array> and <dict> containers (respectively).
61
+ * +IO+ (and its descendants) and +StringIO+ objects are read from and their contents placed in a <data> element.
62
+ * 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. See below for more details.
63
+
64
+ ==== Creating a plist
65
+
66
+ There are two ways to generate complete plists. Given an object:
67
+
68
+ obj = [1, :two, {'c' => 0xd}]
69
+
70
+ If you've mixed in <tt>Plist::Emit</tt> (which is already done for +Array+ and +Hash+), you can simply call +to_plist+:
71
+
72
+ obj.to_plist
73
+
74
+ This is equivalent to calling <tt>Plist::Emit.dump(obj)</tt>. Either one will yield:
75
+
76
+ <?xml version="1.0" encoding="UTF-8"?>
77
+ <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
78
+ <plist version="1.0">
79
+ <array>
80
+ <integer>1</integer>
81
+ <string>two</string>
82
+ <dict>
83
+ <key>c</key>
84
+ <integer>13</integer>
85
+ </dict>
86
+ </array>
87
+ </plist>
88
+
89
+ You can also dump plist fragments by passing +false+ as the second parameter:
90
+
91
+ Plist::Emit.dump('holy cow!', false)
92
+ => "<string>holy cow!</string>"
93
+
94
+ ==== Custom serialization
95
+
96
+ If your class can be safely coerced into a native plist datatype, you can implement +to_plist_node+. Upon encountering an object of a class it doesn't recognize, the plist library will check to see if it responds to +to_plist_node+, and if so, insert the result of that call into the plist output.
97
+
98
+ An example:
99
+
100
+ class MyFancyString
101
+ ...
102
+
103
+ def to_plist_node
104
+ return "<string>#{self.defancify}</string>"
105
+ end
106
+ end
107
+
108
+ When you attempt to serialize a +MyFancyString+ object, the +to_plist_node+ method will be called and the object's contents will be defancified and placed in the plist.
109
+
110
+ If for whatever reason you can't add this method, your object will be serialized with <tt>Marshal.dump</tt> instead.
111
+
112
+ == Links
113
+
114
+ [Project Page] http://plist.rubyforge.org
115
+ [GitHub] http://github.com/bleything/plist
116
+ [RDoc] http://plist.rubyforge.org
117
+
118
+ == Credits
119
+
120
+ plist is maintained by Ben Bleything <mailto:ben@bleything.net> and Patrick May <mailto:patrick@hexane.org>. Patrick wrote most of the code; Ben is a recent addition to the project, having merged in his plist generation library.
121
+
122
+ Other folks who have helped along the way:
123
+
124
+ [<b>Martin Dittus</b>] who pointed out that +Time+ wasn't enough for plist <tt>Dates</tt>, especially those in <tt>~/Library/Cookies/Cookies.plist</tt>
125
+ [<b>Chuck Remes</b>] who pushed Patrick towards implementing <tt>#to_plist</tt>
126
+ [<b>Mat Schaffer</b>] who supplied code and test cases for <tt><data></tt> elements
127
+ [<b>Michael Granger</b>] for encouragement and help
128
+ [<b>Carsten Bormann, Chris Hoffman, Dana Contreras, Hongli Lai, Johan Sørensen</b>] for contributing Ruby 1.9.x compatibility fixes
129
+
130
+ == License and Copyright
131
+
132
+ plist is released under the MIT License.
133
+
134
+ Portions of the code (notably the Rakefile) contain code pulled and/or adapted from other projects. These files contain a comment at the top describing what was used.
135
+
136
+ === MIT License
137
+
138
+ Copyright (c) 2006-2010, Ben Bleything <ben@bleything.net> and Patrick May <patrick@hexane.org>
139
+
140
+ Permission is hereby granted, free of charge, to any person obtaining
141
+ a copy of this software and associated documentation files (the
142
+ "Software"), to deal in the Software without restriction, including
143
+ without limitation the rights to use, copy, modify, merge, publish,
144
+ distribute, sublicense, and/or sell copies of the Software, and to
145
+ permit persons to whom the Software is furnished to do so, subject to
146
+ the following conditions:
147
+
148
+ The above copyright notice and this permission notice shall be included
149
+ in all copies or substantial portions of the Software.
150
+
151
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
152
+ KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
153
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
154
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
155
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
156
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
157
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
158
+
data/Rakefile ADDED
@@ -0,0 +1,154 @@
1
+ #
2
+ # Plist Rakefile
3
+ #
4
+ # Based heavily on Geoffrey Grosenbach's Rakefile for gruff.
5
+ # Includes whitespace-fixing task based on code from Typo.
6
+ #
7
+ # Copyright 2006-2010 Ben Bleything and Patrick May
8
+ # Distributed under the MIT License
9
+ #
10
+
11
+ require 'fileutils'
12
+ require 'rubygems'
13
+ require 'rake'
14
+ require 'rake/testtask'
15
+ require 'rake/packagetask'
16
+ # require 'rake/gempackagetask'
17
+ require 'rake/contrib/rubyforgepublisher'
18
+ require "bundler/gem_tasks"
19
+
20
+ $:.unshift(File.dirname(__FILE__) + "/lib")
21
+ require 'plist'
22
+
23
+ PKG_NAME = 'plist'
24
+ PKG_VERSION = Plist::VERSION
25
+ PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
26
+
27
+ RELEASE_NAME = "REL #{PKG_VERSION}"
28
+
29
+ RUBYFORGE_PROJECT = "plist"
30
+ RUBYFORGE_USER = ENV['RUBYFORGE_USER']
31
+
32
+ TEST_FILES = Dir.glob('test/test_*')
33
+ TEST_ASSETS = Dir.glob('test/assets/*')
34
+ LIB_FILES = Dir.glob('lib/**/*')
35
+ RELEASE_FILES = [ "Rakefile", "README.rdoc", "CHANGELOG", "LICENSE" ] + LIB_FILES + TEST_FILES + TEST_ASSETS
36
+
37
+ task :default => [ :test ]
38
+ # Run the unit tests
39
+ Rake::TestTask.new { |t|
40
+ t.libs << "test"
41
+ t.test_files = TEST_FILES
42
+ t.verbose = true
43
+ }
44
+
45
+ desc "Clean pkg, coverage, and rdoc; remove .bak files"
46
+ task :clean => [ :clobber_rdoc, :clobber_package, :clobber_coverage ] do
47
+ puts cmd = "find . -type f -name *.bak -delete"
48
+ `#{cmd}`
49
+ end
50
+
51
+ task :clobber_coverage do
52
+ puts cmd = "rm -rf coverage"
53
+ `#{cmd}`
54
+ end
55
+
56
+ desc "Generate coverage analysis with rcov (requires rcov to be installed)"
57
+ task :rcov => [ :clobber_coverage ] do
58
+ puts cmd = "rcov -Ilib --xrefs -T test/*.rb"
59
+ puts `#{cmd}`
60
+ end
61
+
62
+ desc "Strip trailing whitespace and fix newlines for all release files"
63
+ task :fix_whitespace => [ :clean ] do
64
+ RELEASE_FILES.reject {|i| i =~ /assets/}.each do |filename|
65
+ next if File.directory? filename
66
+
67
+ File.open(filename) do |file|
68
+ newfile = ''
69
+ needs_love = false
70
+
71
+ file.readlines.each_with_index do |line, lineno|
72
+ if line =~ /[ \t]+$/
73
+ needs_love = true
74
+ puts "#{filename}: trailing whitespace on line #{lineno}"
75
+ line.gsub!(/[ \t]*$/, '')
76
+ end
77
+
78
+ if line.chomp == line
79
+ needs_love = true
80
+ puts "#{filename}: no newline on line #{lineno}"
81
+ line << "\n"
82
+ end
83
+
84
+ newfile << line
85
+ end
86
+
87
+ if needs_love
88
+ tempname = "#{filename}.new"
89
+
90
+ File.open(tempname, 'w').write(newfile)
91
+ File.chmod(File.stat(filename).mode, tempname)
92
+
93
+ FileUtils.ln filename, "#{filename}.bak"
94
+ FileUtils.ln tempname, filename, :force => true
95
+ File.unlink(tempname)
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ desc "Copy documentation to rubyforge"
102
+ task :update_rdoc => [ :rdoc ] do
103
+ Rake::SshDirPublisher.new("#{RUBYFORGE_USER}@rubyforge.org", "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}", "rdoc").upload
104
+ end
105
+
106
+ begin
107
+ require 'rdoc/task'
108
+
109
+ # Generate the RDoc documentation
110
+ RDoc::Task.new do |rdoc|
111
+ rdoc.title = "All-purpose Property List manipulation library"
112
+ rdoc.main = "README.rdoc"
113
+
114
+ rdoc.rdoc_dir = 'rdoc'
115
+ rdoc.rdoc_files.include('README.rdoc', 'LICENSE', 'CHANGELOG')
116
+ rdoc.rdoc_files.include('lib/**')
117
+
118
+ rdoc.options = [
119
+ '-H', # show hash marks on method names in comments
120
+ '-N', # show line numbers
121
+ ]
122
+ end
123
+ rescue LoadError
124
+ $stderr.puts "Could not load rdoc tasks"
125
+ end
126
+
127
+ # Create compressed packages
128
+ spec = Gem::Specification.new do |s|
129
+ s.name = PKG_NAME
130
+ s.version = PKG_VERSION
131
+
132
+ s.summary = "All-purpose Property List manipulation library."
133
+ s.description = <<-EOD
134
+ Plist is a library to manipulate Property List files, also known as plists. It can parse plist files into native Ruby data structures as well as generating new plist files from your Ruby objects.
135
+ EOD
136
+
137
+ s.authors = "Ben Bleything and Patrick May"
138
+ s.homepage = "http://plist.rubyforge.org"
139
+
140
+ s.rubyforge_project = RUBYFORGE_PROJECT
141
+
142
+ s.has_rdoc = true
143
+
144
+ s.files = RELEASE_FILES
145
+ s.test_files = TEST_FILES
146
+
147
+ s.autorequire = 'plist'
148
+ end
149
+
150
+ # Rake::GemPackageTask.new(spec) do |p|
151
+ # p.gem_spec = spec
152
+ # p.need_tar = true
153
+ # p.need_zip = true
154
+ # end
@@ -0,0 +1,222 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # = plist
4
+ #
5
+ # Copyright 2006-2010 Ben Bleything and Patrick May
6
+ # Distributed under the MIT License
7
+ #
8
+
9
+ module Plist ; end
10
+
11
+ # === Create a plist
12
+ # You can dump an object to a plist in one of two ways:
13
+ #
14
+ # * <tt>Plist::Emit.dump(obj)</tt>
15
+ # * <tt>obj.to_plist</tt>
16
+ # * This requires that you mixin the <tt>Plist::Emit</tt> module, which is already done for +Array+ and +Hash+.
17
+ #
18
+ # The following Ruby classes are converted into native plist types:
19
+ # Array, Bignum, Date, DateTime, Fixnum, Float, Hash, Integer, String, Symbol, Time, true, false
20
+ # * +Array+ and +Hash+ are both recursive; their elements will be converted into plist nodes inside the <array> and <dict> containers (respectively).
21
+ # * +IO+ (and its descendants) and +StringIO+ objects are read from and their contents placed in a <data> element.
22
+ # * 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.
23
+ #
24
+ # For detailed usage instructions, refer to USAGE[link:files/docs/USAGE.html] and the methods documented below.
25
+ module Plist::Emit
26
+ # Helper method for injecting into classes. Calls <tt>Plist::Emit.dump</tt> with +self+.
27
+ def to_plist(envelope = true)
28
+ return Plist::Emit.dump(self, envelope)
29
+ end
30
+
31
+ # Helper method for injecting into classes. Calls <tt>Plist::Emit.save_plist</tt> with +self+.
32
+ def save_plist(filename)
33
+ Plist::Emit.save_plist(self, filename)
34
+ end
35
+
36
+ # The following Ruby classes are converted into native plist types:
37
+ # Array, Bignum, Date, DateTime, Fixnum, Float, Hash, Integer, String, Symbol, Time
38
+ #
39
+ # Write us (via RubyForge) if you think another class can be coerced safely into one of the expected plist classes.
40
+ #
41
+ # +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+.
42
+ #
43
+ # 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.
44
+ def self.dump(obj, envelope = true)
45
+ output = plist_node(obj)
46
+
47
+ output = wrap(output) if envelope
48
+
49
+ return output
50
+ end
51
+
52
+ # Writes the serialized object's plist to the specified filename.
53
+ def self.save_plist(obj, filename)
54
+ File.open(filename, 'wb') do |f|
55
+ f.write(obj.to_plist)
56
+ end
57
+ end
58
+
59
+ private
60
+ def self.plist_node(element)
61
+ output = ''
62
+
63
+ if element.respond_to? :to_plist_node
64
+ output << element.to_plist_node
65
+ else
66
+ case element
67
+ when Array
68
+ if element.empty?
69
+ output << "<array/>\n"
70
+ else
71
+ output << tag('array') {
72
+ element.collect {|e| plist_node(e)}
73
+ }
74
+ end
75
+ when Hash
76
+ if element.empty?
77
+ output << "<dict/>\n"
78
+ else
79
+ inner_tags = []
80
+
81
+ element.keys.sort_by{|k| k.to_s }.each do |k|
82
+ v = element[k]
83
+ inner_tags << tag('key', CGI::escapeHTML(k.to_s))
84
+ inner_tags << plist_node(v)
85
+ end
86
+
87
+ output << tag('dict') {
88
+ inner_tags
89
+ }
90
+ end
91
+ when true, false
92
+ output << "<#{element}/>\n"
93
+ when Time
94
+ output << tag('date', element.utc.strftime('%Y-%m-%dT%H:%M:%SZ'))
95
+ when Date # also catches DateTime
96
+ output << tag('date', element.strftime('%Y-%m-%dT%H:%M:%SZ'))
97
+ when String, Symbol, Fixnum, Bignum, Integer, Float
98
+ output << tag(element_type(element), CGI::escapeHTML(element.to_s))
99
+ when IO, StringIO
100
+ element.rewind
101
+ contents = element.read
102
+ # note that apple plists are wrapped at a different length then
103
+ # what ruby's base64 wraps by default.
104
+ # I used #encode64 instead of #b64encode (which allows a length arg)
105
+ # because b64encode is b0rked and ignores the length arg.
106
+ data = "\n"
107
+ Base64::encode64(contents).gsub(/\s+/, '').scan(/.{1,68}/o) { data << $& << "\n" }
108
+ output << tag('data', data)
109
+ else
110
+ output << comment( 'The <data> element below contains a Ruby object which has been serialized with Marshal.dump.' )
111
+ data = "\n"
112
+ Base64::encode64(Marshal.dump(element)).gsub(/\s+/, '').scan(/.{1,68}/o) { data << $& << "\n" }
113
+ output << tag('data', data )
114
+ end
115
+ end
116
+
117
+ return output
118
+ end
119
+
120
+ def self.comment(content)
121
+ return "<!-- #{content} -->\n"
122
+ end
123
+
124
+ def self.tag(type, contents = '', &block)
125
+ out = nil
126
+
127
+ if block_given?
128
+ out = IndentedString.new
129
+ out << "<#{type}>"
130
+ out.raise_indent
131
+
132
+ out << block.call
133
+
134
+ out.lower_indent
135
+ out << "</#{type}>"
136
+ else
137
+ out = "<#{type}>#{contents.to_s}</#{type}>\n"
138
+ end
139
+
140
+ return out.to_s
141
+ end
142
+
143
+ def self.wrap(contents)
144
+ output = ''
145
+
146
+ output << '<?xml version="1.0" encoding="UTF-8"?>' + "\n"
147
+ output << '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' + "\n"
148
+ output << '<plist version="1.0">' + "\n"
149
+
150
+ output << contents
151
+
152
+ output << '</plist>' + "\n"
153
+
154
+ return output
155
+ end
156
+
157
+ def self.element_type(item)
158
+ case item
159
+ when String, Symbol
160
+ 'string'
161
+
162
+ when Fixnum, Bignum, Integer
163
+ 'integer'
164
+
165
+ when Float
166
+ 'real'
167
+
168
+ else
169
+ raise "Don't know about this data type... something must be wrong!"
170
+ end
171
+ end
172
+ private
173
+ class IndentedString #:nodoc:
174
+ attr_accessor :indent_string
175
+
176
+ def initialize(str = "\t")
177
+ @indent_string = str
178
+ @contents = ''
179
+ @indent_level = 0
180
+ end
181
+
182
+ def to_s
183
+ return @contents
184
+ end
185
+
186
+ def raise_indent
187
+ @indent_level += 1
188
+ end
189
+
190
+ def lower_indent
191
+ @indent_level -= 1 if @indent_level > 0
192
+ end
193
+
194
+ def <<(val)
195
+ if val.is_a? Array
196
+ val.each do |f|
197
+ self << f
198
+ end
199
+ else
200
+ # if it's already indented, don't bother indenting further
201
+ unless val =~ /\A#{@indent_string}/
202
+ indent = @indent_string * @indent_level
203
+
204
+ @contents << val.gsub(/^/, indent)
205
+ else
206
+ @contents << val
207
+ end
208
+
209
+ # it already has a newline, don't add another
210
+ @contents << "\n" unless val =~ /\n$/
211
+ end
212
+ end
213
+ end
214
+ end
215
+
216
+ class Array #:nodoc:
217
+ include Plist::Emit
218
+ end
219
+
220
+ class Hash #:nodoc:
221
+ include Plist::Emit
222
+ end