Class: OpenNebula::XMLElement
- Inherits:
-
Object
- Object
- OpenNebula::XMLElement
- Defined in:
- lib/opennebula/xml_element.rb
Overview
The XMLElement class provides an abstraction of the underlying XML parser engine. It provides XML-related methods for the Pool and PoolElement classes
Direct Known Subclasses
Class Method Summary collapse
-
.build_xml(xml, root_element) ⇒ Object
- 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.
Instance Method Summary collapse
-
#[](key) ⇒ String?
Extract a text element from the XML description of the PoolElement.
-
#add_element(xpath, elems) ⇒ Object
Add a new element to the xml xpath::String xpath xpression where the elemente will be added elems::Hash Hash containing the pairs key-value to be included Examples: add_element(‘VM’, ‘NEW_ITEM’ => ‘NEW_VALUE’) <VM><NEW_ITEM>NEW_VALUE</NEW_ITEM>…</VM>.
-
#attr(key, name) ⇒ Object
- Gets an attribute from an element key
- String xpath for the element name
-
String name of the attribute.
-
#delete_element(xpath) ⇒ Object
Delete an element from the xml xpath::String xpath expression that selects the elemnts to be deleted.
-
#each(xpath_str, &block) ⇒ Object
- Iterates over every Element in the XPath and calls the block with a a XMLElement block
-
Block.
- #each_xpath(xpath_str, &block) ⇒ Object
-
#element_xml(xpath) ⇒ Object
Returns the xml of an element.
-
#has_elements?(xpath_str) ⇒ Boolean
- Returns wheter there are elements for a given XPath xpath_str
-
String XPath expression to locate the element.
-
#initialize(xml = nil) ⇒ XMLElement
constructor
- xml
-
_opaque xml object_ an xml object as returned by build_xml.
-
#initialize_xml(xml, root_element) ⇒ Object
- Initialize a XML document for the element xml
- String the XML document of the object root_element
-
String Base xml element.
- #name ⇒ Object
-
#retrieve_elements(filter) ⇒ Object
Gets an array of text from elements extracted using the XPATH expression passed as filter.
-
#retrieve_xmlelements(xpath_str) ⇒ XMLElement
Iterates over every Element in the XPath and returns an array with XMLElements.
-
#set_content(content) ⇒ Object
Update the content of the current doc.
-
#template_like_str(root_element, indent = true, xpath_exp = nil) ⇒ Object
- Returns elements in text form Known bug: For some older versions of REXML, the xpath expression returns more nodes than expected root_element
- String base element indent
- Boolean indents the resulting string, default true xpath_exp
-
String filter elements with a XPath.
-
#template_str(indent = true) ⇒ Object
- Returns the <TEMPLATE> element in text form indent
-
Boolean indents the resulting string, default true.
-
#template_xml ⇒ Object
Returns the <TEMPLATE> element in XML form.
- #text ⇒ Object
-
#to_hash ⇒ Hash
A hash representing the resource.
- #to_xml(pretty = false) ⇒ Object
-
#xml_nil? ⇒ Boolean
Checks if the internal XML representation is valid.
Constructor Details
#initialize(xml = nil) ⇒ XMLElement
- xml
-
_opaque xml object_ an xml object as returned by build_xml
26 27 28 |
# File 'lib/opennebula/xml_element.rb', line 26 def initialize(xml=nil) @xml = xml end |
Class Method Details
.build_xml(xml, root_element) ⇒ Object
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
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/opennebula/xml_element.rb', line 56 def self.build_xml(xml, root_element) begin if NOKOGIRI doc = Nokogiri::XML(xml).xpath("/#{root_element}") else doc = REXML::Document.new(xml).root end rescue Exception => e return OpenNebula::Error.new(e.) end return doc end |
Instance Method Details
#[](key) ⇒ String?
Extract a text element from the XML description of the PoolElement.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/opennebula/xml_element.rb', line 86 def [](key) if NOKOGIRI element=@xml.xpath(key.to_s) return nil if element.size == 0 else element=@xml.elements[key.to_s] return '' if element && !element.has_text? end element.text if element end |
#add_element(xpath, elems) ⇒ Object
Add a new element to the xml xpath::String xpath xpression where the elemente will be added elems::Hash Hash containing the pairs key-value to be included Examples:
add_element('VM', 'NEW_ITEM' => 'NEW_VALUE')
<VM><NEW_ITEM>NEW_VALUE</NEW_ITEM>...</VM>
add_element('VM/TEMPLATE', 'V1' => {'X1' => 'A1', 'Y2' => 'A2'})
<VM><TEMPLATE><V1><X1>A1</X1><Y2>A2</Y2>...</TEMPLATE></VM>
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/opennebula/xml_element.rb', line 119 def add_element(xpath, elems) elems.each { |key, value| if value.instance_of?(Hash) if NOKOGIRI elem = Nokogiri::XML::Node.new key, @xml.document value.each { |k2, v2| child = Nokogiri::XML::Node.new k2, elem child.content = v2 elem.add_child(child) } @xml.xpath(xpath.to_s).first.add_child(elem) else elem = REXML::Element.new(key) value.each { |k2, v2| elem.add_element(k2).text = v2 } @xml.elements[xpath].add_element(elem) end else if NOKOGIRI elem = Nokogiri::XML::Node.new key, @xml.document elem.content = value @xml.xpath(xpath.to_s).first.add_child(elem) else @xml.elements[xpath].add_element(key).text = value end end } end |
#attr(key, name) ⇒ Object
Gets an attribute from an element
- key
-
String xpath for the element
- name
-
String name of the attribute
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/opennebula/xml_element.rb', line 201 def attr(key,name) value = nil if NOKOGIRI element=@xml.xpath(key.to_s.upcase) if element.size == 0 return nil end attribute = element.attr(name) value = attribute.text if attribute != nil else element=@xml.elements[key.to_s.upcase] value = element.attributes[name] if element != nil end return value end |
#delete_element(xpath) ⇒ Object
Delete an element from the xml xpath::String xpath expression that selects the elemnts to be deleted
102 103 104 105 106 107 108 |
# File 'lib/opennebula/xml_element.rb', line 102 def delete_element(xpath) if NOKOGIRI @xml.xpath(xpath.to_s).remove else @xml.delete_element(xpath.to_s) end end |
#each(xpath_str, &block) ⇒ Object
Iterates over every Element in the XPath and calls the block with a a XMLElement
- block
-
Block
225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/opennebula/xml_element.rb', line 225 def each(xpath_str,&block) if NOKOGIRI @xml.xpath(xpath_str).each { |pelem| block.call XMLElement.new(pelem) } else @xml.elements.each(xpath_str) { |pelem| block.call XMLElement.new(pelem) } end end |
#each_xpath(xpath_str, &block) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/opennebula/xml_element.rb', line 237 def each_xpath(xpath_str,&block) if NOKOGIRI @xml.xpath(xpath_str).each { |pelem| block.call pelem.text } else @xml.elements.each(xpath_str) { |pelem| block.call pelem.text } end end |
#element_xml(xpath) ⇒ Object
Returns the xml of an element
289 290 291 292 293 294 295 |
# File 'lib/opennebula/xml_element.rb', line 289 def element_xml(xpath) if NOKOGIRI @xml.xpath(xpath).to_s else @xml.elements[xpath].to_s end end |
#has_elements?(xpath_str) ⇒ Boolean
Returns wheter there are elements for a given XPath
- xpath_str
-
String XPath expression to locate the element
263 264 265 266 267 268 269 270 271 |
# File 'lib/opennebula/xml_element.rb', line 263 def has_elements?(xpath_str) if NOKOGIRI element = @xml.xpath(xpath_str.to_s.upcase) return element != nil && element.children.size > 0 else element = @xml.elements[xpath_str.to_s] return element != nil && element.has_elements? end end |
#initialize_xml(xml, root_element) ⇒ Object
Initialize a XML document for the element
- xml
-
String the XML document of the object
- root_element
-
String Base xml element
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/opennebula/xml_element.rb', line 33 def initialize_xml(xml, root_element) @xml = XMLElement.build_xml(xml, root_element) if OpenNebula.is_error?(@xml) @xml = nil else if NOKOGIRI if @xml.size == 0 @xml = nil end else if @xml.name != root_element @xml = nil end end end @xml end |
#name ⇒ Object
249 250 251 |
# File 'lib/opennebula/xml_element.rb', line 249 def name @xml.name end |
#retrieve_elements(filter) ⇒ Object
Gets an array of text from elements extracted using the XPATH expression passed as filter
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/opennebula/xml_element.rb', line 160 def retrieve_elements(filter) elements_array = Array.new if NOKOGIRI @xml.xpath(filter.to_s).each { |pelem| elements_array << pelem.text if pelem.text } else @xml.elements.each(filter.to_s) { |pelem| elements_array << pelem.text if pelem.text } end if elements_array.size == 0 return nil else return elements_array end end |
#retrieve_xmlelements(xpath_str) ⇒ XMLElement
Iterates over every Element in the XPath and returns an array with XMLElements
184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/opennebula/xml_element.rb', line 184 def retrieve_xmlelements(xpath_str) collection = [] if NOKOGIRI @xml.xpath(xpath_str).each { |pelem| collection << XMLElement.new(pelem) } else @xml.elements.each(xpath_str) { |pelem| collection << XMLElement.new(pelem) } end collection end |
#set_content(content) ⇒ Object
Update the content of the current doc
150 151 152 153 154 155 156 |
# File 'lib/opennebula/xml_element.rb', line 150 def set_content(content) if NOKOGIRI @xml.content = content else @xml.text = content end end |
#template_like_str(root_element, indent = true, xpath_exp = nil) ⇒ Object
Returns elements in text form Known bug: For some older versions of REXML, the xpath expression
returns more nodes than expected
- root_element
-
String base element
- indent
-
Boolean indents the resulting string, default true
- xpath_exp
-
String filter elements with a XPath
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/opennebula/xml_element.rb', line 303 def template_like_str(root_element, indent=true, xpath_exp=nil) if NOKOGIRI xml_template = @xml.xpath(root_element).to_s rexml = REXML::Document.new(xml_template).root else rexml = @xml.elements[root_element] end if indent ind_enter = "\n" ind_tab = ' ' else ind_enter = '' ind_tab = ' ' end str = rexml.elements.collect(xpath_exp) {|n| next if n.class != REXML::Element str_line = "" if n.has_elements? str_line << "#{n.name}=[#{ind_enter}" << n.collect { |n2| next if n2.class != REXML::Element or !n2.has_text? str = "#{ind_tab}#{n2.name}=#{attr_to_str(n2.text)}" }.compact.join(",#{ind_enter}") << " ]" else next if !n.has_text? str_line << "#{n.name}=#{attr_to_str(n.text)}" end str_line }.compact.join("\n") return str end |
#template_str(indent = true) ⇒ Object
Returns the <TEMPLATE> element in text form
- indent
-
Boolean indents the resulting string, default true
275 276 277 |
# File 'lib/opennebula/xml_element.rb', line 275 def template_str(indent=true) template_like_str('TEMPLATE', indent) end |
#template_xml ⇒ Object
Returns the <TEMPLATE> element in XML form
280 281 282 283 284 285 286 |
# File 'lib/opennebula/xml_element.rb', line 280 def template_xml if NOKOGIRI @xml.xpath('TEMPLATE').to_s else @xml.elements['TEMPLATE'].to_s end end |
#text ⇒ Object
253 254 255 256 257 258 259 |
# File 'lib/opennebula/xml_element.rb', line 253 def text if NOKOGIRI @xml.content else @xml.text end end |
#to_hash ⇒ Hash
Returns a hash representing the resource.
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/opennebula/xml_element.rb', line 365 def to_hash hash = {} if NOKOGIRI if @xml.instance_of?(Nokogiri::XML::NodeSet) @xml.each { |c| if c.element? build_hash(hash, c) end } else build_hash(hash, @xml) end else build_hash(hash, @xml) end hash end |
#to_xml(pretty = false) ⇒ Object
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/opennebula/xml_element.rb', line 347 def to_xml(pretty=false) if NOKOGIRI && pretty str = @xml.to_xml elsif REXML_FORMATTERS && pretty str = String.new formatter = REXML::Formatters::Pretty.new formatter.compact = true formatter.write(@xml,str) else str = @xml.to_s end return str end |
#xml_nil? ⇒ Boolean
Checks if the internal XML representation is valid
71 72 73 |
# File 'lib/opennebula/xml_element.rb', line 71 def xml_nil? return @xml.nil? end |