Class: VCloudSdk::Xml::Wrapper
- Inherits:
-
Object
- Object
- VCloudSdk::Xml::Wrapper
show all
- Defined in:
- lib/ruby_vcloud_sdk/xml/wrapper.rb
Direct Known Subclasses
AdminCatalog, AdminOrg, AllocatedIpAddresses, CatalogItem, Disk, DiskAttachOrDetachParams, DiskCreateParams, EdgeGateway, Entity, File, GatewayInterface, InstantiateVAppTemplateParams, IpAddress, IpRange, IpRanges, IpScope, Item, Link, Media, MediaInsertOrEjectParams, MetadataValue, Network, NetworkConfig, NetworkConfigSection, NetworkConnection, NetworkConnectionSection, Org, OrgNetwork, OrgVdcNetwork, ProductSection, ProductSectionList, QueryResultRecords, RecomposeVAppParams, Session, SupportedVersions, Task, UploadVAppTemplateParams, VApp, VCloud, Vdc, VdcStorageProfile, VirtualHardwareSection, Vm, Vms
Instance Method Summary
collapse
-
#==(other) ⇒ Object
-
#[](attr) ⇒ Object
There seem to be a Nokogiri bug such that attribute’s namespace prefixes are ignored.
-
#[]=(attr, value) ⇒ Object
-
#add_child(child, namespace_prefix = nil, namespace_href = nil, parent = @root) ⇒ Object
-
#attribute(name) ⇒ Object
get the attribute regardless what namespace the attribute has.
-
#content ⇒ Object
-
#content=(value) ⇒ Object
-
#create_child(tag, namespace_prefix = nil, namespace_href = nil) ⇒ Object
Creates a child node but does not add it to the document.
-
#create_qualified_name(name, href) ⇒ Object
-
#create_xpath_query(type_name, attrs = nil, only_immediate = false, namespace = VCLOUD_NAMESPACE) ⇒ Object
-
#doc_namespaces ⇒ Object
-
#edit_link ⇒ Object
-
#get_nodes(type_name, attrs = nil, only_immediate = false, namespace = VCLOUD_NAMESPACE) ⇒ Object
-
#href ⇒ Object
-
#href=(href) ⇒ Object
-
#href_id ⇒ Object
-
#initialize(xml, ns = nil, ns_definitions = nil) ⇒ Wrapper
constructor
Because we are wrapping individual nodes in a wrapper and turning them into XML docs, we need to preserve the namespace information with each node.
-
#name ⇒ Object
-
#name=(name) ⇒ Object
-
#power_off_link ⇒ Object
-
#power_on_link ⇒ Object
-
#remove_link ⇒ Object
-
#running_tasks ⇒ Object
-
#to_s ⇒ Object
-
#type ⇒ Object
-
#type=(type) ⇒ Object
-
#undeploy_link ⇒ Object
-
#urn ⇒ Object
-
#xpath(*args) ⇒ Object
Constructor Details
#initialize(xml, ns = nil, ns_definitions = nil) ⇒ Wrapper
Because we are wrapping individual nodes in a wrapper and turning them into XML docs, we need to preserve the namespace information with each node
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 63
def initialize(xml, ns = nil, ns_definitions = nil)
if xml.is_a?(Nokogiri::XML::Document)
@doc = xml
@root = @doc.root
else
@root = xml
end
if ns
@ns = ns
else
@ns = @root.namespace
end
@doc_namespaces = ns_definitions.nil? ? [] :
Array.new(ns_definitions)
if @root.namespace_definitions
@doc_namespaces.concat(@root.namespace_definitions)
end
end
|
Instance Method Details
#==(other) ⇒ Object
230
231
232
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 230
def ==(other)
@root.to_s == other.node.to_s
end
|
#[](attr) ⇒ Object
There seem to be a Nokogiri bug such that attribute’s namespace prefixes are ignored. For example, to return an attribute named vcloud:capacity, use “capacity”, not “vcloud:capacity”.
209
210
211
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 209
def [](attr)
@root[attr]
end
|
#[]=(attr, value) ⇒ Object
218
219
220
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 218
def []=(attr, value)
@root[attr] = value
end
|
#add_child(child, namespace_prefix = nil, namespace_href = nil, parent = @root) ⇒ Object
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 243
def add_child(
child,
namespace_prefix = nil,
namespace_href = nil,
parent = @root)
if child.is_a? Wrapper
@root.add_child(child.node)
elsif child.is_a? String
node = Nokogiri::XML::Node.new(child, parent)
set_namespace(node, namespace_prefix, namespace_href)
parent.add_child(node)
else
fail CpiError, "Cannot add child. Unknown object passed in."
end
end
|
#attribute(name) ⇒ Object
get the attribute regardless what namespace the attribute has
214
215
216
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 214
def attribute(name)
@root.attribute(name)
end
|
#content ⇒ Object
222
223
224
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 222
def content
@root.content
end
|
#content=(value) ⇒ Object
226
227
228
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 226
def content=(value)
@root.content = value
end
|
#create_child(tag, namespace_prefix = nil, namespace_href = nil) ⇒ Object
Creates a child node but does not add it to the document. Used when a new child node has to be in a specific location or order.
261
262
263
264
265
266
267
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 261
def create_child(tag,
namespace_prefix = nil,
namespace_href = nil)
node = Nokogiri::XML::Node.new(tag, @root)
set_namespace(node, namespace_prefix, namespace_href)
node
end
|
#create_qualified_name(name, href) ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 177
def create_qualified_name(name, href)
namespace_wanted = nil
ns_wanted_no_prefix = nil
@doc_namespaces.each do |ns|
if ns.href == href
if ns.prefix.nil?
ns_wanted_no_prefix = ns
else
namespace_wanted = ns
break
end
end
end
namespace_wanted = ns_wanted_no_prefix unless namespace_wanted
fail CpiError,
"Namespace #{href} not found." unless namespace_wanted
ns_prefix = namespace_wanted.prefix.nil? ? "xmlns" :
namespace_wanted.prefix
"#{ns_prefix}:#{name}"
end
|
#create_xpath_query(type_name, attrs = nil, only_immediate = false, namespace = VCLOUD_NAMESPACE) ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 161
def create_xpath_query(type_name, attrs = nil, only_immediate = false,
namespace = VCLOUD_NAMESPACE)
qualified_name = create_qualified_name(type_name, namespace)
depth_prefix = only_immediate ? nil : ".//"
if attrs && attrs.length > 0
attrs_list = []
attrs.each do |k, v|
attrs_list.push(%Q[@#{k}="#{v}"])
end
"#{depth_prefix}#{qualified_name}[#{attrs_list.join(" and ")}]"
else
"#{depth_prefix}#{qualified_name}"
end
end
|
#doc_namespaces ⇒ Object
85
86
87
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 85
def doc_namespaces
@doc_namespaces
end
|
#edit_link ⇒ Object
132
133
134
135
136
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 132
def edit_link
get_nodes(XML_TYPE[:LINK],
{ rel: XML_TYPE[:EDIT] },
true).first
end
|
#get_nodes(type_name, attrs = nil, only_immediate = false, namespace = VCLOUD_NAMESPACE) ⇒ Object
201
202
203
204
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 201
def get_nodes(type_name, attrs = nil, only_immediate = false,
namespace = VCLOUD_NAMESPACE)
xpath(create_xpath_query(type_name, attrs, only_immediate, namespace))
end
|
#href ⇒ Object
93
94
95
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 93
def href
@root["href"]
end
|
#href=(href) ⇒ Object
97
98
99
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 97
def href=(href)
@root["href"] = href
end
|
#href_id ⇒ Object
101
102
103
104
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 101
def href_id
URI.parse(href).path.split('/')[-1]
end
|
#name ⇒ Object
106
107
108
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 106
def name
@root["name"]
end
|
#name=(name) ⇒ Object
110
111
112
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 110
def name=(name)
@root["name"] = name
end
|
#power_off_link ⇒ Object
144
145
146
147
148
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 144
def power_off_link
get_nodes(XML_TYPE[:LINK],
{ rel: "power:powerOff"},
true).first
end
|
#power_on_link ⇒ Object
138
139
140
141
142
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 138
def power_on_link
get_nodes(XML_TYPE[:LINK],
{ rel: "power:powerOn"},
true).first
end
|
#remove_link ⇒ Object
126
127
128
129
130
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 126
def remove_link
get_nodes(XML_TYPE[:LINK],
{ rel: XML_TYPE[:REMOVE] },
true).first
end
|
#running_tasks ⇒ Object
156
157
158
159
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 156
def running_tasks
get_nodes(XML_TYPE[:TASK],
{ status: TASK_STATUS[:RUNNING] })
end
|
#to_s ⇒ Object
234
235
236
237
238
239
240
241
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 234
def to_s
add_namespaces
.to_xml
.each_line
.reduce("") do |xml, line|
xml.concat(line.sub(/^\s+$/, ""))
end
end
|
#type ⇒ Object
118
119
120
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 118
def type
@root["type"]
end
|
#type=(type) ⇒ Object
122
123
124
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 122
def type=(type)
@root["type"] = type
end
|
#undeploy_link ⇒ Object
150
151
152
153
154
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 150
def undeploy_link
get_nodes(XML_TYPE[:LINK],
{ rel: "undeploy" },
true).first
end
|
#urn ⇒ Object
114
115
116
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 114
def urn
@root["id"]
end
|
#xpath(*args) ⇒ Object
89
90
91
|
# File 'lib/ruby_vcloud_sdk/xml/wrapper.rb', line 89
def xpath(*args)
WrapperFactory.wrap_nodes(@root.xpath(*args), @ns, @doc_namespaces)
end
|