Class: CFPropertyList::XML

Inherits:
ParserInterface show all
Defined in:
lib/rbREXMLParser.rb,
lib/rbLibXMLParser.rb,
lib/rbNokogiriParser.rb

Overview

XML parser

Instance Method Summary collapse

Instance Method Details

#append_node(parent, child) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/rbREXMLParser.rb', line 58

def append_node(parent, child)
  if child.is_a?(String) then
    parent.add_text child
  else
    parent.elements << child
  end
  parent
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
# File 'lib/rbREXMLParser.rb', line 12

def load(opts)
  if(opts.has_key?(:file)) then
    File.open(opts[:file], "rb") { |fd| doc = REXML::Document.new(fd) }
  else
    doc = REXML::Document.new(opts[:data])
  end

  root = doc.root.elements[1]
  return import_xml(root)
end

#new_node(name) ⇒ Object



49
50
51
52
# File 'lib/rbREXMLParser.rb', line 49

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

#new_text(val) ⇒ Object



54
55
56
# File 'lib/rbREXMLParser.rb', line 54

def new_text(val)
  val
end

#to_str(opts = {}) ⇒ Object

serialize CFPropertyList object to XML

opts = {}

Specify options: :formatted - Use indention and line breaks



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rbREXMLParser.rb', line 25

def to_str(opts={})
  doc = REXML::Document.new
  @doc = doc

  doc.context[:attribute_quote] = :quote

  doc.add_element 'plist', {'version' => '1.0'}
  doc.root << opts[:root].to_xml(self)

  formatter = if opts[:formatted] then
    f = REXML::Formatters::Pretty.new(2)
    f.compact = true
    f
  else
    REXML::Formatters::Default.new
  end

  str = formatter.write(doc.root, "")
  str1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" + str + "\n"
  str1.force_encoding('UTF-8') if str1.respond_to?(:force_encoding)

  return str1
end