Removed 'base_path' from AkInstaller->setInstalledVersion because Ak:make_dir can...
[akelos.git] / vendor / domit / xml_domit_nodemaps.php
blobe8cb35b46c1cfba87f3ea8a1d222055860fbaf91
1 <?php
2 /**
3 * DOMIT node maps are structures for storing and accessing collections of DOMIT_Nodes.
4 * @package domit-xmlparser
5 * @copyright (C) 2004 John Heinstein. All rights reserved
6 * @license http://www.gnu.org/copyleft/lesser.html LGPL License
7 * @author John Heinstein <johnkarl@nbnet.nb.ca>
8 * @link http://www.engageinteractive.com/domit/ DOMIT! Home Page
9 * DOMIT! is Free Software
10 **/
12 if (!defined('DOMIT_INCLUDE_PATH')) {
13 define('DOMIT_INCLUDE_PATH', (dirname(__FILE__) . "/"));
16 /**
17 * A DOM NodeList implementation
19 * @package domit-xmlparser
20 * @author John Heinstein <johnkarl@nbnet.nb.ca>
22 class DOMIT_NodeList {
23 /** @var Array A container for the nodes in the list */
24 var $arNodeList = array();
26 /**
27 * Return the node at the specified index
28 * @param int The index of the requested node
29 * @return Object A reference to the requested node, or null
31 function &item($index) {
32 if ($index < $this->getLength()) {
33 return $this->arNodeList[$index];
35 return null;
36 } //item
38 /**
39 * Returns the number of nodes in the list
40 * @return int The number of nodes in the list
42 function getLength() {
43 return count($this->arNodeList);
44 } //getLength
46 /**
47 * Appends a node to the list
48 * @return Object The appended node
50 function &appendNode(&$node) {
51 $this->arNodeList[] =& $node;
52 return $node;
53 } //appendNode
55 /**
56 * Removes the specified node from the list
57 * @param Object A reference to the node to be removed
58 * @return Object A reference to the removed node
60 function &removeNode(&$node) {
61 $total = $this->getLength();
62 $returnNode = null;
63 $found = false;
65 for ($i = 0; $i < $total; $i++) {
66 if (!$found) {
67 if ($node->uid == $this->arNodeList[$i]->uid) {
68 $found = true;
69 $returnNode=& $node;
73 if ($found) {
74 if ($i == ($total - 1)) {
75 unset($this->arNodeList[$i]);
77 else {
78 $this->arNodeList[$i] =& $this->arNodeList[($i + 1)];
83 return $returnNode;
84 } //$removeNode
86 /**
87 * Formats a string for presentation as HTML
88 * @param string The string to be formatted
89 * @param boolean True if the string is to be sent directly to output
90 * @return string The HTML formatted string
92 function forHTML($str, $doPrint = false) {
93 require_once(DOMIT_INCLUDE_PATH . 'xml_domit_utilities.php');
94 return DOMIT_Utilities::forHTML($str, $doPrint);
95 } //forHTML
97 /**
98 * Generates an array representation of the node and its children
99 * @return Array A representation of the node and its children
101 function toArray() {
102 return $this->arNodeList;
103 } //toArray
106 * Copies a node and/or its children
107 * @param boolean True if all child nodes are also to be cloned
108 * @return Object A copy of the node and/or its children
110 function &createClone($deep = false) {
111 $className = get_class($this);
112 $clone =& new $className();
114 foreach ($this->arNodeList as $key => $value) {
115 $currNode =& $this->arNodeList[$key];
116 $clone->arNodeList[$key] =& $currNode->cloneNode($deep);
119 return $clone;
120 } //createClone
123 * Generates a string representation of the node and its children
124 * @param boolean True if HTML readable output is desired
125 * @param boolean True if illegal xml characters in text nodes and attributes should be converted to entities
126 * @return string The string representation
128 function toString($htmlSafe = false, $subEntities=false) {
129 $result = '';
131 foreach ($this->arNodeList as $key => $value) {
132 $currNode =& $this->arNodeList[$key];
133 $result .= $currNode->toString(false, $subEntities);
136 if ($htmlSafe) $result = $this->forHTML($result);
138 return $result;
139 } //toString
140 } //DOMIT_NodeList
143 * A DOM NamedNodeMap implementation
145 * @package domit-xmlparser
146 * @author John Heinstein <johnkarl@nbnet.nb.ca>
148 class DOMIT_NamedNodeMap {
149 /** @var Array A container for the nodes in the map */
150 var $arNodeMap = array();
151 /** @var Array A numerical index to the keys of the mapped nodes */
152 var $indexedNodeMap = array();
153 /** @var boolean True if the list has been modified and $indexedNodeMap needs reindexing */
154 var $isDirty = true;
157 * Gets a node with the specifed name
158 * @param string The name of the node
159 * @return mixed A reference to the requested node, or null
161 function &getNamedItem($name) {
162 if (isset($this->arNodeMap[$name])) {
163 return $this->arNodeMap[$name];
166 return null;
167 } //getNamedItem
170 * Reindexes the numerical index for the named node map
172 function reindexNodeMap() {
173 $this->indexedNodeMap = array();
175 foreach ($this->arNodeMap as $key => $value) {
176 $this->indexedNodeMap[] = $key;
179 $this->isDirty = false;
180 } //reindexNodeMap
183 * Assigns a node to the list
184 * @param Object A reference to the node to be assigned
185 * @return Object A reference to the assigned node
187 function &setNamedItem(&$arg) {
188 $returnNode = null;
190 if (isset($this->arNodeMap[$arg->nodeName])) {
191 $returnNode =& $this->arNodeMap[$arg->nodeName];
193 else {
194 $this->isDirty = true;
197 $this->arNodeMap[$arg->nodeName] =& $arg;
198 return $returnNode;
199 } //setNamedItem
202 * Removes a node from the list, by name
203 * @param string The name of the node to be removed
204 * @return mixed A reference to the removed node, or null
206 function &removeNamedItem($name) {
207 $returnNode = null;
209 if (isset($this->arNodeMap[$name])) {
210 $returnNode =& $this->arNodeMap[$name];
211 unset($this->arNodeMap[$name]);
212 $this->isDirty = true;
215 return $returnNode;
216 } //removeNamedItem
219 * Gets a node with the specifed name, taking into account namespaces
220 * @param string The namespaceURI of the node
221 * @param string The localName of the node
222 * @return mixed A reference to the requested node, or null
224 function &getNamedItemNS($namespaceURI, $localName) {
225 $key = $this->getKeyNS($namespaceURI, $localName);
227 if (isset($this->arNodeMap[$key])) {
228 return $this->arNodeMap[$key];
231 return null;
232 } //getNamedItemNS
235 * Assigns a node to the list, using its namespaceURI and localName
236 * @param Object A reference to the node to be assigned
237 * @return Object A reference to the assigned node
239 function &setNamedItemNS(&$arg) {
240 $returnNode = null;
241 $key = $this->getKeyNS($arg->namespaceURI, $arg->localName);
243 if (isset($this->arNodeMap[$key])) {
244 $returnNode =& $this->arNodeMap[$key];
246 else {
247 $this->isDirty = true;
250 $this->arNodeMap[$key] =& $arg;
251 return $returnNode;
252 } //setNamedItemNS
255 * Removes a node from the list, by name, by local name and namespace URI
256 * @param string The namespaceURI of the node to be removed
257 * @param string The localName of the node to be removed
258 * @return mixed A reference to the removed node, or null
260 function &removeNamedItemNS($namespaceURI, $localName) {
261 $returnNode = null;
262 $key = $this->getKeyNS($namespaceURI, $localName);
264 if (isset($this->arNodeMap[$key])) {
265 $returnNode =& $this->arNodeMap[$key];
266 unset($this->arNodeMap[$key]);
267 $this->isDirty = true;
270 return $returnNode;
271 } //removeNamedItemNS
274 * Returns the key of the NamedNodeMap, given the namespaceURI and localName
275 * @param string The namespaceURI of the node to be removed
276 * @param string The localName of the node to be removed
277 * @return string The key of the NamedNodeMap
279 function getKeyNS($namespaceURI, $localName) {
280 if ($namespaceURI != '') {
281 return $namespaceURI . ":" . $localName;
284 return $localName;
285 } //getKeyNS
288 * Return the node at the specified index
289 * @param int The index of the requested node
290 * @return mixed A reference to the requested node, or null
292 function &item($index) {
293 if ($this->isDirty) $this->reindexNodeMap();
294 return $this->arNodeMap[$this->indexedNodeMap[$index]];
295 } //item
298 * Returns the number of nodes in the map
299 * @return int The number of nodes in the map
301 function getLength() {
302 return count($this->arNodeMap);
303 } //getLength
306 * Formats a string for presentation as HTML
307 * @param string The string to be formatted
308 * @param boolean True if the string is to be sent directly to output
309 * @return string The HTML formatted string
311 function forHTML($str, $doPrint = false) {
312 require_once(DOMIT_INCLUDE_PATH . 'xml_domit_utilities.php');
313 return DOMIT_Utilities::forHTML($str, $doPrint);
314 } //forHTML
317 * Generates an array representation of the node and its children
318 * @return Array A representation of the node and its children
320 function toArray() {
321 return $this->arNodeMap;
322 } //toArray
325 * Copies a node and/or its children
326 * @param boolean True if all child nodes are also to be cloned
327 * @return Object A copy of the node and/or its children
329 function &createClone($deep = false) {
330 $className = get_class($this);
331 $clone =& new $className();
333 foreach ($this->arNodeMap as $key => $value) {
334 $currNode =& $this->arNodeMap[$key];
335 $clone->arNodeMap[$key] =& $currNode->cloneNode($deep);
338 return $clone;
339 } //createClone
342 * Generates a string representation of the node and its children
343 * @param boolean True if HTML readable output is desired
344 * @param boolean True if illegal xml characters in text nodes and attributes should be converted to entities
345 * @return string The string representation
347 function toString($htmlSafe = false, $subEntities=false) {
348 $result = '';
350 foreach ($this->arNodeMap as $key => $value) {
351 $currNode =& $this->arNodeMap[$key];
352 $result .= $currNode->toString(false, $subEntities);
355 if ($htmlSafe) $result = $this->forHTML($result);
357 return $result;
358 } //toString
359 } //DOMIT_NamedNodeMap
362 * A NamedNodeMap with specialized functionality for Attribute nodes
364 * @package domit-xmlparser
365 * @author John Heinstein <johnkarl@nbnet.nb.ca>
367 class DOMIT_NamedNodeMap_Attr extends DOMIT_NamedNodeMap {
369 * Generates an array representation of the node and its children
370 * @return Array A representation of the node and its children
372 function toArray() {
373 $arReturn = array();
375 foreach ($this->arNodeMap as $key => $value) {
376 $arReturn[$key] = $this->arNodeMap[$key]->getValue();
379 return $arReturn;
380 } //toArray
383 * Generates a string representation of the node and its children
384 * @param boolean True if HTML readable output is desired
385 * @param boolean True if illegal xml characters in text nodes and attributes should be converted to entities
386 * @return string The string representation
388 function toString($htmlSafe = false, $subEntities=false) {
389 $result = '';
391 foreach ($this->arNodeMap as $key => $value) {
392 $currNode =& $this->arNodeMap[$key];
393 $result .= $currNode->toString(false, $subEntities);
396 if ($htmlSafe) $result = $this->forHTML($result);
398 return $result;
399 } //toString
400 } //DOMIT_NamedNodeMap_Attr