Class: CFPropertyList::LibXMLParser

Inherits:
XMLParserInterface show all
Defined in:
lib/cfpropertylist/rbLibXMLParser.rb

Overview

XML parser

Instance Method Summary collapse

Instance Method Details

#append_node(parent, child) ⇒ Object



65
66
67
# File 'lib/cfpropertylist/rbLibXMLParser.rb', line 65

def append_node(parent, child)
  parent << child
end

#load(opts) ⇒ Object

read a XML file

opts
  • :file - The filename of the file to load

  • :data - The data to parse



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cfpropertylist/rbLibXMLParser.rb', line 12

def load(opts)
  doc = nil

  if(opts.has_key?(:file)) then
    doc = LibXML::XML::Document.file(opts[:file],:options => LibXML::XML::Parser::Options::NOBLANKS|LibXML::XML::Parser::Options::NOENT)
  else
    doc = LibXML::XML::Document.string(opts[:data],:options => LibXML::XML::Parser::Options::NOBLANKS|LibXML::XML::Parser::Options::NOENT)
  end

  if doc
    root = doc.root.first
    return import_xml(root)
  end
rescue LibXML::XML::Error => e
  raise CFFormatError.new('invalid XML: ' + e.message)
end

#new_node(name) ⇒ Object



57
58
59
# File 'lib/cfpropertylist/rbLibXMLParser.rb', line 57

def new_node(name)
  LibXML::XML::Node.new(name)
end

#new_text(val) ⇒ Object



61
62
63
# File 'lib/cfpropertylist/rbLibXMLParser.rb', line 61

def new_text(val)
  LibXML::XML::Node.new_text(val)
end

#to_str(opts = {}) ⇒ Object

serialize CFPropertyList object to XML

opts = {}

Specify options: :formatted - Use indention and line breaks



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cfpropertylist/rbLibXMLParser.rb', line 31

def to_str(opts={})
  doc = LibXML::XML::Document.new

  doc.root = LibXML::XML::Node.new('plist')
  doc.encoding = LibXML::XML::Encoding::UTF_8

  doc.root['version'] = '1.0'
  doc.root << opts[:root].to_xml(self)

  # ugly hack, but there's no other possibility I know
  str = doc.to_s(:indent => opts[:formatted])
  str1 = String.new
  first = false
  str.each_line do |line|
    str1 << line
    unless(first) then
      str1 << "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" if line =~ /^\s*<\?xml/
    end

    first = true
  end

  str1.force_encoding('UTF-8') if str1.respond_to?(:force_encoding)
  return str1
end