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
12 if (!defined('DOMIT_INCLUDE_PATH')) {
13 define('DOMIT_INCLUDE_PATH', (dirname(__FILE__
) . "/"));
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();
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];
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
);
47 * Appends a node to the list
48 * @return Object The appended node
50 function &appendNode(&$node) {
51 $this->arNodeList
[] =& $node;
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();
65 for ($i = 0; $i < $total; $i++
) {
67 if ($node->uid
== $this->arNodeList
[$i]->uid
) {
74 if ($i == ($total - 1)) {
75 unset($this->arNodeList
[$i]);
78 $this->arNodeList
[$i] =& $this->arNodeList
[($i +
1)];
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);
98 * Generates an array representation of the node and its children
99 * @return Array A representation of the node and its children
102 return $this->arNodeList
;
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);
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) {
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);
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 */
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];
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;
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) {
190 if (isset($this->arNodeMap
[$arg->nodeName
])) {
191 $returnNode =& $this->arNodeMap
[$arg->nodeName
];
194 $this->isDirty
= true;
197 $this->arNodeMap
[$arg->nodeName
] =& $arg;
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) {
209 if (isset($this->arNodeMap
[$name])) {
210 $returnNode =& $this->arNodeMap
[$name];
211 unset($this->arNodeMap
[$name]);
212 $this->isDirty
= true;
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];
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) {
241 $key = $this->getKeyNS($arg->namespaceURI
, $arg->localName
);
243 if (isset($this->arNodeMap
[$key])) {
244 $returnNode =& $this->arNodeMap
[$key];
247 $this->isDirty
= true;
250 $this->arNodeMap
[$key] =& $arg;
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) {
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;
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;
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]];
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
);
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);
317 * Generates an array representation of the node and its children
318 * @return Array A representation of the node and its children
321 return $this->arNodeMap
;
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);
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) {
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);
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
375 foreach ($this->arNodeMap
as $key => $value) {
376 $arReturn[$key] = $this->arNodeMap
[$key]->getValue();
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) {
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);
400 } //DOMIT_NamedNodeMap_Attr