Class: Jabber::Protocol::ParsedXMLElement
- Inherits:
-
Object
- Object
- Jabber::Protocol::ParsedXMLElement
- Defined in:
- lib/jabber4r/protocol.rb
Overview
This class is constructed from XML data elements that are received from the Jabber service.
Defined Under Namespace
Classes: NilParsedXMLElement
Instance Attribute Summary collapse
-
#element_children ⇒ Object
readonly
A hash of ParsedXMLElement children.
-
#element_data ⇒ Object
readonly
The data <tag>data</tag> for a tag.
-
#element_parent ⇒ Object
readonly
The parent ParsedXMLElement.
-
#element_tag ⇒ Object
readonly
The <tag> as String.
Instance Method Summary collapse
-
#[](number) ⇒ Object
Calls the parent’s element_children (hash) index off of this elements tag and gets the supplied index.
-
#add_attribute(name, value) ⇒ Object
Add the attribute to the element <tag name=“value”>data</tag>.
-
#add_child(tag) ⇒ Object
Factory to build a child element from this element with the given tag.
-
#append_data(data) ⇒ Object
Appends data to the element.
-
#consume_element ⇒ Object
When an xml is received from the Jabber service and a ParsedXMLElement is created, it is propogated to all filters and listeners.
-
#count ⇒ Object
Returns the count of siblings with this element’s tag.
-
#element_consumed? ⇒ Boolean
Checks if the element is consumed.
-
#initialize(tag, parent = nil) ⇒ ParsedXMLElement
constructor
Construct an instance for the given tag.
-
#method_missing(methId, *args) ⇒ Object
Overrides to allow for directly accessing child elements and attributes.
-
#size ⇒ Object
see _count.
-
#to_s ⇒ Object
Returns the valid XML as a string.
Constructor Details
#initialize(tag, parent = nil) ⇒ ParsedXMLElement
Construct an instance for the given tag
- tag
- String
-
The tag
- parent
- Jabber::Protocol::ParsedXMLElement = nil
-
The parent element
1071 1072 1073 1074 1075 1076 1077 |
# File 'lib/jabber4r/protocol.rb', line 1071 def initialize(tag, parent=nil) @element_tag = tag @element_parent = parent @element_children = {} @attributes = {} @element_consumed = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(methId, *args) ⇒ Object
Overrides to allow for directly accessing child elements and attributes. If prefaced by attr_ it looks for an attribute that matches or checks for a child with a tag that matches the method name. If no match occurs, it returns a NilParsedXMLElement (singleton) instance.
- Example
-
<alpha number=“1”><beta number=“2”>Beta Data</beta></alpha>
element.element_tag #=> alpha
element.attr_number #=> 1
element.beta.element_data #=> Beta Data
1178 1179 1180 1181 1182 1183 1184 1185 1186 |
# File 'lib/jabber4r/protocol.rb', line 1178 def method_missing(methId, *args) tag = methId.id2name if tag[0..4]=="attr_" return @attributes[tag[5..-1]] end list = @element_children[tag] return list[0] if list return NilParsedXMLElement.instance end |
Instance Attribute Details
#element_children ⇒ Object (readonly)
A hash of ParsedXMLElement children
1060 1061 1062 |
# File 'lib/jabber4r/protocol.rb', line 1060 def element_children @element_children end |
#element_data ⇒ Object (readonly)
The data <tag>data</tag> for a tag
1063 1064 1065 |
# File 'lib/jabber4r/protocol.rb', line 1063 def element_data @element_data end |
#element_parent ⇒ Object (readonly)
The parent ParsedXMLElement
1057 1058 1059 |
# File 'lib/jabber4r/protocol.rb', line 1057 def element_parent @element_parent end |
#element_tag ⇒ Object (readonly)
The <tag> as String
1054 1055 1056 |
# File 'lib/jabber4r/protocol.rb', line 1054 def element_tag @element_tag end |
Instance Method Details
#[](number) ⇒ Object
Calls the parent’s element_children (hash) index off of this elements tag and gets the supplied index. In this sense it gets its sibling based on offset.
- number
- Integer
-
The number of the sibling to get
- return
- Jabber::Protocol::ParsedXMLElement
-
The sibling element
1144 1145 1146 |
# File 'lib/jabber4r/protocol.rb', line 1144 def [](number) return @element_parent.element_children[@element_tag][number] if @element_parent end |
#add_attribute(name, value) ⇒ Object
Add the attribute to the element
<tag name="value">data</tag>
- name
- String
-
The attribute name
- value
- String
-
The attribute value
- return
- Jabber::Protocol::ParsedXMLElement
-
self for chaining
1087 1088 1089 1090 |
# File 'lib/jabber4r/protocol.rb', line 1087 def add_attribute(name, value) @attributes[name]=value self end |
#add_child(tag) ⇒ Object
Factory to build a child element from this element with the given tag
- tag
- String
-
The tag name
- return
- Jabber::Protocol::ParsedXMLElement
-
The newly created child element
1098 1099 1100 1101 1102 1103 |
# File 'lib/jabber4r/protocol.rb', line 1098 def add_child(tag) child = ParsedXMLElement.new(tag, self) @element_children[tag] = Array.new if not @element_children.has_key? tag @element_children[tag] << child return child end |
#append_data(data) ⇒ Object
Appends data to the element
- data
- String
-
The data to append
- return
- Jabber::Protocol::ParsedXMLElement
-
self for chaining
1130 1131 1132 1133 1134 |
# File 'lib/jabber4r/protocol.rb', line 1130 def append_data(data) @element_data = "" unless @element_data @element_data += data self end |
#consume_element ⇒ Object
When an xml is received from the Jabber service and a ParsedXMLElement is created, it is propogated to all filters and listeners. Any one of those can consume the element to prevent its propogation to other filters or listeners. This method marks the element as consumed.
1111 1112 1113 |
# File 'lib/jabber4r/protocol.rb', line 1111 def consume_element @element_consumed = true end |
#count ⇒ Object
Returns the count of siblings with this element’s tag
- return
- Integer
-
The number of sibling elements
1153 1154 1155 1156 |
# File 'lib/jabber4r/protocol.rb', line 1153 def count return @element_parent.element_children[@element_tag].size if @element_parent return 0 end |
#element_consumed? ⇒ Boolean
Checks if the element is consumed
- return
- Boolean
-
True if the element is consumed
1120 1121 1122 |
# File 'lib/jabber4r/protocol.rb', line 1120 def element_consumed? @element_consumed end |
#size ⇒ Object
see _count
1161 1162 1163 |
# File 'lib/jabber4r/protocol.rb', line 1161 def size count end |
#to_s ⇒ Object
Returns the valid XML as a string
- return
- String
-
XML string
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 |
# File 'lib/jabber4r/protocol.rb', line 1192 def to_s begin result = "\n<#{@element_tag}" @attributes.each {|key, value| result += (' '+key+'="'+value+'"') } if @element_children.size>0 or @element_data result += ">" else result += "/>" end result += @element_data if @element_data @element_children.each_value {|array| array.each {|je| result += je.to_s} } result += "\n" if @element_children.size>0 result += "</#{@element_tag}>" if @element_children.size>0 or @element_data result rescue => exception puts exception.to_s end end |