Class OpenNebula::XMLElement

  1. OpenNebula/XMLUtils.rb
Parent: Object
Pool VirtualNetworkPool ClusterPool ImagePool VirtualMachinePool HostPool UserPool XMLElement PoolElement XMLPool User VirtualNetwork Cluster Image VirtualMachine Host Enumerable Client ImageRepository Error OpenNebula dot/f_0.png

The XMLElement class provides an abstraction of the underlying XML parser engine. It provides XML-related methods for the Pool and PoolElement classes

Public class methods

build_xml (xml, root_element)

Builds a XML document

xml:String the XML document of the object
root_element:String Base xml element
return
XML object for the underlying XML engine
[show source]
    # File OpenNebula/XMLUtils.rb, line 44
44:         def self.build_xml(xml, root_element)
45:             if NOKOGIRI
46:                 doc = Nokogiri::XML(xml).xpath("/#{root_element}")
47:             else
48:                 doc = REXML::Document.new(xml).root
49:             end
50: 
51:             return doc
52:         end
new (xml=nil)
xml:_opaque xml object_ an xml object as returned by build_xml
[show source]
    # File OpenNebula/XMLUtils.rb, line 19
19:         def initialize(xml=nil)
20:             @xml = xml
21:         end

Public instance methods

[] (key)

Extract an element from the XML description of the PoolElement. key::String The name of the element

return
String the value of the element

Examples:

['VID'] # gets VM id
['HISTORY/HOSTNAME'] # get the hostname from the history
[show source]
    # File OpenNebula/XMLUtils.rb, line 59
59:         def [](key)
60:             if NOKOGIRI
61:                 element=@xml.xpath(key.to_s)
62: 
63:                 if element.size == 0
64:                     return nil
65:                 end
66:             else
67:                 element=@xml.elements[key.to_s]
68:             end
69: 
70:             if element
71:                 element.text
72:             end
73:         end
attr (key,name)

Gets an attribute from an elemenT

key:String xpath for the element
name:String name of the attribute
[show source]
    # File OpenNebula/XMLUtils.rb, line 78
78:         def attr(key,name)
79:             value = nil
80: 
81:             if NOKOGIRI
82:                 element=@xml.xpath(key.to_s.upcase)
83:                 if element.size == 0
84:                     return nil
85:                 end
86: 
87:                 attribute = element.attr(name)
88: 
89:                 value = attribute.text if attribute != nil
90:             else
91:                 element=@xml.elements[key.to_s.upcase]
92: 
93:                 value = element.attributes[name] if element != nil
94:             end
95: 
96:             return value
97:         end
each (xpath_str,&block)

Iterates over every Element in the XPath and calls the block with a a XMLElement

block:Block
[show source]
     # File OpenNebula/XMLUtils.rb, line 102
102:         def each(xpath_str,&block)
103:             if NOKOGIRI
104:                 @xml.xpath(xpath_str).each { |pelem|
105:                     block.call XMLElement.new(pelem)
106:                 }
107:             else
108:                 @xml.elements.each(xpath_str) { |pelem|
109:                     block.call XMLElement.new(pelem)
110:                 }
111:             end
112:         end
has_elements? (xpath_str)
[show source]
     # File OpenNebula/XMLUtils.rb, line 126
126:         def has_elements?(xpath_str)
127:             if NOKOGIRI
128:                 element = @xml.xpath(xpath_str.to_s.upcase)
129:                 return element != nil && element.children.size > 0
130:             else
131:                 element = @xml.elements[xpath_str.to_s]
132:                 return element != nil && element.has_elements?
133:             end
134:         end
initialize_xml (xml, root_element)

Initialize a XML document for the element

xml:String the XML document of the object
root_element:String Base xml element
[show source]
    # File OpenNebula/XMLUtils.rb, line 26
26:         def initialize_xml(xml, root_element)
27:             if NOKOGIRI
28:                 @xml = Nokogiri::XML(xml).xpath("/#{root_element}")
29:                 if @xml.size == 0
30:                     @xml = nil
31:                 end
32:             else
33:                 @xml = REXML::Document.new(xml).root
34:                 if @xml.name != root_element
35:                     @xml = nil
36:                 end
37:             end
38:         end
name ()
[show source]
     # File OpenNebula/XMLUtils.rb, line 114
114:         def name
115:             @xml.name
116:         end
template_like_str (root_element, indent=true)
[show source]
     # File OpenNebula/XMLUtils.rb, line 140
140:         def template_like_str(root_element, indent=true)
141:             if NOKOGIRI
142:                 xml_template=@xml.xpath(root_element).to_s
143:                 rexml=REXML::Document.new(xml_template).root
144:             else
145:                 rexml=@xml.elements[root_element]
146:             end
147: 
148:             if indent
149:                 ind_enter="\n"
150:                 ind_tab='  '
151:             else
152:                 ind_enter=''
153:                 ind_tab=' '
154:             end
155: 
156:             str=rexml.collect {|n|
157:                 if n.class==REXML::Element
158:                     str_line=""
159:                     if n.has_elements?
160:                         str_line << n.name << "=[" << ind_enter
161: 
162:                         str_line << n.collect {|n2|
163:                             if n2 && n2.class==REXML::Element
164:                                 str = ind_tab + n2.name + "="
165:                                 str += n2.text if n2.text
166:                                 str
167:                             end
168:                         }.compact.join(","+ind_enter)
169:                         str_line<<" ]"
170:                     else
171:                         str_line<<n.name << "=" << n.text.to_s
172:                     end
173:                     str_line
174:                 end
175:             }.compact.join("\n")
176: 
177:             str
178:         end
template_str (indent=true)
[show source]
     # File OpenNebula/XMLUtils.rb, line 136
136:         def template_str(indent=true)
137:             template_like_str('TEMPLATE', indent)
138:         end
text ()
[show source]
     # File OpenNebula/XMLUtils.rb, line 118
118:         def text
119:             if NOKOGIRI
120:                 @xml.content
121:             else
122:                 @xml.text
123:             end
124:         end
to_xml (pretty=false)
[show source]
     # File OpenNebula/XMLUtils.rb, line 180
180:         def to_xml(pretty=false)
181:             if NOKOGIRI
182:                 @xml.to_xml
183:             else
184:                 str = ""
185:                 if pretty
186:                     REXML::Formatters::Pretty.new(1).write(@xml,str)
187:                 else
188:                     REXML::Formatters::Default.new.write(@xml,str)
189:                 end
190:                 str
191:             end
192:         end