Parent

Class Index [+]

Quicksearch

TaskJuggler::XMLElement

This class models an XML node that may contain other XML nodes. XML element trees can be constructed with the class constructor and converted into XML.

Public Class Methods

new(name, attributes = {}, selfClosing = false) click to toggle source

Construct a new XML element and include it in an existing XMLElement tree.

    # File lib/XMLElement.rb, line 22
22:     def initialize(name, attributes = {}, selfClosing = false)
23:       if (name.nil? && attributes.length > 0) ||
24:          (!name.nil? && !name.is_a?(String))
25:         raise "Name must be nil or a String "
26:       end
27:       @name = name
28:       attributes.each do |n, v|
29:         if n.nil? || v.nil?
30:           raise "Attribute name (#{n}) or value (#{v}) may not be nil"
31:         end
32:       end
33:       @attributes = attributes
34:       @children = []
35:       # This can be set to true if <name /> is legal for this element.
36:       @selfClosing = selfClosing
37:     end

Public Instance Methods

<<(arg) click to toggle source

Add a new child or a set of new childs to the element.

    # File lib/XMLElement.rb, line 40
40:     def <<(arg)
41:       # If the argument is an array, we have to insert each element
42:       # individually.
43:       if arg.is_a?(XMLElement)
44:         @children << arg
45:       elsif arg.is_a?(Array)
46:         # Delete all nil entries
47:         arg.delete_if { |i| i.nil? }
48:         # Check that the rest are really all XMLElement objects.
49:         arg.each do |i|
50:           unless i.is_a?(XMLElement)
51:             raise "Element must be of type XMLElement not #{i.class}"
52:           end
53:         end
54:         @children += arg
55:       elsif arg.nil?
56:         # do nothing
57:       else
58:         raise "Elements must be of type XMLElement not #{arg.class}"
59:       end
60:       self
61:     end
[](attribute) click to toggle source

Return the value of attribute attribute.

    # File lib/XMLElement.rb, line 69
69:     def [](attribute)
70:       @attributes[attribute]
71:     end
[]=(attribute, value) click to toggle source

Add or change attribute to value.

    # File lib/XMLElement.rb, line 64
64:     def []=(attribute, value)
65:       @attributes[attribute] = value
66:     end
to_s(indent = 0) click to toggle source

Return the element and all sub elements as properly formatted XML.

    # File lib/XMLElement.rb, line 75
75:     def to_s(indent = 0)
76:       out = '<' + @name
77:       @attributes.keys.sort.each do |attrName|
78:         out << " #{attrName}=\"#{escape(@attributes[attrName], true)}\""
79:       end
80:       if @children.empty? && @selfClosing
81:         out << '/>'
82:       else
83:         out << '>'
84:         @children.each do |child|
85:           # We only insert newlines for multiple childs and after a tag has been
86:           # closed.
87:           if @children.size > 1 && !child.is_a?(XMLText) && out[1] == >>
88:             out << "\n" + indentation(indent + 1)
89:           end
90:           out << child.to_s(indent + 1)
91:         end
92:         out << "\n" + indentation(indent) if @children.size > 1 && out[1] == >>
93:         out << '</' + @name + '>'
94:       end
95:     end

Protected Instance Methods

escape(str, quotes = false) click to toggle source

Escape special characters in input String str.

     # File lib/XMLElement.rb, line 100
100:     def escape(str, quotes = false)
101:       out = ''
102:       str.each_utf8_char do |c|
103:         case c
104:         when '&'
105:           out << '&amp;'
106:         when '"'
107:           out << '\"'
108:         else
109:           out << c
110:         end
111:       end
112:       out
113:     end
indentation(indent) click to toggle source
     # File lib/XMLElement.rb, line 115
115:     def indentation(indent)
116:       ' ' * indent
117:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.