Class REXML::Element
In: lib/xmpp4r/rexmladdons.rb
Parent: Object
IOSource Element lib/xmpp4r/rexmladdons.rb REXML dot/m_90_0.png

this class adds a few helper methods to REXML::Element

Methods

Public Class methods

[Source]

    # File lib/xmpp4r/rexmladdons.rb, line 81
81:     def self.import(xmlelement)
82:       self.new(xmlelement.name).import(xmlelement)
83:     end

Public Instance methods

Test for equality of two elements, useful for assert_equal in test cases. Tries to parse String o as XML.

See Test::Unit::Assertions

[Source]

     # File lib/xmpp4r/rexmladdons.rb, line 97
 97:     def ==(o)
 98:       return false unless self.kind_of? REXML::Element
 99:       if o.kind_of? REXML::Element
100:         # Ok
101:       elsif o.kind_of? String
102:         # Parse o
103:         begin
104:           o = REXML::Document.new(o).root
105:         rescue REXML::ParseException
106:           return false
107:         end
108:       else
109:         # Cannot compare with anything other than Elements or Strings
110:         return false
111:       end
112: 
113:       return false unless name == o.name
114: 
115:       attributes.each_attribute do |attr|
116:         return false unless attr.value == o.attributes[attr.name]
117:       end
118: 
119:       o.attributes.each_attribute do |attr|
120:         return false unless attributes[attr.name] == attr.value
121:       end
122: 
123:       children.each_with_index do |child,i|
124:         return false unless child == o.children[i]
125:       end
126: 
127:       return true
128:     end

Deletes one or more children elements, not just one like REXML::Element#delete_element

[Source]

    # File lib/xmpp4r/rexmladdons.rb, line 88
88:     def delete_elements(element)
89:       while(delete_element(element)) do end
90:     end

Returns first element of name e

[Source]

    # File lib/xmpp4r/rexmladdons.rb, line 36
36:     def first_element(e)
37:       each_element(e) { |el| return el }
38:       return nil
39:     end

Returns text of first element of name e

[Source]

    # File lib/xmpp4r/rexmladdons.rb, line 43
43:     def first_element_text(e)
44:       el = first_element(e)
45:       if el
46:         return el.text
47:       else
48:         return nil
49:       end
50:     end

import this element‘s children and attributes

[Source]

    # File lib/xmpp4r/rexmladdons.rb, line 63
63:     def import(xmlelement)
64:       if @name and @name != xmlelement.name
65:         raise "Trying to import an #{xmlelement.name} to a #{@name} !"
66:       end
67:       add_attributes(xmlelement.attributes.clone)
68:       @context = xmlelement.context
69:       xmlelement.each do |e|
70:         if e.kind_of? REXML::Element
71:           typed_add(e.deep_clone)
72:         elsif e.kind_of? REXML::Text
73:           add_text(e.value)
74:         else
75:           add(e.clone)
76:         end
77:       end
78:       self
79:     end

Replaces or adds a child element of name e with text t.

[Source]

    # File lib/xmpp4r/rexmladdons.rb, line 22
22:     def replace_element_text(e, t)
23:       el = first_element(e)
24:       if el.nil?
25:         el = REXML::Element.new(e)
26:         add_element(el)
27:       end
28:       if t
29:         el.text = t
30:       end
31:       self
32:     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.

[Source]

    # File lib/xmpp4r/rexmladdons.rb, line 57
57:     def typed_add(e)
58:       add(e)
59:     end

[Validate]