lib/rbCFPropertyList.rb in CFPropertyList-2.2.0 vs lib/rbCFPropertyList.rb in CFPropertyList-2.2.1
- old
+ new
@@ -220,14 +220,16 @@
@@parsers = [Binary, CFPropertyList.xml_parser_interface]
# Path of PropertyList
attr_accessor :filename
- # Path of PropertyList
+ # the original format of the PropertyList
attr_accessor :format
# the root value in the plist file
attr_accessor :value
+ # default value for XML generation; if true generate formatted XML
+ attr_accessor :formatted
# initialize a new CFPropertyList, arguments are:
#
# :file:: Parse a file
# :format:: Format is one of FORMAT_BINARY or FORMAT_XML. Defaults to FORMAT_AUTO
@@ -236,15 +238,26 @@
# All arguments are optional
def initialize(opts={})
@filename = opts[:file]
@format = opts[:format] || FORMAT_AUTO
@data = opts[:data]
+ @formatted = opts[:formatted]
load(@filename) unless @filename.nil?
load_str(@data) unless @data.nil?
end
+ # returns a list of registered parsers
+ def self.parsers
+ @@parsers
+ end
+
+ # set a list of parsers
+ def self.parsers=(val)
+ @@parsers = val
+ end
+
# Load an XML PropertyList
# filename = nil:: The filename to read from; if nil, read from the file defined by instance variable +filename+
def load_xml(filename=nil)
load(filename,List::FORMAT_XML)
end
@@ -286,12 +299,14 @@
prsr = nil
if filetype == "bplist" then
raise CFFormatError.new("Wrong file version #{version}") unless version == "00"
prsr = Binary.new
+ @format = List::FORMAT_BINARY
else
prsr = CFPropertyList.xml_parser_interface.new
+ @format = List::FORMAT_XML
end
@value = prsr.load({:data => str})
end
end
@@ -319,16 +334,20 @@
prsr = nil
if filetype == "bplist" then
raise CFFormatError.new("Wong file version #{version}") unless version == "00"
prsr = Binary.new
+ @format = List::FORMAT_BINARY
else
prsr = CFPropertyList.xml_parser_interface.new
+ @format = List::FORMAT_XML
end
@value = prsr.load({:file => file})
end
+
+ raise CFFormatError.new("Invalid format or parser error!") if @value.nil?
end
# Serialize CFPropertyList object to specified format and write it to file
# file = nil:: The filename of the file to write to. Uses +filename+ instance variable if nil
# format = nil:: The format to save in. Uses +format+ instance variable if nil
@@ -343,10 +362,13 @@
elsif(!File.writable?(file)) then
raise IOError.new("File #{file} not writable!")
end
opts[:root] = @value
+ opts[:formatted] = @formatted unless opts.has_key?(:formatted)
+
prsr = @@parsers[format-1].new
+
content = prsr.to_str(opts)
File.open(file, 'wb') {
|fd|
fd.write content