Class: Jabber::Protocol::ParsedXMLElement

Inherits:
Object
  • Object
show all
Defined in:
lib/jabber4r/protocol/parsed_xml_element.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

Instance Method Summary collapse

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



68
69
70
71
72
73
74
# File 'lib/jabber4r/protocol/parsed_xml_element.rb', line 68

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


175
176
177
178
179
180
181
182
183
# File 'lib/jabber4r/protocol/parsed_xml_element.rb', line 175

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_childrenObject (readonly)

A hash of ParsedXMLElement children



57
58
59
# File 'lib/jabber4r/protocol/parsed_xml_element.rb', line 57

def element_children
  @element_children
end

#element_dataObject (readonly)

The data <tag>data</tag> for a tag



60
61
62
# File 'lib/jabber4r/protocol/parsed_xml_element.rb', line 60

def element_data
  @element_data
end

#element_parentObject (readonly)

The parent ParsedXMLElement



54
55
56
# File 'lib/jabber4r/protocol/parsed_xml_element.rb', line 54

def element_parent
  @element_parent
end

#element_tagObject (readonly)

The <tag> as String



51
52
53
# File 'lib/jabber4r/protocol/parsed_xml_element.rb', line 51

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



141
142
143
# File 'lib/jabber4r/protocol/parsed_xml_element.rb', line 141

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



84
85
86
87
# File 'lib/jabber4r/protocol/parsed_xml_element.rb', line 84

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



95
96
97
98
99
100
# File 'lib/jabber4r/protocol/parsed_xml_element.rb', line 95

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



127
128
129
130
131
# File 'lib/jabber4r/protocol/parsed_xml_element.rb', line 127

def append_data(data)
  @element_data = "" unless @element_data
  @element_data += data
  self
end

#consume_elementObject

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.



108
109
110
# File 'lib/jabber4r/protocol/parsed_xml_element.rb', line 108

def consume_element
  @element_consumed = true
end

#countObject

Returns the count of siblings with this element’s tag

return
Integer

The number of sibling elements



150
151
152
153
# File 'lib/jabber4r/protocol/parsed_xml_element.rb', line 150

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

Returns:

  • (Boolean)


117
118
119
# File 'lib/jabber4r/protocol/parsed_xml_element.rb', line 117

def element_consumed?
  @element_consumed
end

#sizeObject

see _count



158
159
160
# File 'lib/jabber4r/protocol/parsed_xml_element.rb', line 158

def size
  count
end

#to_sObject

Returns the valid XML as a string

return
String

XML string



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/jabber4r/protocol/parsed_xml_element.rb', line 189

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