class CFPropertyList::List

Class representing a CFPropertyList. Instanciate with new

Constants

FORMAT_AUTO

Format constant for automatic format recognizing

FORMAT_BINARY

Format constant for binary format

FORMAT_PLAIN

Format constant for the old plain format

FORMAT_XML

Format constant for XML format

Attributes

filename[RW]

Path of PropertyList

format[RW]

the original format of the PropertyList

formatted[RW]

default value for XML generation; if true generate formatted XML

value[RW]

the root value in the plist file

Public Class Methods

new(opts={}) click to toggle source

initialize a new CFPropertyList, arguments are:

:file

Parse a file

:format

Format is one of FORMAT_BINARY or FORMAT_XML. Defaults to FORMAT_AUTO

:data

Parse a string

All arguments are optional

# File lib/cfpropertylist/rbCFPropertyList.rb, line 231
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
parsers() click to toggle source

returns a list of registered parsers

# File lib/cfpropertylist/rbCFPropertyList.rb, line 242
def self.parsers
  @@parsers
end
parsers=(val) click to toggle source

set a list of parsers

# File lib/cfpropertylist/rbCFPropertyList.rb, line 247
def self.parsers=(val)
  @@parsers = val
end

Public Instance Methods

load(file=nil,format=nil) click to toggle source

Read a plist file

file = nil

The filename of the file to read. If nil, use filename instance variable

format = nil

The format of the plist file. Auto-detect if nil

# File lib/cfpropertylist/rbCFPropertyList.rb, line 327
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
load_binary(filename=nil) click to toggle source

read a binary plist file

filename = nil

The filename to read from; if nil, read from the file defined by instance variable filename

# File lib/cfpropertylist/rbCFPropertyList.rb, line 259
def load_binary(filename=nil)
  load(filename,List::FORMAT_BINARY)
end
load_binary_str(str=nil) click to toggle source

load a plist from a binary string

str

The string containing the plist

# File lib/cfpropertylist/rbCFPropertyList.rb, line 277
def load_binary_str(str=nil)
  load_str(str,List::FORMAT_BINARY)
end
load_plain(filename=nil) click to toggle source

read a plain plist file

filename = nil

The filename to read from; if nil, read from the file defined by instance variable filename

# File lib/cfpropertylist/rbCFPropertyList.rb, line 265
def load_plain(filename=nil)
  load(filename,List::FORMAT_PLAIN)
end
load_plain_str(str=nil) click to toggle source

load a plist from a plain string

str

The string containing the plist

# File lib/cfpropertylist/rbCFPropertyList.rb, line 283
def load_plain_str(str=nil)
  load_str(str,List::FORMAT_PLAIN)
end
load_str(str=nil,format=nil) click to toggle source

load a plist from a string

str = nil

The string containing the plist

format = nil

The format of the plist

# File lib/cfpropertylist/rbCFPropertyList.rb, line 290
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
load_xml(filename=nil) click to toggle source

Load an XML PropertyList

filename = nil

The filename to read from; if nil, read from the file defined by instance variable filename

# File lib/cfpropertylist/rbCFPropertyList.rb, line 253
def load_xml(filename=nil)
  load(filename,List::FORMAT_XML)
end
load_xml_str(str=nil) click to toggle source

load a plist from a XML string

str

The string containing the plist

# File lib/cfpropertylist/rbCFPropertyList.rb, line 271
def load_xml_str(str=nil)
  load_str(str,List::FORMAT_XML)
end
save(file=nil,format=nil,opts={}) click to toggle source

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

# File lib/cfpropertylist/rbCFPropertyList.rb, line 369
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
to_str(format=List::FORMAT_BINARY,opts={}) click to toggle source

convert plist to string

format = List::FORMAT_BINARY

The format to save the plist

opts={}

Pass parser options

# File lib/cfpropertylist/rbCFPropertyList.rb, line 399
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