Class: Wiris::Xml

Inherits:
Object
  • Object
show all
Includes:
Comparable, REXML
Defined in:
lib/src-generic/Xml.rb

Constant Summary collapse

Element =
:element
PCData =
:text
CData =
:text

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Xml

Returns a new instance of Xml.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/src-generic/Xml.rb', line 34

def initialize(node)
	@node =node
	@nodeType = node.node_type
	if node.class == REXML::CData
		@nodeType = :text
	end
	if (@nodeType == :text)
		@nodeName = :text
	elsif (@nodeType == :text)
		@nodeName = :text
	else
		@nodeName = node.name
	end

end

Class Method Details

.createDocumentObject



148
149
150
# File 'lib/src-generic/Xml.rb', line 148

def self.createDocument()
	return Xml.new REXML::Document.new
end

.parse(xml) ⇒ Object



49
50
51
# File 'lib/src-generic/Xml.rb', line 49

def self.parse(xml)
	return Xml.new REXML::Document.new xml
end

Instance Method Details

#addChild(x) ⇒ Object



126
127
128
# File 'lib/src-generic/Xml.rb', line 126

def addChild(x)
	@node << x.node
end

#attributesObject



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/src-generic/Xml.rb', line 74

def attributes()
    atts = Array.new;
    if @node.attributes.nil?
         return Iterator.new
    end

    @node.attributes.each_attribute { |attr|
				atts.push(attr.expanded_name)
			}

    return Iterator.new atts.each
end

#createCData_(text) ⇒ Object



170
171
172
173
# File 'lib/src-generic/Xml.rb', line 170

def createCData_(text)
	elem = REXML::CData.new(text)
	return Xml.new elem
end

#createElement_(name) ⇒ Object



152
153
154
155
# File 'lib/src-generic/Xml.rb', line 152

def createElement_(name)
	elementNode = REXML::Element.new name
	return Xml.new elementNode
end

#createPCData_(text) ⇒ Object



157
158
159
160
# File 'lib/src-generic/Xml.rb', line 157

def createPCData_(text)
	textNode = Text.new text
	return Xml.new textNode
end

#elementsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/src-generic/Xml.rb', line 57

def elements()
	childElements = Array.new
	node = nil
	if (self.firstChild() != nil)
		node = self.firstChild().node
	end

	while (!node.nil?)
		if (node.node_type == :element)
			childElements.push(node)
		end
	node = node.next_sibling
	end
	# return XmlIterator.new(childElements.each)
	return XmlIterator.new childElements.each
end

#firstChildObject



109
110
111
112
113
114
115
# File 'lib/src-generic/Xml.rb', line 109

def firstChild()
	if defined?(@node.each_child).nil? || @node.each_child.count == 0
		return nil
	end

	return Xml.new @node.each_child.next
end

#firstElementObject



117
118
119
120
121
122
123
124
# File 'lib/src-generic/Xml.rb', line 117

def firstElement()
	@node.each_recursive {|e|
				if e.class == REXML::Element
					return Xml.new e
				end
	}
	return nil
end

#get(attributeName) ⇒ Object



87
88
89
# File 'lib/src-generic/Xml.rb', line 87

def get(attributeName)
    return @node.attributes[attributeName]
end

#getNodeValue_Object



162
163
164
165
166
167
168
# File 'lib/src-generic/Xml.rb', line 162

def getNodeValue_()
	if (@node.node_type == :text || @node.node_type == :node)
		return @node.value
	else
		return @node.text
	end
end

#insertChild(x, pos) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/src-generic/Xml.rb', line 130

def insertChild(x, pos)
	if pos < 0
	end
	# On ruby first index starts at 1.
	# Check if node has childs, if not insert node directly.
	if @node.elements[1].nil?
		xmlparsed.firstChild.addChild(x)
	else
		@node.insert_before(@node.elements[1], x.node)
	end
end

#iteratorObject



53
54
55
# File 'lib/src-generic/Xml.rb', line 53

def iterator()
	return XmlIterator.new(@node.each)
end

#nodeObject



16
17
18
# File 'lib/src-generic/Xml.rb', line 16

def node
	@node
end

#node=(node) ⇒ Object



13
14
15
# File 'lib/src-generic/Xml.rb', line 13

def node=(node)
	@node=node
end

#nodeNameObject



23
24
25
# File 'lib/src-generic/Xml.rb', line 23

def nodeName
	@nodeName
end

#nodeName=(nodeName) ⇒ Object



20
21
22
# File 'lib/src-generic/Xml.rb', line 20

def nodeName=(nodeName)
	@nodeName=nodeName
end

#nodeTypeObject



30
31
32
# File 'lib/src-generic/Xml.rb', line 30

def nodeType
	@nodeType
end

#nodeType=(nodeType) ⇒ Object



27
28
29
# File 'lib/src-generic/Xml.rb', line 27

def nodeType=(nodeType)
	@nodeType=nodeType
end

#parent_Object



102
103
104
105
106
107
# File 'lib/src-generic/Xml.rb', line 102

def parent_()
	if @node.parent.nil?
		return nil
	end
	return Xml.new @node.parent
end

#remove(attributeName) ⇒ Object



91
92
93
94
95
# File 'lib/src-generic/Xml.rb', line 91

def remove(attributeName)
	if !@node.attributes[attributeName].nil?
		@node.attributes.delete attributeName
	end
end

#removeChild(x) ⇒ Object



141
142
143
144
145
146
# File 'lib/src-generic/Xml.rb', line 141

def removeChild(x)
	remove = @node == x.node.parent
	if remove
		x.node.remove
	end
end

#set(attributeName, attributeValue) ⇒ Object



97
98
99
100
# File 'lib/src-generic/Xml.rb', line 97

def set(attributeName, attributeValue)
    newAtrribute = REXML::Attribute.new(attributeName, attributeValue);
    @node.attributes.add(newAtrribute)
end

#toStringObject



175
176
177
# File 'lib/src-generic/Xml.rb', line 175

def toString()
	return @node.to_s
end