PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/s3db3.5.10/pearlib/XML/Tree.php

https://code.google.com/p/s3db/
PHP | 370 lines | 175 code | 25 blank | 170 comment | 17 complexity | 046ce3031bb819468503c9a5bfda2416 MD5 | raw file
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Bernd Römer <berndr@bonn.edu> |
  17. // | Sebastian Bergmann <sb@sebastian-bergmann.de> |
  18. // | Tomas V.V.Cox <cox@idecnet.com> (tree mapping from xml file)|
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: Tree.php,v 1.19 2002/05/17 12:15:23 cox Exp $
  22. //
  23. require_once 'XML/Parser.php';
  24. require_once 'XML/Tree/Node.php';
  25. /**
  26. * PEAR::XML_Tree
  27. *
  28. * Purpose
  29. *
  30. * Allows for the building of XML data structures
  31. * using a tree representation, without the need
  32. * for an extension like DOMXML.
  33. *
  34. * Example
  35. *
  36. * $tree = new XML_Tree;
  37. * $root =& $tree->addRoot('root');
  38. * $foo =& $root->addChild('foo');
  39. *
  40. * header('Content-Type: text/xml');
  41. * $tree->dump();
  42. *
  43. * @author Bernd Römer <berndr@bonn.edu>
  44. * @package XML
  45. * @version $Version$ - 1.0
  46. */
  47. class XML_Tree extends XML_Parser
  48. {
  49. /**
  50. * File Handle
  51. *
  52. * @var ressource
  53. */
  54. var $file = NULL;
  55. /**
  56. * Filename
  57. *
  58. * @var string
  59. */
  60. var $filename = '';
  61. /**
  62. * Namespace
  63. *
  64. * @var array
  65. */
  66. var $namespace = array();
  67. /**
  68. * Root
  69. *
  70. * @var object XML_Tree_Node
  71. */
  72. var $root = NULL;
  73. /**
  74. * XML Version
  75. *
  76. * @var string
  77. */
  78. var $version = '1.0';
  79. /**
  80. * Constructor
  81. *
  82. * @param string Filename
  83. * @param string XML Version
  84. */
  85. function XML_Tree($filename = '', $version = '1.0') {
  86. $this->filename = $filename;
  87. $this->version = $version;
  88. }
  89. /**
  90. * Add root node.
  91. *
  92. * @param string $name name of root element
  93. * @return object XML_Tree_Node reference to root node
  94. *
  95. * @access public
  96. */
  97. function &addRoot($name, $content = '', $attributes = array()) {
  98. $this->root = new XML_Tree_Node($name, $content, $attributes);
  99. return $this->root;
  100. }
  101. /**
  102. * @deprecated
  103. */
  104. function &add_root($name, $content = '', $attributes = array()) {
  105. return $this->addRoot($name, $content, $attributes);
  106. }
  107. /**
  108. * inserts a child/tree (child) into tree ($path,$pos) and
  109. * maintains namespace integrity
  110. *
  111. * @param array $path path to parent of child to remove
  112. * @param integer $pos position of child to be inserted in its parents children-list
  113. * @param mixed $child child-node (by XML_Tree,XML_Node or Name)
  114. * @param string $content content (text) for new node
  115. * @param array $attributes attribute-hash for new node
  116. *
  117. * @return object XML_Tree_Node inserted child (node)
  118. * @access public
  119. */
  120. function &insertChild($path,$pos,$child, $content = '', $attributes = array()) {
  121. // update namespace to maintain namespace integrity
  122. $count=count($path);
  123. foreach($this->namespace as $key => $val) {
  124. if ((array_slice($val,0,$count)==$path) && ($val[$count]>=$pos))
  125. $this->namespace[$key][$count]++;
  126. }
  127. $parent=&$this->get_node_by_path($path);
  128. return($parent->insert_child($pos,$child,$content,$attributes));
  129. }
  130. /**
  131. * @deprecated
  132. */
  133. function &insert_child($path,$pos,$child, $content = '', $attributes = array()) {
  134. return $this->insertChild($path, $child, $content, $attributes);
  135. }
  136. /*
  137. * removes a child ($path,$pos) from tree ($path,$pos) and
  138. * maintains namespace integrity
  139. *
  140. * @param array $path path to parent of child to remove
  141. * @param integer $pos position of child in parents children-list
  142. *
  143. * @return object XML_Tree_Node parent whichs child was removed
  144. * @access public
  145. */
  146. function &removeChild($path,$pos) {
  147. // update namespace to maintain namespace integrity
  148. $count=count($path);
  149. foreach($this->namespace as $key => $val) {
  150. if (array_slice($val,0,$count)==$path) {
  151. if ($val[$count]==$pos) { unset($this->namespace[$key]); break; }
  152. if ($val[$count]>$pos)
  153. $this->namespace[$key][$count]--;
  154. }
  155. }
  156. $parent=&$this->get_node_by_path($path);
  157. return($parent->remove_child($pos));
  158. }
  159. /**
  160. * @deprecated
  161. */
  162. function &remove_child($path, $pos) {
  163. return $this->removeChild($path, $pos);
  164. }
  165. /*
  166. * Maps a xml file to a objects tree
  167. *
  168. * @return mixed The objects tree (XML_tree or an Pear error)
  169. * @access public
  170. */
  171. function &getTreeFromFile ()
  172. {
  173. $this->folding = false;
  174. $this->XML_Parser(null, 'event');
  175. $err = $this->setInputFile($this->filename);
  176. if (PEAR::isError($err)) {
  177. return $err;
  178. }
  179. $this->cdata = null;
  180. $err = $this->parse();
  181. if (PEAR::isError($err)) {
  182. return $err;
  183. }
  184. return $this->root;
  185. }
  186. function getTreeFromString($str)
  187. {
  188. $this->folding = false;
  189. $this->XML_Parser(null, 'event');
  190. $this->cdata = null;
  191. $err = $this->parseString($str);
  192. if (PEAR::isError($err)) {
  193. return $err;
  194. }
  195. return $this->root;
  196. }
  197. /**
  198. * Handler for the xml-data
  199. *
  200. * @param mixed $xp ignored
  201. * @param string $elem name of the element
  202. * @param array $attribs attributes for the generated node
  203. *
  204. * @access private
  205. */
  206. function startHandler($xp, $elem, &$attribs)
  207. {
  208. // root elem
  209. if (!isset($this->i)) {
  210. $this->obj1 =& $this->add_root($elem, null, $attribs);
  211. $this->i = 2;
  212. } else {
  213. // mixed contents
  214. if (!empty($this->cdata)) {
  215. $parent_id = 'obj' . ($this->i - 1);
  216. $parent =& $this->$parent_id;
  217. $parent->children[] = &new XML_Tree_Node(null, $this->cdata);
  218. }
  219. $obj_id = 'obj' . $this->i++;
  220. $this->$obj_id = &new XML_Tree_Node($elem, null, $attribs);
  221. }
  222. $this->cdata = null;
  223. return null;
  224. }
  225. /**
  226. * Handler for the xml-data
  227. *
  228. * @param mixed $xp ignored
  229. * @param string $elem name of the element
  230. *
  231. * @access private
  232. */
  233. function endHandler($xp, $elem)
  234. {
  235. $this->i--;
  236. if ($this->i > 1) {
  237. $obj_id = 'obj' . $this->i;
  238. // recover the node created in StartHandler
  239. $node =& $this->$obj_id;
  240. // mixed contents
  241. if (count($node->children) > 0) {
  242. if (trim($this->cdata)) {
  243. $node->children[] = &new XML_Tree_Node(null, $this->cdata);
  244. }
  245. } else {
  246. $node->set_content($this->cdata);
  247. }
  248. $parent_id = 'obj' . ($this->i - 1);
  249. $parent =& $this->$parent_id;
  250. // attach the node to its parent node children array
  251. $parent->children[] = $node;
  252. }
  253. $this->cdata = null;
  254. return null;
  255. }
  256. /*
  257. * The xml character data handler
  258. *
  259. * @param mixed $xp ignored
  260. * @param string $data PCDATA between tags
  261. *
  262. * @access private
  263. */
  264. function cdataHandler($xp, $data)
  265. {
  266. if (trim($data)) {
  267. $this->cdata .= $data;
  268. }
  269. }
  270. /**
  271. * Get a copy of this tree.
  272. *
  273. * @return object XML_Tree
  274. * @access public
  275. */
  276. function clone() {
  277. $clone=new XML_Tree($this->filename,$this->version);
  278. $clone->root=$this->root->clone();
  279. // clone all other vars
  280. $temp=get_object_vars($this);
  281. foreach($temp as $varname => $value)
  282. if (!in_array($varname,array('filename','version','root')))
  283. $clone->$varname=$value;
  284. return($clone);
  285. }
  286. /**
  287. * Print text representation of XML tree.
  288. *
  289. * @access public
  290. */
  291. function dump() {
  292. echo $this->get();
  293. }
  294. /**
  295. * Get text representation of XML tree.
  296. *
  297. * @return string XML
  298. * @access public
  299. */
  300. function &get() {
  301. $out = '<?xml version="' . $this->version . "\"?>\n";
  302. $out .= $this->root->get();
  303. return $out;
  304. }
  305. /**
  306. * Get current namespace.
  307. *
  308. * @param string $name namespace
  309. * @return string
  310. *
  311. * @access public
  312. */
  313. function &getName($name) {
  314. return $this->root->get_element($this->namespace[$name]);
  315. }
  316. /**
  317. * @deprecated
  318. */
  319. function &get_name($name) {
  320. return $this->getName($name);
  321. }
  322. /**
  323. * Register a namespace.
  324. *
  325. * @param string $name namespace
  326. * @param string $path path
  327. *
  328. * @access public
  329. */
  330. function registerName($name, $path) {
  331. $this->namespace[$name] = $path;
  332. }
  333. /**
  334. * @deprecated
  335. */
  336. function register_name($name, $path) {
  337. return $this->registerName($name, $path);
  338. }
  339. }
  340. ?>