Class: REXML::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/xmpp4r/lib/xmpp4r/rexmladdons.rb

Overview

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

Direct Known Subclasses

Jabber::XMPPElement, Place, Thing, World

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.import(xmlelement) ⇒ Object



81
82
83
# File 'lib/vendor/xmpp4r/lib/xmpp4r/rexmladdons.rb', line 81

def self.import(xmlelement)
  self.new(xmlelement.name).import(xmlelement)
end

Instance Method Details

#==(o) ⇒ Object

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

See Test::Unit::Assertions



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/vendor/xmpp4r/lib/xmpp4r/rexmladdons.rb', line 97

def ==(o)
  return false unless self.kind_of? REXML::Element
  if o.kind_of? REXML::Element
    # Ok
  elsif o.kind_of? String
    # Parse o
    begin
      o = REXML::Document.new(o).root
    rescue REXML::ParseException
      return false
    end
  else
    # Cannot compare with anything other than Elements or Strings
    return false
  end

  return false unless name == o.name

  attributes.each_attribute do |attr|
    return false unless attr.value == o.attributes[attr.name]
  end

  o.attributes.each_attribute do |attr|
    return false unless attributes[attr.name] == attr.value
  end

  children.each_with_index do |child,i|
    return false unless child == o.children[i]
  end

  return true
end

#delete_elements(element) ⇒ Object

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



88
89
90
# File 'lib/vendor/xmpp4r/lib/xmpp4r/rexmladdons.rb', line 88

def delete_elements(element)
  while(delete_element(element)) do end
end

#first_element(e) ⇒ Object

Returns first element of name e



36
37
38
39
# File 'lib/vendor/xmpp4r/lib/xmpp4r/rexmladdons.rb', line 36

def first_element(e)
  each_element(e) { |el| return el }
  return nil
end

#first_element_text(e) ⇒ Object

Returns text of first element of name e



43
44
45
46
47
48
49
50
# File 'lib/vendor/xmpp4r/lib/xmpp4r/rexmladdons.rb', line 43

def first_element_text(e)
  el = first_element(e)
  if el
    return el.text
  else
    return nil
  end
end

#import(xmlelement) ⇒ Object

import this element’s children and attributes



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/vendor/xmpp4r/lib/xmpp4r/rexmladdons.rb', line 63

def import(xmlelement)
  if @name and @name != xmlelement.name
    raise "Trying to import an #{xmlelement.name} to a #{@name} !"
  end
  add_attributes(xmlelement.attributes.clone)
  @context = xmlelement.context
  xmlelement.each do |e|
    if e.kind_of? REXML::Element
      typed_add(e.deep_clone)
    elsif e.kind_of? REXML::Text
      add_text(e.value)
    else
      add(e.clone)
    end
  end
  self
end

#replace_element_text(e, t) ⇒ Object

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



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vendor/xmpp4r/lib/xmpp4r/rexmladdons.rb', line 22

def replace_element_text(e, t)
  el = first_element(e)
  if el.nil?
    el = REXML::Element.new(e)
    add_element(el)
  end
  if t
    el.text = t
  end
  self
end

#typed_add(e) ⇒ Object

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.



57
58
59
# File 'lib/vendor/xmpp4r/lib/xmpp4r/rexmladdons.rb', line 57

def typed_add(e)
  add(e)
end