Class: Handsoap::XmlMason::Element

Inherits:
Node
  • Object
show all
Defined in:
lib/handsoap/xml_mason.rb

Instance Method Summary collapse

Methods inherited from Node

#add, #alias

Constructor Details

#initialize(parent, prefix, node_name, value = nil, options = {}) ⇒ Element

:yields: Handsoap::XmlMason::Element



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/handsoap/xml_mason.rb', line 108

def initialize(parent, prefix, node_name, value = nil, options = {}) # :yields: Handsoap::XmlMason::Element
  super()
#         if prefix.to_s == ""
#           raise "missing prefix"
#         end
  @parent = parent
  @prefix = prefix
  @node_name = node_name
  @children = []
  @attributes = {}
  if options[:attributes]
    @attributes = options[:attributes]
  end
  if not value.nil?
    set_value value.to_s, options
  end
  if block_given?
    yield self
  end
end

Instance Method Details

#append_child(node) ⇒ Object

Adds a child node.

You usually won’t need to call this method, but will rather use add



139
140
141
142
143
144
145
# File 'lib/handsoap/xml_mason.rb', line 139

def append_child(node)
  if value_node?
    raise "Element already has a text value. Can't add nodes"
  end
  @children << node
  return node
end

#defines_namespace?(prefix) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/handsoap/xml_mason.rb', line 200

def defines_namespace?(prefix)
  @attributes.keys.include?("xmlns:#{prefix}") || @parent.defines_namespace?(prefix)
end

#documentObject

Returns the document that this element belongs to, or self if this is the document.



129
130
131
# File 'lib/handsoap/xml_mason.rb', line 129

def document
  @parent.respond_to?(:document) ? @parent.document : @parent
end

#find(name) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/handsoap/xml_mason.rb', line 166

def find(name)
  name = name.to_s if name.kind_of? Symbol
  if @node_name == name || full_name == name
    return self
  end
  @children.each do |node|
    if node.respond_to? :find
      tmp = node.find(name)
      if tmp
        return tmp
      end
    end
  end
  return nil
end

#find_all(name) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/handsoap/xml_mason.rb', line 181

def find_all(name)
  name = name.to_s if name.kind_of? Symbol
  result = []
  if @node_name == name || full_name == name
    result << self
  end
  @children.each do |node|
    if node.respond_to? :find
      result = result.concat(node.find_all(name))
    end
  end
  return result
end

#full_nameObject

Returns the qname (prefix:nodename)



133
134
135
# File 'lib/handsoap/xml_mason.rb', line 133

def full_name
  @prefix.nil? ? @node_name : (@prefix + ":" + @node_name)
end

#get_namespace(prefix) ⇒ Object



197
198
199
# File 'lib/handsoap/xml_mason.rb', line 197

def get_namespace(prefix)
  @namespaces[prefix] || @parent.get_namespace(prefix)
end

#set_attr(name, value) ⇒ Object

Sets the value of an attribute.



162
163
164
165
# File 'lib/handsoap/xml_mason.rb', line 162

def set_attr(name, value)
  full_name = parse_ns(name).join(":")
  @attributes[name] = value
end

#set_value(value, options = {}) ⇒ Object

Sets the inner text of this element.

By default the string is escaped, but you can pass the option flag :raw to inject XML.

You usually won’t need to call this method, but will rather use add



151
152
153
154
155
156
157
158
159
160
# File 'lib/handsoap/xml_mason.rb', line 151

def set_value(value, options = {})
  if @children.length > 0
    raise "Element already has children. Can't set value"
  end
  if options && options.include?(:raw)
    @children = [RawContent.new(value)]
  else
    @children = [TextNode.new(value)]
  end
end

#to_s(indentation = '') ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/handsoap/xml_mason.rb', line 203

def to_s(indentation = '')
  # todo resolve attribute prefixes aswell
  if @prefix && (not defines_namespace?(@prefix))
    set_attr "xmlns:#{@prefix}", get_namespace(@prefix)
  end
  name = XmlMason.xml_escape(full_name)
  attr = (@attributes.any? ? (" " + @attributes.map { |key, value| XmlMason.xml_escape(key) + '="' + XmlMason.xml_escape(value) + '"' }.join(" ")) : "")
  if @children.any?
    if value_node?
      children = @children[0].to_s(indentation + "  ")
    else
      children = @children.map { |node| "\n" + node.to_s(indentation + "  ") }.join("") + "\n" + indentation
    end
    indentation + "<" + name + attr + ">" + children + "</" + name + ">"
  else
    indentation + "<" + name + attr + " />"
  end
end

#value_node?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/handsoap/xml_mason.rb', line 194

def value_node?
  @children.length == 1 && @children[0].kind_of?(TextNode)
end