Class: Handsoap::XmlMason::Element
- Defined in:
- lib/handsoap/xml_mason.rb
Instance Method Summary collapse
-
#append_child(node) ⇒ Object
Adds a child node.
- #defines_namespace?(prefix) ⇒ Boolean
-
#document ⇒ Object
Returns the document that this element belongs to, or self if this is the document.
- #find(name) ⇒ Object
- #find_all(name) ⇒ Object
-
#full_name ⇒ Object
Returns the qname (prefix:nodename).
- #get_namespace(prefix) ⇒ Object
-
#initialize(parent, prefix, node_name, value = nil, flags = []) ⇒ Element
constructor
:yields: Handsoap::XmlMason::Element.
-
#set_attr(name, value) ⇒ Object
Sets the value of an attribute.
-
#set_value(value, *flags) ⇒ Object
Sets the inner text of this element.
- #to_s(indentation = '') ⇒ Object
- #value_node? ⇒ Boolean
Methods inherited from Node
Constructor Details
#initialize(parent, prefix, node_name, value = nil, flags = []) ⇒ Element
:yields: Handsoap::XmlMason::Element
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/handsoap/xml_mason.rb', line 108 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
136 137 138 139 140 141 142 |
# File 'lib/handsoap/xml_mason.rb', line 136 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
197 198 199 |
# File 'lib/handsoap/xml_mason.rb', line 197 def defines_namespace?(prefix) @attributes.keys.include?("xmlns:#{prefix}") || @parent.defines_namespace?(prefix) end |
#document ⇒ Object
Returns the document that this element belongs to, or self if this is the document.
126 127 128 |
# File 'lib/handsoap/xml_mason.rb', line 126 def document @parent.respond_to?(:document) ? @parent.document : @parent end |
#find(name) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/handsoap/xml_mason.rb', line 163 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
178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/handsoap/xml_mason.rb', line 178 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_name ⇒ Object
Returns the qname (prefix:nodename)
130 131 132 |
# File 'lib/handsoap/xml_mason.rb', line 130 def full_name @prefix.nil? ? @node_name : (@prefix + ":" + @node_name) end |
#get_namespace(prefix) ⇒ Object
194 195 196 |
# File 'lib/handsoap/xml_mason.rb', line 194 def get_namespace(prefix) @namespaces[prefix] || @parent.get_namespace(prefix) end |
#set_attr(name, value) ⇒ Object
Sets the value of an attribute.
159 160 161 162 |
# File 'lib/handsoap/xml_mason.rb', line 159 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
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/handsoap/xml_mason.rb', line 148 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
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/handsoap/xml_mason.rb', line 200 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
191 192 193 |
# File 'lib/handsoap/xml_mason.rb', line 191 def value_node? @children.length == 1 && @children[0].kind_of?(TextNode) end |