Class Jabber::XMPPElement
In: lib/xmpp4r/xmppelement.rb
Parent: REXML::Element

This class represents an XML element and provides functionality for automatic casting of XML element classes according to their element name and namespace.

Deriving classes must met these criteria:

  • The element name and namespace must be specified by calling the name_xmlns class method
  • The class constructor must be callable with no mandatory parameter

Methods

Public Class methods

Find a class for given name and namespace

name:[String]
xmlns:[String]
result:A descendant of XMPPElement or REXML::Element

[Source]

    # File lib/xmpp4r/xmppelement.rb, line 70
70:     def self.class_for_name_xmlns(name, xmlns)
71:       if @@name_xmlns_classes.has_key? [name, xmlns]
72:         @@name_xmlns_classes[[name, xmlns]]
73:       elsif @@name_xmlns_classes.has_key? [name, nil]
74:         @@name_xmlns_classes[[name, nil]]
75:       else
76:         REXML::Element
77:       end
78:     end

Set whether this element is always built with an xmlns attribute

[Source]

    # File lib/xmpp4r/xmppelement.rb, line 35
35:     def self.force_xmlns(force)
36:       @@force_xmlns = force
37:     end

Whether this element is always built with an xmlns attribute

[Source]

    # File lib/xmpp4r/xmppelement.rb, line 41
41:     def self.force_xmlns?
42:       @@force_xmlns
43:     end

Import another REXML::Element descendant to:

  • Either an element class that registered with name and xmlns before
  • Or if none was found to the class itself (you may call this class method on a deriving class)

[Source]

    # File lib/xmpp4r/xmppelement.rb, line 86
86:     def self.import(element)
87:       klass = class_for_name_xmlns(element.name, element.namespace)
88:       if klass != self and klass.ancestors.include?(self)
89:         klass.new.import(element)
90:       else
91:         self.new.import(element)
92:       end
93:     end

Specify XML element name and xmlns for a deriving class, this pair and the class will be added to a global pool

If the namespace is nil the class is a "wildcard class" matching elements with any xmlns if no other class with that namespace was defined

[Source]

    # File lib/xmpp4r/xmppelement.rb, line 29
29:     def self.name_xmlns(name, xmlns=nil)
30:       @@name_xmlns_classes[[name, xmlns]] = self
31:     end

Find the name and namespace for a given class. This class must have registered these two values by calling name_xmlns at definition time.

Raises an exception if none was found

klass:[Class]
result:[String, String] name and namespace

[Source]

    # File lib/xmpp4r/xmppelement.rb, line 53
53:     def self.name_xmlns_for_class(klass)
54:       klass.ancestors.each do |klass1|
55:         @@name_xmlns_classes.each do |name_xmlns,k|
56:           if klass1 == k
57:             return name_xmlns
58:           end
59:         end
60:       end
61: 
62:       raise NoNameXmlnsRegistered.new(klass)
63:     end

Initialize this element, which will then be initialized with the name registered with name_xmlns.

[Source]

     # File lib/xmpp4r/xmppelement.rb, line 98
 98:     def initialize(*arg)
 99:       if arg.empty?
100:         name, xmlns = self.class::name_xmlns_for_class(self.class)
101:         super(name)
102:         if self.class::force_xmlns?
103:           add_namespace(xmlns)
104:         end
105:       else
106:         super
107:       end
108:     end

Public Instance methods

[Source]

     # File lib/xmpp4r/xmppelement.rb, line 140
140:     def clone
141:       cloned = self.class.new
142:       cloned.add_attributes self.attributes.clone
143:       cloned.context = @context
144:       cloned
145:     end

[Source]

     # File lib/xmpp4r/xmppelement.rb, line 128
128:     def parent=(new_parent)
129:       if parent and parent.namespace('') == namespace('') and attributes['xmlns'].nil?
130:         add_namespace parent.namespace('')
131:       end
132: 
133:       super
134: 
135:       if new_parent and new_parent.namespace('') == namespace('')
136:         delete_namespace
137:       end
138:     end

Set XML language attribute (chainable)

[Source]

     # File lib/xmpp4r/xmppelement.rb, line 162
162:     def set_xml_lang(l)
163:       self.xml_lang = l
164:       self
165:     end

Add a child element which will be imported according to the child‘s name and xmlns

element:[REXML::Element] Child
result:[REXML::Element or descendant of XMPPElement] New child

[Source]

     # File lib/xmpp4r/xmppelement.rb, line 115
115:     def typed_add(element)
116:       if element.kind_of? REXML::Element
117:         element_ns = (element.namespace.to_s == '') ? namespace : element.namespace
118: 
119:         klass = XMPPElement::class_for_name_xmlns(element.name, element_ns)
120:         if klass != element.class
121:           element = klass.import(element)
122:         end
123:       end
124: 
125:       super(element)
126:     end

Generic XML attribute ‘xml:lang’ (REXML provides no shortcut)

[Source]

     # File lib/xmpp4r/xmppelement.rb, line 150
150:     def xml_lang
151:       attributes['xml:lang']
152:     end

Set XML language attribute

[Source]

     # File lib/xmpp4r/xmppelement.rb, line 156
156:     def xml_lang=(l)
157:       attributes['xml:lang'] = l
158:     end

[Validate]