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, flags = []) ⇒ Element

:yields: Handsoap::XmlMason::Element



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/handsoap/xml_mason.rb', line 104

def initialize(parent, prefix, node_name, value = nil, flags = []) # :yields: Handsoap::XmlMason::Element
  super()
#         if prefix.to_s == ""
#           raise "missing prefix"
#         end
  @parent = parent
  @prefix = prefix
  @node_name = node_name
  @children = []
  @attributes = {}
  if not value.nil?
    set_value value.to_s, *flags
  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



132
133
134
135
136
137
138
# File 'lib/handsoap/xml_mason.rb', line 132

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)


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

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.



122
123
124
# File 'lib/handsoap/xml_mason.rb', line 122

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

#find(name) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/handsoap/xml_mason.rb', line 159

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



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/handsoap/xml_mason.rb', line 174

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)



126
127
128
# File 'lib/handsoap/xml_mason.rb', line 126

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

#get_namespace(prefix) ⇒ Object



190
191
192
# File 'lib/handsoap/xml_mason.rb', line 190

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

#set_attr(name, value) ⇒ Object

Sets the value of an attribute.



155
156
157
158
# File 'lib/handsoap/xml_mason.rb', line 155

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

#set_value(value, *flags) ⇒ Object

Sets the inner text of this element.

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

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



144
145
146
147
148
149
150
151
152
153
# File 'lib/handsoap/xml_mason.rb', line 144

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

#to_s(indentation = '') ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/handsoap/xml_mason.rb', line 196

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)


187
188
189
# File 'lib/handsoap/xml_mason.rb', line 187

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