PageRenderTime 64ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/saf/lib/PEAR/Structures/Tree/Memory/XML.php

https://github.com/wokkie/sitellite
PHP | 287 lines | 80 code | 26 blank | 181 comment | 9 complexity | 70d0814210ed8a4b8b704ab4a9898a00 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 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: Wolfram Kriesing <wolfram@kriesing.de> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: XML.php,v 1.1.1.1 2005/04/29 04:44:43 lux Exp $
  20. require_once "XML/Parser.php";
  21. /**
  22. * the XML interface for the tree class
  23. *
  24. * @package Tree
  25. * @author
  26. * @version
  27. * @access public
  28. */
  29. class Tree_Memory_XML extends XML_Parser
  30. {
  31. /**
  32. * @var array $data the first element has to be empty, so we can use the parentId=0 as "no parent"
  33. */
  34. var $data = array(0=>NULL);
  35. /**
  36. * @var integer $level
  37. */
  38. var $level = 0;
  39. /**
  40. * @var array $parentIdOnLevel
  41. */
  42. var $parentIdOnLevel = array();
  43. /**
  44. * @var boolean $folding set case folding for the XML_Parser to false
  45. */
  46. var $folding = false; // turn off case folding
  47. /**
  48. * @var boolean if true it converts all attributes and tag names etc to lower case
  49. * this is default, since i dont see no way of case insensitive comparison
  50. * in the tree class, since you can access the internal data directly
  51. * or you get them returned ... i know this is not 100% proper OOP but that's how it is right now
  52. */
  53. var $_toLower = true;
  54. /**
  55. *
  56. *
  57. * @version 2002/01/17
  58. * @access public
  59. * @author Wolfram Kriesing <wolfram@kriesing.de>
  60. * @return boolean true on success
  61. */
  62. function Tree_Memory_XML( $dsn , $options )
  63. {
  64. $handle = $dsn;
  65. $this->XML_Parser();
  66. if (@is_resource($handle)) {
  67. $this->setInput($handle);
  68. } elseif ($handle != "") {
  69. $this->setInputFile($handle);
  70. } else {
  71. return $this->raiseError("No filename passed.");
  72. }
  73. }
  74. /**
  75. *
  76. *
  77. * @version 2002/01/17
  78. * @access public
  79. * @author Wolfram Kriesing <wolfram@kriesing.de>
  80. * @return boolean true on success
  81. */
  82. function startHandler($parser, $element, $attribs)
  83. {
  84. $elementBeforeId = sizeof($this->data)-1;
  85. $curId = sizeof($this->data);
  86. $this->data[$curId]['id'] = $curId;
  87. $this->data[$curId]['name'] = $this->_toLower ? strtolower($element) : $element;
  88. $this->data[$curId]['level'] = $this->level;
  89. $this->data[$curId]['attributes'] = $attribs;
  90. if( $this->_toLower )
  91. {
  92. $this->data[$curId]['attributes'] = array();
  93. foreach( $attribs as $key=>$value )
  94. $this->data[$curId]['attributes'][strtolower($key)] = $value;
  95. }
  96. if( isset($this->data[$elementBeforeId]['level']) &&
  97. $this->level == $this->data[$elementBeforeId]['level'] ) // is that a new child, or just a 'next' of a child?
  98. {
  99. $this->data[$curId]['parentId'] = $this->data[$elementBeforeId]['parentId'];
  100. }
  101. else // set stuff for the first child !!!
  102. {
  103. if( $this->level>0 ) // the root has no parent
  104. {
  105. $parentId = $this->parentIdOnLevel[$this->level-1];
  106. $this->data[$curId]['parentId'] = $parentId;
  107. }
  108. else
  109. {
  110. $this->data[$curId]['parentId'] = 0;
  111. }
  112. }
  113. $this->parentIdOnLevel[$this->level] = $curId;
  114. #print "$curId $element ".$this->data[$curId]['parentId'].'<br>';
  115. $this->level++;
  116. }
  117. /**
  118. *
  119. *
  120. * @version 2002/01/17
  121. * @access public
  122. * @author Wolfram Kriesing <wolfram@kriesing.de>
  123. * @return boolean true on success
  124. */
  125. function endHandler($parser, $element)
  126. {
  127. $this->level--;
  128. }
  129. /**
  130. *
  131. *
  132. * @version 2002/01/17
  133. * @access public
  134. * @author Wolfram Kriesing <wolfram@kriesing.de>
  135. * @return boolean true on success
  136. */
  137. function cdataHandler($parser, $cdata)
  138. {
  139. # QUESTION: why is this method called multiple times for one element?
  140. # is every space a cdata ???
  141. # ANSWER: if you call xml_parse($parser, "foo ", false) and then
  142. # xml_parse($parser, "bar", true), callbacks are done once
  143. # for each xml_parse() call.
  144. if( !isset($this->data[ sizeof($this->data)-1 ]['cdata']) )
  145. $this->data[ sizeof($this->data)-1 ]['cdata'] = '';
  146. #print "cdata = '$cdata'\r\n";
  147. $this->data[ sizeof($this->data)-1 ]['cdata'].= $cdata;
  148. }
  149. /**
  150. *
  151. *
  152. * @version 2002/01/17
  153. * @access public
  154. * @author Wolfram Kriesing <wolfram@kriesing.de>
  155. * @return boolean true on success
  156. */
  157. function defaultHandler($parser, $cdata)
  158. {
  159. # $this->data[ sizeof($this->data)-1 ]['cdata'] = $cdata;
  160. # not in use yet :-( is that ok??
  161. }
  162. /**
  163. * read the data from the xml file and prepare them so the tree
  164. * class can work with it, the preparation is mainly done in startHandler
  165. *
  166. * @version 2002/01/17
  167. * @access public
  168. * @author Wolfram Kriesing <wolfram@kriesing.de>
  169. * @return boolean true on success
  170. */
  171. function setup()
  172. {
  173. $this->parse();
  174. return $this->data;
  175. } // end of function
  176. /**
  177. * read the data from an xml string and prepare them so the tree
  178. * class can work with it, the preparation is mainly done in startHandler
  179. *
  180. * @version 2002/02/05
  181. * @access public
  182. * @author Wolfram Kriesing <wolfram@kriesing.de>
  183. * @return boolean true on success
  184. */
  185. function setupByRawData( $xmlString )
  186. {
  187. $this->parseString( $xmlString , true );
  188. return $this->data;
  189. }
  190. /**
  191. * TO BE IMPLEMNTED
  192. * adds _one_ new element in the tree under the given parent
  193. * the values' keys given have to match the db-columns, because the
  194. * value gets inserted in the db directly
  195. * to add an entire node containing children and so on see 'addNode()'
  196. *
  197. * @see addNode()
  198. * @version 2001/10/09
  199. * @access public
  200. * @author Wolfram Kriesing <wolfram@kriesing.de>
  201. * @param array $newValues this array contains the values that shall be inserted in the db-table
  202. * @return mixed either boolean false on failure or the id of the inserted row
  203. */
  204. /* function add( $newValues )
  205. {
  206. // add the data in the internal structure $this->data
  207. $this->data[sizeof($this->data)] = $newValues;
  208. # i am thinking if it might be a good solution to walk the data-array
  209. # and write each line singlely until the one to add comes, write it and
  210. # keep on writing the data-array
  211. # but that means writing the entire file every time any method that
  212. # changes the xml-file's structure the entire file is written,
  213. # can that not be done somehow better ???
  214. # // and regenerate the xml file
  215. # $this->_writeFile();
  216. } // end of function
  217. */
  218. /**
  219. * TO BE IMPLEMNTED
  220. * removes the given node
  221. *
  222. * @version 2001/10/09
  223. * @access public
  224. * @author Wolfram Kriesing <wolfram@kriesing.de>
  225. * @param mixed $id the id of the node to be removed
  226. * @return boolean true on success
  227. */
  228. /* function remove( $id )
  229. {
  230. // remove the data from this->data
  231. unset($this->data[$id]);
  232. # see comment in "add"-method
  233. } // end of function
  234. */
  235. /**
  236. * TO BE IMPLEMNTED
  237. * move an entry under a given parent or behind a given entry
  238. *
  239. * @version 2001/10/10
  240. * @access public
  241. * @author Wolfram Kriesing <wolfram@kriesing.de>
  242. * @param
  243. * @param
  244. * @param integer if prevId is given the element with the id idToMove shall be moved _behind_ element with id=prevId
  245. * before would be easier, but then no element could be inserted at the end :-/
  246. * @return boolean true for success
  247. */
  248. /* function move( $idToMove , $newParentId , $prevId=0 )
  249. {
  250. $this->data[$idToMove]['parentId'] = $newParentId;
  251. $this->data[$idToMove]['prevId'] = $prevId;
  252. # see comment in "add"-method
  253. } // end of function
  254. */
  255. }
  256. ?>