lib/plist4r/backend/libxml4r.rb in plist4r-0.2.2 vs lib/plist4r/backend/libxml4r.rb in plist4r-1.0.0
- old
+ new
@@ -1,9 +1,13 @@
require 'plist4r/backend_base'
+require 'libxml4r'
+require 'base64'
+require 'date'
-# Requires Libxml4r. Implements loading / parsing for the :xml file format only.
+# This backend uses Libxml4r / Libxml-Ruby to parse xml plists
+# @author Dreamcat4 (dreamcat4@gmail.com)
module Plist4r::Backend::Libxml4r
class << self
def tree_hash n
hash = ::Plist4r::OrderedHash.new
n_xml_keys = n.nodes["key"]
@@ -15,16 +19,22 @@
hash[k] = eval(vnode.name)
when "string"
hash[k] = vnode.inner_xml
when "integer"
hash[k] = vnode.inner_xml.to_i
+ when "real"
+ hash[k] = vnode.inner_xml.to_f
+ when "date"
+ hash[k] = Time.parse vnode.inner_xml
+ when "data"
+ bstr = Base64.decode64(vnode.inner_xml)
+ bstr.blob = true
+ hash[k] = bstr
when "array"
hash[k] = tree_array(vnode)
when "dict"
hash[k] = tree_hash(vnode)
- else
- raise "Unsupported / not recognized plist key: #{vnode.name}"
end
end
return hash
end
@@ -36,24 +46,28 @@
array << eval(node.name)
when "string"
array << node.inner_xml
when "integer"
array << node.inner_xml.to_i
+ when "real"
+ array << node.inner_xml.to_f
+ when "date"
+ array << Time.parse(node.inner_xml)
+ when "data"
+ bstr = bstr = Base64.decode64(node.inner_xml)
+ bstr.blob = true
+ array << bstr
when "array"
array << tree_array(node)
when "dict"
array << tree_hash(node)
- else
- raise "Unsupported / not recognized plist key: #{vnode.name}"
end
end
return array
end
def parse_plist_xml string
- require 'rubygems'
- require 'libxml4r'
::LibXML::XML.default_keep_blanks = false
doc = string.to_xmldoc
doc.strip!
root = doc.node["/plist/dict"]
@@ -68,24 +82,14 @@
end
end
ordered_hash
end
- def from_string plist, string
- plist_format = Plist4r.string_detect_format string
- raise "#{self} - cant convert string of format #{plist_format}" unless plist_format == :xml
- hash = parse_plist_xml string
+ def from_xml plist
+ hash = parse_plist_xml plist.from_string
plist.import_hash hash
- plist.file_format plist_format
+ plist.file_format "xml"
return plist
- end
-
- def open plist
- filename = plist.filename_path
- file_format = Plist4r.file_detect_format filename
- raise "#{self} - cant load file of format #{file_format}" unless file_format == :xml
-
- return from_string plist, File.read(filename)
end
end
end