| Class | REXML::Element |
| In: |
lib/xmpp4r/rexmladdons.rb
|
| Parent: | Object |
this class adds a few helper methods to REXML::Element
# File lib/xmpp4r/rexmladdons.rb, line 87
87: def self.import(xmlelement)
88: self.new(xmlelement.name).import(xmlelement)
89: end
Test for equality of two elements, useful for assert_equal in test cases. Tries to parse String o as XML.
See Test::Unit::Assertions
# File lib/xmpp4r/rexmladdons.rb, line 103
103: def ==(o)
104: return false unless self.kind_of? REXML::Element
105: if o.kind_of? REXML::Element
106: # Ok
107: elsif o.kind_of? String
108: # Parse o
109: begin
110: o = REXML::Document.new(o).root
111: rescue REXML::ParseException
112: return false
113: end
114: else
115: # Cannot compare with anything other than Elements or Strings
116: return false
117: end
118:
119: return false unless name == o.name
120:
121: attributes.each_attribute do |attr|
122: return false unless attr.value == o.attributes[attr.name]
123: end
124:
125: o.attributes.each_attribute do |attr|
126: return false unless attributes[attr.name] == attr.value
127: end
128:
129: children.each_with_index do |child,i|
130: return false unless child == o.children[i]
131: end
132:
133: return true
134: end
Deletes one or more children elements, not just one like REXML::Element#delete_element
# File lib/xmpp4r/rexmladdons.rb, line 94
94: def delete_elements(element)
95: while(delete_element(element)) do end
96: end
# File lib/xmpp4r/rexmladdons.rb, line 20
20: def each_elements(*els, &block)
21: els.inject([ ]) do |res, e|
22: res + each_element(e, &block)
23: end
24: end
Returns first element of name e
# File lib/xmpp4r/rexmladdons.rb, line 42
42: def first_element(e)
43: each_element(e) { |el| return el }
44: return nil
45: end
Returns text of first element of name e
# File lib/xmpp4r/rexmladdons.rb, line 49
49: def first_element_text(e)
50: el = first_element(e)
51: if el
52: return el.text
53: else
54: return nil
55: end
56: end
import this element‘s children and attributes
# File lib/xmpp4r/rexmladdons.rb, line 69
69: def import(xmlelement)
70: if @name and @name != xmlelement.name
71: raise "Trying to import an #{xmlelement.name} to a #{@name} !"
72: end
73: add_attributes(xmlelement.attributes.clone)
74: @context = xmlelement.context
75: xmlelement.each do |e|
76: if e.kind_of? REXML::Element
77: typed_add(e.deep_clone)
78: elsif e.kind_of? REXML::Text
79: add_text(e.value)
80: else
81: add(e.clone)
82: end
83: end
84: self
85: end
Replaces or adds a child element of name e with text t.
# File lib/xmpp4r/rexmladdons.rb, line 28
28: def replace_element_text(e, t)
29: el = first_element(e)
30: if el.nil?
31: el = REXML::Element.new(e)
32: add_element(el)
33: end
34: if t
35: el.text = t
36: end
37: self
38: end
This method does exactly the same thing as add(), but it can be overriden by subclasses to provide on-the-fly object creations. For example, if you import a REXML::Element of name ‘plop’, and you have a Plop class that subclasses REXML::Element, with typed_add you can get your REXML::Element to be "magically" converted to Plop.
# File lib/xmpp4r/rexmladdons.rb, line 63
63: def typed_add(e)
64: add(e)
65: end