| 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:
Find a class for given name and namespace
| name: | [String] |
| xmlns: | [String] |
| result: | A descendant of XMPPElement or REXML::Element |
# File lib/xmpp4r/xmppelement.rb, line 75
75: def self.class_for_name_xmlns(name, xmlns)
76: if @@name_xmlns_classes.has_key? [name, xmlns]
77: @@name_xmlns_classes[[name, xmlns]]
78: elsif @@name_xmlns_classes.has_key? [name, nil]
79: @@name_xmlns_classes[[name, nil]]
80: else
81: REXML::Element
82: end
83: end
Set whether this element is always built with an xmlns attribute
# File lib/xmpp4r/xmppelement.rb, line 40
40: def self.force_xmlns(force)
41: @@force_xmlns = force
42: end
Whether this element is always built with an xmlns attribute
# File lib/xmpp4r/xmppelement.rb, line 46
46: def self.force_xmlns?
47: @@force_xmlns
48: end
Import another REXML::Element descendant to:
# File lib/xmpp4r/xmppelement.rb, line 91
91: def self.import(element)
92: klass = class_for_name_xmlns(element.name, element.namespace)
93: if klass != self and klass.ancestors.include?(self)
94: klass.new.import(element)
95: else
96: self.new.import(element)
97: end
98: 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
# File lib/xmpp4r/xmppelement.rb, line 34
34: def self.name_xmlns(name, xmlns=nil)
35: @@name_xmlns_classes[[name, xmlns]] = self
36: 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 |
# File lib/xmpp4r/xmppelement.rb, line 58
58: def self.name_xmlns_for_class(klass)
59: klass.ancestors.each do |klass1|
60: @@name_xmlns_classes.each do |name_xmlns,k|
61: if klass1 == k
62: return name_xmlns
63: end
64: end
65: end
66:
67: raise NoNameXmlnsRegistered.new(klass)
68: end
Initialize this element, which will then be initialized with the name registered with name_xmlns.
# File lib/xmpp4r/xmppelement.rb, line 103
103: def initialize(*arg)
104: if arg.empty?
105: name, xmlns = self.class::name_xmlns_for_class(self.class)
106: super(name)
107: if self.class::force_xmlns?
108: add_namespace(xmlns)
109: end
110: else
111: super
112: end
113: end
# File lib/xmpp4r/xmppelement.rb, line 133
133: def parent=(new_parent)
134: if parent and parent.namespace('') == namespace('') and attributes['xmlns'].nil?
135: add_namespace parent.namespace('')
136: end
137:
138: super
139:
140: if new_parent and new_parent.namespace('') == namespace('')
141: delete_namespace
142: end
143: 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 |
# File lib/xmpp4r/xmppelement.rb, line 120
120: def typed_add(element)
121: if element.kind_of? REXML::Element
122: element_ns = (element.namespace.to_s == '') ? namespace : element.namespace
123:
124: klass = XMPPElement::class_for_name_xmlns(element.name, element_ns)
125: if klass != element.class
126: element = klass.import(element)
127: end
128: end
129:
130: super(element)
131: end