baseline
[omp.pkp.sfu.ca.git] / lib / pkp / classes / xml / XMLNode.inc.php
blob13ace94d80c8848d5fbbb72a5e5e3348b967e64a
1 <?php
3 /**
4 * @file classes/xml/XMLNode.inc.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
9 * @class XMLNode
10 * @ingroup xml
12 * @brief Default handler for XMLParser returning a simple DOM-style object.
13 * This handler parses an XML document into a tree structure of XMLNode objects.
16 // $Id: XMLNode.inc.php,v 1.9 2009/09/23 00:42:47 asmecher Exp $
18 class XMLNode {
20 /** @var string the element (tag) name */
21 var $name;
23 /** @var XMLNode reference to the parent node (null if this is the root node) */
24 var $parent;
26 /** @var array the element's attributes */
27 var $attributes;
29 /** @var string the element's value */
30 var $value;
32 /** @var array references to the XMLNode children of this node */
33 var $children;
35 /**
36 * Constructor.
37 * @param $name element/tag name
39 function XMLNode($name = null) {
40 $this->name = $name;
41 $this->parent = null;
42 $this->attributes = array();
43 $this->value = null;
44 $this->children = array();
47 /**
48 * @param $includeNamespace boolean
49 * @return string
51 function getName($includeNamespace = true) {
52 if (
53 $includeNamespace ||
54 ($i = strpos($this->name, ':')) === false
55 ) return $this->name;
56 return substr($this->name, $i+1);
59 /**
60 * @param $name string
62 function setName($name) {
63 $this->name = $name;
66 /**
67 * @return XMLNode
69 function &getParent() {
70 return $this->parent;
73 /**
74 * @param $parent XMLNode
76 function setParent(&$parent) {
77 $this->parent =& $parent;
80 /**
81 * @return array all attributes
83 function getAttributes() {
84 return $this->attributes;
87 /**
88 * @param $name string attribute name
89 * @return string attribute value
91 function getAttribute($name) {
92 return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
95 /**
96 * @param $name string attribute name
97 * @param value string attribute value
99 function setAttribute($name, $value) {
100 $this->attributes[$name] = $value;
104 * @param $attributes array
106 function setAttributes($attributes) {
107 $this->attributes = $attributes;
111 * @return string
113 function &getValue() {
114 return $this->value;
118 * @param $value string
120 function setValue($value) {
121 $this->value =& $value;
125 * @return array this node's children (XMLNode objects)
127 function &getChildren() {
128 return $this->children;
132 * @param $name
133 * @param $index
134 * @return XMLNode the ($index+1)th child matching the specified name
136 function &getChildByName($name, $index = 0) {
137 if (!is_array($name)) $name = array($name);
138 foreach ($this->children as $key => $junk) {
139 $child =& $this->children[$key];
140 if (in_array($child->getName(), $name)) {
141 if ($index == 0) {
142 return $child;
143 } else {
144 $index--;
147 unset($child);
149 $child = null;
150 return $child;
154 * Get the value of a child node.
155 * @param $name String name of node
156 * @param $index Optional integer index of child node to find
158 function &getChildValue($name, $index = 0) {
159 $node =& $this->getChildByName($name);
160 if ($node) {
161 $returner =& $node->getValue();
162 } else {
163 $returner = null;
165 return $returner;
169 * @param $node XMLNode the child node to add
171 function addChild(&$node) {
172 $this->children[] =& $node;
176 * @param $output file handle to write to, or true for stdout, or null if XML to be returned as string
177 * @return string
179 function &toXml($output = null) {
180 $nullVar = null;
181 $out = '';
183 if ($this->parent === null) {
184 // This is the root node. Output information about the document.
185 $out .= "<?xml version=\"" . $this->getAttribute('version') . "\" encoding=\"UTF-8\"?>\n";
186 if ($this->getAttribute('type') != '') {
187 if ($this->getAttribute('url') != '') {
188 $out .= "<!DOCTYPE " . $this->getAttribute('type') . " PUBLIC \"" . $this->getAttribute('dtd') . "\" \"" . $this->getAttribute('url') . "\">";
189 } else {
190 $out .= "<!DOCTYPE " . $this->getAttribute('type') . " SYSTEM \"" . $this->getAttribute('dtd') . "\">";
195 if ($this->name !== null) {
196 $out .= '<' . $this->name;
197 foreach ($this->attributes as $name => $value) {
198 $value = XMLNode::xmlentities($value);
199 $out .= " $name=\"$value\"";
201 $out .= '>';
203 $out .= XMLNode::xmlentities($this->value, ENT_NOQUOTES);
204 foreach ($this->children as $child) {
205 if ($output !== null) {
206 if ($output === true) echo $out;
207 else fwrite ($output, $out);
208 $out = '';
210 $out .= $child->toXml($output);
212 if ($this->name !== null) $out .= '</' . $this->name . '>';
213 if ($output !== null) {
214 if ($output === true) echo $out;
215 else fwrite ($output, $out);
216 return $nullVar;
218 return $out;
221 function xmlentities($string, $quote_style=ENT_QUOTES) {
222 return htmlspecialchars($string, $quote_style, 'UTF-8');
225 function destroy() {
226 unset($this->value, $this->attributes, $this->parent, $this->name);
227 foreach ($this->children as $child) {
228 $child->destroy();
230 unset($this->children);