summaryrefslogtreecommitdiff
path: root/README.rdoc
blob: 2fd54ee90d54d5f06aef60be9902b391815fba35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
= All-purpose Property List manipulation library

{rdoc-image:https://badge.fury.io/rb/plist.svg}[https://rubygems.org/gems/plist]
{rdoc-image:https://github.com/patsplat/plist/actions/workflows/ci.yml/badge.svg}[https://github.com/patsplat/plist/actions/workflows/ci.yml]

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.

== Usage

=== Security considerations

By default, Plist.parse_xml uses Marshal.load for <data/> attributes. If the <data/> attribute contains malicious data, an attacker can gain code execution.

You should never use the default Plist.parse_xml with untrusted plists!

To disable the Marshal.load behavior, use <tt>marshal: false</tt>. This will return the raw binary <data> contents as an IO object instead of attempting to unmarshal it.

=== Parsing

  result = Plist.parse_xml('path/to/example.plist')
  # or
  result = Plist.parse_xml('path/to/example.plist', marshal: false)

  result.class
  => Hash

  "#{result['FirstName']} #{result['LastName']}"
  => "John Public"

  result['ZipPostal']
  => "12345"

==== Example Property List

  <?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">
  <dict>
          <key>FirstName</key>
          <string>John</string>

          <key>LastName</key>
          <string>Public</string>

          <key>StreetAddr1</key>
          <string>123 Anywhere St.</string>

          <key>StateProv</key>
          <string>CA</string>

          <key>City</key>
          <string>Some Town</string>

          <key>CountryName</key>
          <string>United States</string>

          <key>AreaCode</key>
          <string>555</string>

          <key>LocalPhoneNumber</key>
          <string>5551212</string>

          <key>ZipPostal</key>
          <string>12345</string>
  </dict>
  </plist>

=== Generation

plist also provides the ability to generate plists from Ruby objects.  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.  See below for more details.

==== Creating a plist

There are two ways to generate complete plists.  Given an object:

  obj = [1, :two, {'c' => 0xd}]

If you've mixed in <tt>Plist::Emit</tt> (which is already done for +Array+ and +Hash+), you can simply call +to_plist+:

  obj.to_plist

This is equivalent to calling <tt>Plist::Emit.dump(obj)</tt>.  Either one will yield:

  <?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">
  <array>
      <integer>1</integer>
      <string>two</string>
      <dict>
        <key>c</key>
        <integer>13</integer>
      </dict>
  </array>
  </plist>

You can also dump plist fragments by passing +false+ as the second parameter:

  Plist::Emit.dump('holy cow!', false)
  => "<string>holy cow!</string>"

==== Custom serialization

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.

An example:

  class MyFancyString
    ...

    def to_plist_node
      return "<string>#{self.defancify}</string>"
    end
  end

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.

If for whatever reason you can't add this method, your object will be serialized with <tt>Marshal.dump</tt> instead.

==== Custom indent

You can customize the default indent foramt (default format is tab) or specify the indent format on each serialization. For example, if you want to reduce size of plist output, you can set the indent to <tt>nil</tt>.

An example to change default indent format:

  Plist::Emit::DEFAULT_INDENT = nil

An example to specify indent format on dump:

  Plist::Emit.dump({:foo => :bar}, false)
  => "<dict>\n\t<key>foo</key>\n\t<string>bar</string>\n</dict>\n"

  Plist::Emit.dump({:foo => :bar}, false, :indent => nil)
  => "<dict>\n<key>foo</key>\n<string>bar</string>\n</dict>\n"


== Links

[Rubygems]      https://rubygems.org/gems/plist
[GitHub]        https://github.com/bleything/plist
[RDoc]          http://www.rubydoc.info/gems/plist

== Credits

plist was authored by Ben Bleything <mailto:ben@bleything.net> and Patrick May <mailto:patrick@hexane.org>. Patrick wrote most of the code; Ben contributed his plist generation library. The project is currently maintained by @mattbrictson[https://github.com/mattbrictson].

Other folks who have helped along the way:

[<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>
[<b>Chuck Remes</b>] who pushed Patrick towards implementing <tt>#to_plist</tt>
[<b>Mat Schaffer</b>] who supplied code and test cases for <tt><data></tt> elements
[<b>Michael Granger</b>] for encouragement and help
[<b>Carsten Bormann, Chris Hoffman, Dana Contreras, Hongli Lai, Johan Sørensen</b>] for contributing Ruby 1.9.x compatibility fixes
And thank you to all of the other GitHub contributors[https://github.com/patsplat/plist/graphs/contributors] not mentioned here!

== License and Copyright

plist is released under the MIT License.

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.

=== MIT License

Copyright (c) 2006-2010, Ben Bleything <ben@bleything.net> and Patrick May <patrick@hexane.org>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.