Class: VCloudSdk::Xml::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud/vcloud/xml/wrapper.rb

Instance Method Summary collapse

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 60

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

  # Use (server) supplied prefixes defaulting to the preset ones for
  # those not specified.
  @doc_namespaces = ns_definitions.nil? ? Array.new :
    Array.new(ns_definitions)
  if @root.namespace_definitions
    @doc_namespaces.concat(@root.namespace_definitions)
  end
end

Instance Method Details

#==(other) ⇒ Object



182
183
184
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 182

def ==(other)
  @root.to_s == other.node.to_s
end

#[](attr) ⇒ Object



166
167
168
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 166

def [](attr)
  @root[attr]
end

#[]=(attr, value) ⇒ Object



170
171
172
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 170

def []=(attr, value)
  @root[attr] = value
end

#add_child(child, namespace_prefix = nil, namespace_href = nil, parent = @root) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 191

def add_child(child, namespace_prefix = nil, namespace_href = nil,
    parent = @root)
  if child.is_a? Wrapper
    parent.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 Bosh::Clouds::CpiError, "Cannot add child.  Unknown object passed in."
  end
end

#attribute_with_ns(attr, ns) ⇒ Object



118
119
120
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 118

def attribute_with_ns(attr, ns)
  @root.attribute_with_ns(attr, ns)
end

#contentObject



174
175
176
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 174

def content
  @root.content
end

#content=(value) ⇒ Object



178
179
180
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 178

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.



206
207
208
209
210
211
212
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 206

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

Raises:

  • (Bosh::Clouds::CpiError)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 138

def create_qualified_name(name, href)
  namespace_wanted = nil
  ns_wanted_no_prefix = nil
  # Do it this way so the namespaces are searched in the order they are
  # added.  The first one is the one closest to the node, while the ones
  # at the document root are the last.
  @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
  raise Bosh::Clouds::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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 122

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_namespacesObject



82
83
84
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 82

def doc_namespaces
  @doc_namespaces
end

#errorObject



98
99
100
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 98

def error
   @root["Error"]
end

#get_nodes(type_name, attrs = nil, only_immediate = false, namespace = VCLOUD_NAMESPACE) ⇒ Object



161
162
163
164
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 161

def get_nodes(type_name, attrs = nil, only_immediate = false,
    namespace = VCLOUD_NAMESPACE)
  xpath(create_xpath_query(type_name, attrs, only_immediate, namespace))
end

#hrefObject



90
91
92
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 90

def href
  @root["href"]
end

#href_idObject



94
95
96
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 94

def href_id
  href.split('/')[-1]
end

#nameObject



102
103
104
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 102

def name
  @root["name"]
end

#name=(name) ⇒ Object



106
107
108
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 106

def name=(name)
  @root["name"] = name
end

#to_sObject



186
187
188
189
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 186

def to_s
  add_namespaces.to_xml.each_line.inject("") {
    |xml, line| xml.concat(line.sub(/^\s+$/, "")) }
end

#typeObject



114
115
116
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 114

def type
  @root["type"]
end

#urnObject



110
111
112
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 110

def urn
  @root["id"]
end

#xpath(*args) ⇒ Object



86
87
88
# File 'lib/cloud/vcloud/xml/wrapper.rb', line 86

def xpath(*args)
  WrapperFactory::wrap_nodes(@root.xpath(*args), @ns, @doc_namespaces)
end