lib/plist/parser.rb in plist-3.6.0 vs lib/plist/parser.rb in plist-3.7.0
- old
+ new
@@ -24,12 +24,17 @@
# Plist.parse_xml will blow up if it encounters a Date element.
# If you encounter such an error, or if you have a Date element which
# can't be parsed into a Time object, please create an issue
# attaching your plist file at https://github.com/patsplat/plist/issues
# so folks can implement the proper support.
- def self.parse_xml(filename_or_xml)
- listener = Listener.new
+ #
+ # By default, <data> will be assumed to be a marshaled Ruby object and
+ # interpreted with <tt>Marshal.load</tt>. Pass <tt>marshal: false</tt>
+ # to disable this behavior and return the raw binary data as an IO
+ # object instead.
+ def self.parse_xml(filename_or_xml, options={})
+ listener = Listener.new(options)
# parser = REXML::Parsers::StreamParser.new(File.new(filename), listener)
parser = StreamParser.new(filename_or_xml, listener)
parser.parse
listener.result
end
@@ -37,17 +42,18 @@
class Listener
# include REXML::StreamListener
attr_accessor :result, :open
- def initialize
+ def initialize(options={})
@result = nil
@open = []
+ @options = { :marshal => true }.merge(options).freeze
end
def tag_start(name, attributes)
- @open.push PTag.mappings[name].new
+ @open.push PTag.mappings[name].new(@options)
end
def text(contents)
if @open.last
@open.last.text ||= ''
@@ -152,13 +158,14 @@
key.gsub!(/^p/, '') unless key == "plist"
mappings[key] = sub_class
end
- attr_accessor :text, :children
- def initialize
+ attr_accessor :text, :children, :options
+ def initialize(options)
@children = []
+ @options = options
end
def to_ruby
raise "Unimplemented: " + self.class.to_s + "#to_ruby on #{self.inspect}"
end
@@ -242,15 +249,15 @@
require 'base64'
class PData < PTag
def to_ruby
data = Base64.decode64(text.gsub(/\s+/, '')) unless text.nil?
begin
- return Marshal.load(data)
+ return Marshal.load(data) if options[:marshal]
rescue Exception
- io = StringIO.new
- io.write data
- io.rewind
- return io
end
+ io = StringIO.new
+ io.write data
+ io.rewind
+ io
end
end
end