class CFPropertyList::List
Class representing a CFPropertyList. Instanciate with #new
def self.parsers
def self.parsers @@parsers end
def self.parsers=(val)
def self.parsers=(val) @@parsers = val end
def initialize(opts={})
:data:: Parse a string
:format:: Format is one of FORMAT_BINARY or FORMAT_XML. Defaults to FORMAT_AUTO
:file:: Parse a file
initialize a new CFPropertyList, arguments are:
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
def load(file=nil,format=nil)
file = nil:: The filename of the file to read. If nil, use +filename+ instance variable
Read a plist file
def load(file=nil,format=nil) file = @filename if file.nil? format = @format if format.nil? @value = {} raise IOError.new("File #{file} not readable!") unless File.readable? file case format when List::FORMAT_BINARY, List::FORMAT_XML, List::FORMAT_PLAIN then prsr = @@parsers[format-1].new @value = prsr.load({:file => file}) when List::FORMAT_AUTO then # what we now do is ugly, but neccessary to recognize the file format magic_number = IO.read(file,12) raise IOError.new("File #{file} is empty.") unless magic_number filetype = magic_number[0..5] version = magic_number[6..7] prsr = nil if filetype == "bplist" then raise CFFormatError.new("Wrong file version #{version}") unless version == "00" prsr = Binary.new @format = List::FORMAT_BINARY else if magic_number =~ /^<(\?xml|!DOCTYPE|plist)/ prsr = CFPropertyList.xml_parser_interface.new @format = List::FORMAT_XML else prsr = PlainParser.new @format = List::FORMAT_PLAIN end end @value = prsr.load({:file => file}) end raise CFFormatError.new("Invalid format or parser error!") if @value.nil? end
def load_binary(filename=nil)
read a binary plist file
def load_binary(filename=nil) load(filename,List::FORMAT_BINARY) end
def load_binary_str(str=nil)
load a plist from a binary string
def load_binary_str(str=nil) load_str(str,List::FORMAT_BINARY) end
def load_plain(filename=nil)
read a plain plist file
def load_plain(filename=nil) load(filename,List::FORMAT_PLAIN) end
def load_plain_str(str=nil)
load a plist from a plain string
def load_plain_str(str=nil) load_str(str,List::FORMAT_PLAIN) end
def load_str(str=nil,format=nil)
str = nil:: The string containing the plist
load a plist from a string
def load_str(str=nil,format=nil) str = @data if str.nil? format = @format if format.nil? @value = {} case format when List::FORMAT_BINARY, List::FORMAT_XML, List::FORMAT_PLAIN then prsr = @@parsers[format-1].new @value = prsr.load({:data => str}) when List::FORMAT_AUTO then # what we now do is ugly, but neccessary to recognize the file format filetype = str[0..5] version = str[6..7] prsr = nil if filetype == "bplist" then raise CFFormatError.new("Wrong file version #{version}") unless version == "00" prsr = Binary.new @format = List::FORMAT_BINARY else if str =~ /^<(\?xml|!DOCTYPE|plist)/ prsr = CFPropertyList.xml_parser_interface.new @format = List::FORMAT_XML else prsr = PlainParser.new @format = List::FORMAT_PLAIN end end @value = prsr.load({:data => str}) end end
def load_xml(filename=nil)
Load an XML PropertyList
def load_xml(filename=nil) load(filename,List::FORMAT_XML) end
def load_xml_str(str=nil)
load a plist from a XML string
def load_xml_str(str=nil) load_str(str,List::FORMAT_XML) end
def save(file=nil,format=nil,opts={})
file = nil:: The filename of the file to write to. Uses +filename+ instance variable if nil
Serialize CFPropertyList object to specified format and write it to file
def save(file=nil,format=nil,opts={}) format = @format if format.nil? file = @filename if file.nil? if format != FORMAT_BINARY && format != FORMAT_XML && format != FORMAT_PLAIN raise CFFormatError.new("Format #{format} not supported, use List::FORMAT_BINARY or List::FORMAT_XML") end if(!File.exists?(file)) then raise IOError.new("File #{file} not writable!") unless File.writable?(File.dirname(file)) 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 } end
def to_str(format=List::FORMAT_BINARY,opts={})
format = List::FORMAT_BINARY:: The format to save the plist
convert plist to string
def to_str(format=List::FORMAT_BINARY,opts={}) if format != FORMAT_BINARY && format != FORMAT_XML && format != FORMAT_PLAIN raise CFFormatError.new("Format #{format} not supported, use List::FORMAT_BINARY or List::FORMAT_XML") end prsr = @@parsers[format-1].new opts[:root] = @value opts[:formatted] = @formatted unless opts.has_key?(:formatted) return prsr.to_str(opts) end