Class: CFPropertyList::List
- Inherits:
-
Object
- Object
- CFPropertyList::List
- Defined in:
- lib/cfpropertylist/rbCFPropertyList.rb
Overview
Class representing a CFPropertyList. Instanciate with #new
Constant Summary collapse
- FORMAT_BINARY =
Format constant for binary format
1
- FORMAT_XML =
Format constant for XML format
2
- FORMAT_PLAIN =
Format constant for the old plain format
3
- FORMAT_AUTO =
Format constant for automatic format recognizing
0
- @@parsers =
[Binary, CFPropertyList.xml_parser_interface, PlainParser]
Instance Attribute Summary collapse
-
#filename ⇒ Object
Path of PropertyList.
-
#format ⇒ Object
the original format of the PropertyList.
-
#formatted ⇒ Object
default value for XML generation; if true generate formatted XML.
-
#value ⇒ Object
the root value in the plist file.
Class Method Summary collapse
-
.parsers ⇒ Object
returns a list of registered parsers.
-
.parsers=(val) ⇒ Object
set a list of parsers.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ List
constructor
initialize a new CFPropertyList, arguments are:.
-
#load(file = nil, format = nil) ⇒ Object
- Read a plist file file = nil
-
The filename of the file to read.
-
#load_binary(filename = nil) ⇒ Object
- read a binary plist file filename = nil
-
The filename to read from; if nil, read from the file defined by instance variable
filename
.
-
#load_binary_str(str = nil) ⇒ Object
- load a plist from a binary string str
-
The string containing the plist.
-
#load_plain(filename = nil) ⇒ Object
- read a plain plist file filename = nil
-
The filename to read from; if nil, read from the file defined by instance variable
filename
.
-
#load_plain_str(str = nil) ⇒ Object
- load a plist from a plain string str
-
The string containing the plist.
-
#load_str(str = nil, format = nil) ⇒ Object
- load a plist from a string str = nil
- The string containing the plist format = nil
-
The format of the plist.
-
#load_xml(filename = nil) ⇒ Object
- Load an XML PropertyList filename = nil
-
The filename to read from; if nil, read from the file defined by instance variable
filename
.
-
#load_xml_str(str = nil) ⇒ Object
- load a plist from a XML string str
-
The string containing the plist.
-
#save(file = nil, format = nil, opts = {}) ⇒ Object
- Serialize CFPropertyList object to specified format and write it to file file = nil
-
The filename of the file to write to.
-
#to_str(format = List::FORMAT_BINARY, opts = {}) ⇒ Object
- convert plist to string format = List::FORMAT_BINARY
- The format to save the plist opts={}
-
Pass parser options.
Constructor Details
#initialize(opts = {}) ⇒ List
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
248 249 250 251 252 253 254 255 256 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 248 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 |
Instance Attribute Details
#filename ⇒ Object
Path of PropertyList
233 234 235 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 233 def filename @filename end |
#format ⇒ Object
the original format of the PropertyList
235 236 237 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 235 def format @format end |
#formatted ⇒ Object
default value for XML generation; if true generate formatted XML
239 240 241 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 239 def formatted @formatted end |
#value ⇒ Object
the root value in the plist file
237 238 239 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 237 def value @value end |
Class Method Details
.parsers ⇒ Object
returns a list of registered parsers
259 260 261 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 259 def self.parsers @@parsers end |
.parsers=(val) ⇒ Object
set a list of parsers
264 265 266 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 264 def self.parsers=(val) @@parsers = val end |
Instance Method Details
#load(file = nil, format = nil) ⇒ Object
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
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 344 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) ⇒ Object
read a binary plist file
- filename = nil
-
The filename to read from; if nil, read from the file defined by instance variable
filename
276 277 278 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 276 def load_binary(filename=nil) load(filename,List::FORMAT_BINARY) end |
#load_binary_str(str = nil) ⇒ Object
load a plist from a binary string
- str
-
The string containing the plist
294 295 296 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 294 def load_binary_str(str=nil) load_str(str,List::FORMAT_BINARY) end |
#load_plain(filename = nil) ⇒ Object
read a plain plist file
- filename = nil
-
The filename to read from; if nil, read from the file defined by instance variable
filename
282 283 284 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 282 def load_plain(filename=nil) load(filename,List::FORMAT_PLAIN) end |
#load_plain_str(str = nil) ⇒ Object
load a plist from a plain string
- str
-
The string containing the plist
300 301 302 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 300 def load_plain_str(str=nil) load_str(str,List::FORMAT_PLAIN) end |
#load_str(str = nil, format = nil) ⇒ Object
load a plist from a string
- str = nil
-
The string containing the plist
- format = nil
-
The format of the plist
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 307 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) ⇒ Object
Load an XML PropertyList
- filename = nil
-
The filename to read from; if nil, read from the file defined by instance variable
filename
270 271 272 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 270 def load_xml(filename=nil) load(filename,List::FORMAT_XML) end |
#load_xml_str(str = nil) ⇒ Object
load a plist from a XML string
- str
-
The string containing the plist
288 289 290 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 288 def load_xml_str(str=nil) load_str(str,List::FORMAT_XML) end |
#save(file = nil, format = nil, opts = {}) ⇒ Object
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
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 386 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 = {}) ⇒ Object
convert plist to string
- format = List::FORMAT_BINARY
-
The format to save the plist
- opts={}
-
Pass parser options
416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 416 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 |