NodeClass.php
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* @class NodeClass
* @brief Generic definition of a node
* @details
*/
class NodeClass
{
private $name;
private $value;
private $attributes;
private $children;
public function __construct($name)
{
$this->name = $name;
$this->attributes = array();
$this->children = array();
$this->value = "";
}
public function getName()
{
return $this->name;
}
protected function setValue($val)
{
$this->value = $val;
}
protected function getValue()
{
return $this->value;
}
protected function setAttribute($attName, $attVal)
{
$this->attributes[$attName] = $attVal;
}
public function getAttribute($attName)
{
if (array_key_exists($attName,$this->attributes)) {
return $this->attributes[$attName];
}
return "";
}
protected function hasChildren()
{
return (count($this->children) != 0);
}
protected function addChild($child)
{
$this->children[] = $child;
}
protected function addChildBefore($child, $nodeName)
{
for ($i = 0; $i < count($this->children);++$i)
{
if ($this->children[$i]->getName() == $nodeName)
{
array_splice($this->children, $i, 0, array($child));
return;
}
}
$this->addChild($child);
}
public function getChildren()
{
return $this->children;
}
protected function getChildrenByName($name)
{
$result = array();
foreach ($this->children as $child)
if ($child->getName() == $name)
$result[] = $child;
return $result;
}
protected function getFirstChildByName($name)
{
foreach ($this->children as $child)
if ($child->getName() == $name)
return $child;
return NULL;
}
public function getChildInstanceByName($name, $createIfNoExist = false)
{
$node = $this->getFirstChildByName($name);
if (!isset($node) && $createIfNoExist)
{
$node = new NodeClass($name);
$this->addChild($node);
}
return $node;
}
/*
* @brief Export node to a XML node
*/
public function toXMLNode($doc)
{
$xmlNode = $doc->createElement($this->getName());
if ($this->getValue() != "")
$xmlNode->nodeValue = htmlspecialchars($this->getValue());
foreach ($this->attributes as $key => $value)
$xmlNode->setAttribute($key,$value);
foreach ($this->children as $child)
{
$xmlChildNode = $child->toXMLNode($doc);
$xmlNode->appendChild($xmlChildNode);
}
return $xmlNode;
}
/*
* @brief Method to load a XML node
*/
public function getXmlNodeAttribute($xmlNode, $attribute)
{
return $xmlNode->getAttribute($attribute);
}
public function getXmlNodeValue($xmlNode)
{
return $xmlNode->nodeValue;
}
public function getXmlNodeName($xmlNode)
{
return $xmlNode->nodeName;
}
public function getXmlNodeChildren($xmlNode)
{
return $xmlNode->childNodes;
}
public function getXmlNodeChildrenByTagName($xmlNode, $childrenName)
{
$result = array();
for ($n = $xmlNode->firstChild; $n !== null; $n = $n->nextSibling) {
if ($n instanceof DOMElement && $n->tagName == $childrenName) {
$result[] = $n;
}
}
return $result;
}
public function getXmlNodeChildByTagName($xmlNode, $childName)
{
$xmlChildrenNodes = $this->getXmlNodeChildrenByTagName($xmlNode, $childName);
if (count($xmlChildrenNodes) <= 0)
return NULL;
return $xmlChildrenNodes[0];
}
public function loadFromNode($xmlNode) {
/*//Attributes
//Value
if ($this->getXmlNodeChildren($xmlNode) == NULL) {
if ($this->getXmlNodeValue($xmlNode)) {
$result[$childXmlNode->nodeName] = $this->getXmlNodeValue($xmlNode);
}
return;
}
//Children
foreach ($this->getXmlNodeChildren($xmlNode) as $childXmlNode) {
echo $childXmlNode->nodeName.PHP_EOL;
$nodeObject = NodeFactory::getNodeObject ($childXmlNode->nodeName, $this->getContext());
if (isset($nodeObject)) {
$cloneNodeObject = clone $nodeObject;
if (!array_key_exists($childXmlNode->nodeName,$result))
$result[$childXmlNode->nodeName] = array();
$result[$childXmlNode->nodeName][] = array();
$cloneNodeObject->loadFromNode($childXmlNode, $result[$childXmlNode->nodeName][count($result[$childXmlNode->nodeName])-1]);
}
else
echo "[WARNING] ".$childXmlNode->nodeName." ".$this->getContext().PHP_EOL;
}*/
}
}
?>