PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/catalog/includes/classes/xmldocument.php

https://github.com/eosc/EosC-2.3
PHP | 302 lines | 232 code | 40 blank | 30 comment | 43 complexity | 95cfb3cb372ec244d503273948a90602 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /*
  3. $Id: xmldocument.php,v 1.5 2003/06/27 01:03:03 torinwalker Exp $
  4. Written by Torin Walker
  5. torinwalker@rogers.com
  6. Generic XML Document support for when there is none.
  7. Copyright(c) 2003 by Torin Walker, All rights reserved.
  8. Released under the GNU General Public License
  9. */
  10. define("ELEMENT", 0);
  11. define("TEXTELEMENT", 1);
  12. //*****************
  13. class XMLDocument {
  14. var $root;
  15. var $children;
  16. function XMLDocument() {
  17. }
  18. function createElement($name) {
  19. $node = new Node();
  20. $node->setName($name);
  21. $node->setType(ELEMENT);
  22. return $node;
  23. }
  24. function createTextElement($text) {
  25. $node = new Node();
  26. $node->setType(TEXTELEMENT);
  27. $node->setValue($text);
  28. return $node;
  29. }
  30. function getRoot() {
  31. return $this->root;
  32. }
  33. function setRoot($node) {
  34. $this->root = $node;
  35. }
  36. function toString() {
  37. if ($this->root) {
  38. return $this->root->toString();
  39. } else {
  40. return "DOCUMENT ROOT NOT SET";
  41. }
  42. }
  43. function getValueByPath($path) {
  44. $pathArray = split("/", $path);
  45. if ($pathArray[0] == $this->root->getName()) {
  46. //print_r("Looking for " . $pathArray[0] . "<br>");
  47. array_shift($pathArray);
  48. $newPath = implode("/", $pathArray);
  49. return $this->root->getValueByPath($newPath);
  50. }
  51. }
  52. }
  53. //**********
  54. class Node {
  55. var $name;
  56. var $type;
  57. var $text;
  58. var $parent;
  59. var $children;
  60. var $attributes;
  61. function Node() {
  62. $this->children = array();
  63. $this->attributes = array();
  64. }
  65. function getName() {
  66. return $this->name;
  67. }
  68. function setName($name) {
  69. $this->name = $name;
  70. }
  71. function setParent(&$node) {
  72. $this->parent =& $node;
  73. }
  74. function &getParent() {
  75. return $this->parent;
  76. }
  77. function &getChildren() {
  78. return $this->children;
  79. }
  80. function getType() {
  81. return $this->type;
  82. }
  83. function setType($type) {
  84. $this->type = $type;
  85. }
  86. function getElementByName($name) {
  87. for ($i = 0; $i < count($this->children); $i++) {
  88. if ($this->children[$i]->getType() == ELEMENT) {
  89. if ($this->children[$i]->getName() == $name) {
  90. return $this->children[$i];
  91. }
  92. }
  93. }
  94. return null;
  95. }
  96. function getElementsByName($name) {
  97. $elements = array();
  98. for ($i = 0; $i < count($this->children); $i++) {
  99. if ($this->children[$i]->getType() == ELEMENT) {
  100. if ($this->children[$i]->getName() == $name) {
  101. $elements[] = $this->children[$i];
  102. }
  103. }
  104. }
  105. return $elements;
  106. }
  107. function getValueByPath($path) {
  108. $pathArray = split('/', $path);
  109. $node = $this;
  110. for ($i = 0; $i < count($pathArray); $i++) {
  111. //print_r("Looking for " . $pathArray[$i] ."<br>");
  112. if ($node->getChildren()) {
  113. for ($j = 0; $j < count($node->getChildren()); $j++) {
  114. if ($node->children[$j]->getType() == ELEMENT) {
  115. if ($node->children[$j]->getName() == $pathArray[$i]) {
  116. //print_r("Found " . $pathArray[$i] ."<br>");
  117. $node = $node->children[$j];
  118. }
  119. }
  120. }
  121. }
  122. }
  123. return $node->getValue();
  124. }
  125. function getText() {
  126. return $this->text();
  127. }
  128. function setValue($text) {
  129. $this->text = $text;
  130. }
  131. function getValue() {
  132. $value = NULL;
  133. if ($this->getType() == ELEMENT) {
  134. for ($i = 0; $i < count($this->children); $i++) {
  135. $value .= $this->children[$i]->getValue();
  136. }
  137. } elseif ($this->getType() == TEXTELEMENT) {
  138. $value .= $this->text;
  139. }
  140. return $value;
  141. }
  142. function setAttribute($name, $value) {
  143. $attributes[$name] = $value;
  144. }
  145. function getAttribute($name) {
  146. return $attributes[$name];
  147. }
  148. function addNode(&$node) {
  149. $this->children[] =& $node;
  150. $node->parent =& $this;
  151. }
  152. function parentToString($node) {
  153. while($node->parent) {
  154. //print_r("Node " . $node->name . " has parent<br>");
  155. $node = $node->parent;
  156. }
  157. //print_r("Node contents from root: " . $node->toString() . "<br>");
  158. }
  159. function toString() {
  160. $string = NULL;
  161. //print_r("toString child count " . $this->name . " contains " . count($this->children) . "<br>");
  162. if ($this->type == ELEMENT) {
  163. $string .= '{' . $this->name . '}';
  164. for ($i = 0; $i < count($this->children); $i++) {
  165. $string .= $this->children[$i]->toString();
  166. }
  167. $string .= '{/' . $this->name . '}';
  168. } else {
  169. $string .= $this->getValue();
  170. }
  171. return $string;
  172. }
  173. }
  174. //**************
  175. class XMLParser {
  176. var $xp;
  177. var $document;
  178. var $current;
  179. var $error;
  180. function XMLParser() {
  181. $this->document = new XMLDocument();
  182. $this->error = array();
  183. }
  184. function setDocument($document) {
  185. $this->document = $document;
  186. }
  187. function getDocument() {
  188. return $this->document;
  189. }
  190. function destruct(){
  191. xml_parser_free($this->xp);
  192. }
  193. // return 1 for an error, 0 for no error
  194. function hasErrors() {
  195. if (sizeof($this->error) > 0) {
  196. return 1;
  197. } else {
  198. return 0;
  199. }
  200. }
  201. // return array of error messages
  202. function getError() {
  203. return $this->error;
  204. }
  205. // process xml start tag
  206. function startElement($xp, $name, $attrs) {
  207. //print_r("Found Start Tag: " . $name . "<br>");
  208. $node =& $this->document->createElement($name);
  209. if ($this->document->getRoot()) {
  210. $this->current->addNode($node);
  211. } else {
  212. $this->document->root =& $node;
  213. }
  214. $this->current =& $node;
  215. }
  216. // process xml end tag
  217. function endElement($xp, $name){
  218. //print_r("Found End Tag: " . $name . "<br>");
  219. if ($this->current->getParent()) {
  220. $this->current =& $this->current->getParent();
  221. }
  222. }
  223. // process data between xml tags
  224. function dataHandler($xp, $text) {
  225. //print_r("Adding Data: \"" . $text . "\"<br>");
  226. $node =& $this->document->createTextElement($text);
  227. $this->current->addNode($node);
  228. }
  229. // parse xml document from string
  230. function parse($xmlString) {
  231. if(!($this->xp = @xml_parser_create())) {
  232. $this->error['description'] = 'Could not create xml parser';
  233. }
  234. if(!$this->hasErrors()) {
  235. if(!@xml_set_object($this->xp, $this)) {
  236. $this->error['description'] = 'Could not set xml parser for object';
  237. }
  238. }
  239. if(!$this->hasErrors()) {
  240. if(!@xml_set_element_handler($this->xp, 'startElement', 'endElement')) {
  241. $this->error['description'] = 'Could not set xml element handler';
  242. }
  243. }
  244. if(!$this->hasErrors()) {
  245. if(!@xml_set_character_data_handler($this->xp, 'dataHandler')) {
  246. $this->error['description'] = 'Could not set xml character handler';
  247. }
  248. }
  249. xml_parser_set_option($this->xp, XML_OPTION_CASE_FOLDING, false);
  250. if (!$this->hasErrors()) {
  251. if(!@xml_parse($this->xp, $xmlString)) {
  252. $this->error['description'] = xml_error_string(xml_get_error_code($this->xp));
  253. $this->error['line'] = xml_get_current_line_number($this->xp);
  254. }
  255. }
  256. }
  257. }
  258. ?>