Class: OpenNebula::XMLElement
- Inherits:
-
Object
- Object
- OpenNebula::XMLElement
- Defined in:
- lib/OpenNebula/XMLUtils.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) ⇒ Object
Extract an element from the XML description of the PoolElement.
-
#attr(key, name) ⇒ Object
- Gets an attribute from an elemenT key
- String xpath for the element name
-
String name of the attribute.
-
#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
- #has_elements?(xpath_str) ⇒ Boolean
-
#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 elemenets extracted using the XPATH expression passed as filter.
- #template_like_str(root_element, indent = true) ⇒ Object
- #template_str(indent = true) ⇒ Object
- #text ⇒ Object
- #to_hash(hash = {}, element = nil) ⇒ Object
- #to_xml(pretty = false) ⇒ Object
Constructor Details
#initialize(xml = nil) ⇒ XMLElement
- xml
-
_opaque xml object_ an xml object as returned by build_xml
41 42 43 |
# File 'lib/OpenNebula/XMLUtils.rb', line 41 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
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/OpenNebula/XMLUtils.rb', line 70 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) ⇒ Object
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
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/OpenNebula/XMLUtils.rb', line 89 def [](key) if NOKOGIRI element=@xml.xpath(key.to_s) if element.size == 0 return nil end else element=@xml.elements[key.to_s] end if element element.text end end |
#attr(key, name) ⇒ Object
Gets an attribute from an elemenT
- key
-
String xpath for the element
- name
-
String name of the attribute
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/OpenNebula/XMLUtils.rb', line 131 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 |
#each(xpath_str, &block) ⇒ Object
Iterates over every Element in the XPath and calls the block with a a XMLElement
- block
-
Block
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/OpenNebula/XMLUtils.rb', line 155 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
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/OpenNebula/XMLUtils.rb', line 167 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 |
#has_elements?(xpath_str) ⇒ Boolean
191 192 193 194 195 196 197 198 199 |
# File 'lib/OpenNebula/XMLUtils.rb', line 191 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
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/OpenNebula/XMLUtils.rb', line 48 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 end |
#name ⇒ Object
179 180 181 |
# File 'lib/OpenNebula/XMLUtils.rb', line 179 def name @xml.name end |
#retrieve_elements(filter) ⇒ Object
Gets an array of text from elemenets extracted using the XPATH expression passed as filter
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/OpenNebula/XMLUtils.rb', line 107 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 |
#template_like_str(root_element, indent = true) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/OpenNebula/XMLUtils.rb', line 205 def template_like_str(root_element, indent=true) 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.collect {|n| if n.class==REXML::Element str_line="" if n.has_elements? str_line << n.name << "=[" << ind_enter str_line << n.collect {|n2| if n2 && n2.class==REXML::Element str = "" str << ind_tab << n2.name << '=' str << attr_to_str(n2.text) if n2.text str end }.compact.join(','+ind_enter) str_line<<" ]" else str_line << n.name << '=' << attr_to_str(n.text.to_s) end str_line end }.compact.join("\n") str end |
#template_str(indent = true) ⇒ Object
201 202 203 |
# File 'lib/OpenNebula/XMLUtils.rb', line 201 def template_str(indent=true) template_like_str('TEMPLATE', indent) end |
#text ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/OpenNebula/XMLUtils.rb', line 183 def text if NOKOGIRI @xml.content else @xml.text end end |
#to_hash(hash = {}, element = nil) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/OpenNebula/XMLUtils.rb', line 263 def to_hash(hash={}, element=nil) element ||= @xml.document.root if NOKOGIRI array = element.children if array.length==1 and (array.first.text? or array.first.cdata?) r = array.first.text else r = {} array.each { |c| if c.element? to_hash(r, c) end } end else r = {} if element.has_elements? element.each_element { |c| to_hash(r, c) } elsif element.has_text? r = element.text end end key = element.name if hash.has_key?(key) if hash[key].instance_of?(Array) hash[key] << r else hash[key] = [hash[key], r] end else hash[key] = r end hash end |
#to_xml(pretty = false) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/OpenNebula/XMLUtils.rb', line 246 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 |