Class: CFPropertyList::LibXMLParser

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

Overview

XML parser

Constant Summary collapse

PARSER_OPTIONS =
LibXML::XML::Parser::Options::NOBLANKS|LibXML::XML::Parser::Options::NONET

Instance Method Summary collapse

Instance Method Details

#append_node(parent, child) ⇒ Object



67
68
69
# File 'lib/cfpropertylist/rbLibXMLParser.rb', line 67

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



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

def load(opts)
  doc = nil

  if(opts.has_key?(:file)) then
    doc = LibXML::XML::Document.file(opts[:file],:options => PARSER_OPTIONS)
  else
    doc = LibXML::XML::Document.string(opts[:data],:options => PARSER_OPTIONS)
  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



59
60
61
# File 'lib/cfpropertylist/rbLibXMLParser.rb', line 59

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

#new_text(val) ⇒ Object



63
64
65
# File 'lib/cfpropertylist/rbLibXMLParser.rb', line 63

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



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

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