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, &block) click to toggle source

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

    # File lib/taskjuggler/XMLElement.rb, line 23
23:     def initialize(name, attributes = {}, selfClosing = false, &block)
24:       if (name.nil? && attributes.length > 0) ||
25:          (!name.nil? && !name.is_a?(String))
26:         raise "Name must be nil or a String "
27:       end
28:       @name = name
29:       attributes.each do |n, v|
30:         if n.nil? || v.nil?
31:           raise "Attribute name (#{n}) or value (#{v}) may not be nil"
32:         end
33:       end
34:       @attributes = attributes
35:       # This can be set to true if <name /> is legal for this element.
36:       @selfClosing = selfClosing
37: 
38:       @children = block ? yield(block) : []
39:       # Allow blocks with single elements not to be Arrays. They will be
40:       # automatically converted into Arrays here.
41:       unless @children.is_a?(Array)
42:         @children = [ @children ]
43:       else
44:         @children.flatten!
45:       end
46: 
47:       # Convert all children that are text String objects into XMLText
48:       # objects.
49:       @children.collect! do |c|
50:         c.is_a?(String) ? XMLText.new(c) : c
51:       end
52: 
53:       # Make sure we have no nil objects in the list.
54:       @children.delete_if { |c| c.nil? }
55: 
56:       # Now all children must be XMLElement objects.
57:       @children.each do |c|
58:         unless c.is_a?(XMLElement)
59:           raise ArgumentError,
60:             "Element must be of type XMLElement, not #{c.class}: #{c.inspect}"
61:         end
62:       end
63:     end

Public Instance Methods

<<(arg) click to toggle source

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

    # File lib/taskjuggler/XMLElement.rb, line 66
66:     def <<(arg)
67:       # If the argument is an array, we have to insert each element
68:       # individually.
69:       if arg.is_a?(XMLElement)
70:         @children << arg
71:       elsif arg.is_a?(String)
72:         @children << XMLText.new(arg)
73:       elsif arg.is_a?(Array)
74:         # Delete all nil entries
75:         arg.delete_if { |i| i.nil? }
76:         # Check that the rest are really all XMLElement objects.
77:         arg.each do |i|
78:           unless i.is_a?(XMLElement)
79:             raise ArgumentError,
80:               "Element must be of type XMLElement, not #{i.class}: #{i.inspect}"
81:           end
82:         end
83:         @children += arg
84:       elsif arg.nil?
85:         # do nothing
86:       else
87:         raise "Elements must be of type XMLElement not #{arg.class}"
88:       end
89:       self
90:     end
[](attribute) click to toggle source

Return the value of attribute attribute.

     # File lib/taskjuggler/XMLElement.rb, line 98
 98:     def [](attribute)
 99:       @attributes[attribute]
100:     end
[]=(attribute, value) click to toggle source

Add or change attribute to value.

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

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

     # File lib/taskjuggler/XMLElement.rb, line 104
104:     def to_s(indent = 0)
105:       out = '<' + @name
106:       @attributes.keys.sort.each do |attrName|
107:         out << " #{attrName}=\"#{escape(@attributes[attrName], true)}\""
108:       end
109:       if @children.empty? && @selfClosing
110:         out << '/>'
111:       else
112:         out << '>'
113:         @children.each do |child|
114:           # We only insert newlines for multiple childs and after a tag has been
115:           # closed.
116:           if @children.size > 1 && !child.is_a?(XMLText) && out[1] == >>
117:             out << "\n" + indentation(indent + 1)
118:           end
119:           out << child.to_s(indent + 1)
120:         end
121:         out << "\n" + indentation(indent) if @children.size > 1 && out[1] == >>
122:         out << '</' + @name + '>'
123:       end
124:     end

Protected Instance Methods

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

Escape special characters in input String str.

     # File lib/taskjuggler/XMLElement.rb, line 129
129:     def escape(str, quotes = false)
130:       out = ''
131:       str.each_utf8_char do |c|
132:         case c
133:         when '&'
134:           out << '&amp;'
135:         when '"'
136:           out << '\"'
137:         else
138:           out << c
139:         end
140:       end
141:       out
142:     end
indentation(indent) click to toggle source
     # File lib/taskjuggler/XMLElement.rb, line 144
144:     def indentation(indent)
145:       ' ' * indent
146:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.