Class: Jabber::Protocol::XMLElement

Inherits:
Object
  • Object
show all
Defined in:
lib/jabber4r/protocol/xml_element.rb

Overview

Utility class to create valid XML strings

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, attributes = {}) ⇒ XMLElement

Construct an XMLElement for the supplied tag and attributes

tag
String

XML tag

attributes
Hash = {}

The attribute hash=value



21
22
23
24
25
26
# File 'lib/jabber4r/protocol/xml_element.rb', line 21

def initialize(tag, attributes={})
  @tag = tag
  @elements = []
  @attributes = attributes
  @data = ""
end

Instance Attribute Details

#parentObject

The parent XMLElement



14
15
16
# File 'lib/jabber4r/protocol/xml_element.rb', line 14

def parent
  @parent
end

Instance Method Details

#add_attribute(attrib, value) ⇒ Object

Adds an attribute to this element

attrib
String

The attribute name

value
String

The attribute value

return
Jabber::Protocol::XMLElement

self for chaining



35
36
37
38
# File 'lib/jabber4r/protocol/xml_element.rb', line 35

def add_attribute(attrib, value)
  @attributes[attrib]=value
  self
end

#add_cdata(cdata) ⇒ Object

Adds cdata to this element

cdata
String

The cdata to add

return
Jabber::Protocol::XMLElement

self for chaining



68
69
70
71
# File 'lib/jabber4r/protocol/xml_element.rb', line 68

def add_cdata(cdata)
  @data += "<![CDATA[#{cdata.to_s}]]>"
  self
end

#add_child(tag, attributes = {}) ⇒ Object

Adds a child to this element of the supplied tag

tag
String

The element tag

attributes
Hash = {}

The attributes hash=value

return
Jabber::Protocol::XMLElement

newly created child element



89
90
91
92
93
94
# File 'lib/jabber4r/protocol/xml_element.rb', line 89

def add_child(tag, attributes={})
  child = XMLElement.new(tag, attributes)
  child.parent = self
  @elements << child
  return child
end

#add_data(data) ⇒ Object

Adds data to this element

data
String

The data to add

return
Jabber::Protocol::XMLElement

self for chaining



46
47
48
49
# File 'lib/jabber4r/protocol/xml_element.rb', line 46

def add_data(data)
  @data += data.to_s
  self
end

#add_xml(xml) ⇒ Object

Adds arbitrary XML data to this object

xml
String

the xml to add



101
102
103
# File 'lib/jabber4r/protocol/xml_element.rb', line 101

def add_xml(xml)
  @xml = xml
end

#set_namespace(ns) ⇒ Object

Sets the namespace for this tag

ns
String

The namespace

return
Jabber::Protocol::XMLElement

self for chaining



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

def set_namespace(ns)
  @tag+=":#{ns}"
  self
end

#to_parentObject

Returns the parent element

return
Jabber::Protocol::XMLElement

The parent XMLElement



78
79
80
# File 'lib/jabber4r/protocol/xml_element.rb', line 78

def to_parent
  @parent
end

#to_sObject

Climbs to the top of this elements parent tree and then returns the to_xml XML string.

return
String

The XML string of this element (from the topmost parent).



139
140
141
142
# File 'lib/jabber4r/protocol/xml_element.rb', line 139

def to_s
  return @parent.to_s if @parent
  return to_xml(true)
end

#to_xml(format, indent = 0) ⇒ Object

Recursively builds the XML string by traversing this element’s children.

format
Boolean

True to pretty-print (format) the output string

indent
Integer = 0

The indent level (recursively more)



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/jabber4r/protocol/xml_element.rb', line 112

def to_xml(format, indent=0)
  result = ""
  result += " "*indent if format
  result += "<#{@tag}"
  @attributes.each {|attrib, value| result += (' '+attrib.to_s+'="'+value.to_s+'"') }
  if @data=="" and @elements.size==0
    result +="/>"
    result +="\n" if format
    return result
  end
  result += ">"
  result += "\n" if format and @data==""
  result += @data if @data!=""
  @elements.each {|element| result+=element.to_xml(format, indent+4)}
  result += @xml if not @xml.nil?
  result += " "*indent if format and @data==""
  result+="</#{@tag}>"
  result+="\n" if format
  return result
end