PageRenderTime 75ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/php_tool/lib/Xml.class.php

http://sqltoappledictionary.googlecode.com/
PHP | 376 lines | 373 code | 1 blank | 2 comment | 81 complexity | 6e4223417b7ca0b287887469a2b2ac76 MD5 | raw file
  1. <?php
  2. class Xml {
  3. public function __construct() {
  4. ;
  5. ;
  6. }
  7. public $nodeType;
  8. //;
  9. //;
  10. public $parent;
  11. public $_nodeName;
  12. public $_nodeValue;
  13. public $_attributes;
  14. public $_children;
  15. public $_parent;
  16. public function getNodeName() {
  17. if($this->nodeType != Xml::$Element) {
  18. throw new HException("bad nodeType");
  19. }
  20. return $this->_nodeName;
  21. }
  22. public function setNodeName($n) {
  23. if($this->nodeType != Xml::$Element) {
  24. throw new HException("bad nodeType");
  25. }
  26. return $this->_nodeName = $n;
  27. }
  28. public function getNodeValue() {
  29. if($this->nodeType == Xml::$Element || $this->nodeType == Xml::$Document) {
  30. throw new HException("bad nodeType");
  31. }
  32. return $this->_nodeValue;
  33. }
  34. public function setNodeValue($v) {
  35. if($this->nodeType == Xml::$Element || $this->nodeType == Xml::$Document) {
  36. throw new HException("bad nodeType");
  37. }
  38. return $this->_nodeValue = $v;
  39. }
  40. public function getParent() {
  41. return $this->_parent;
  42. }
  43. public function get($att) {
  44. if($this->nodeType != Xml::$Element) {
  45. throw new HException("bad nodeType");
  46. }
  47. return $this->_attributes->get($att);
  48. }
  49. public function set($att, $value) {
  50. if($this->nodeType != Xml::$Element) {
  51. throw new HException("bad nodeType");
  52. }
  53. $this->_attributes->set($att, htmlspecialchars($value, ENT_COMPAT, "UTF-8"));
  54. }
  55. public function remove($att) {
  56. if($this->nodeType != Xml::$Element) {
  57. throw new HException("bad nodeType");
  58. }
  59. $this->_attributes->remove($att);
  60. }
  61. public function exists($att) {
  62. if($this->nodeType != Xml::$Element) {
  63. throw new HException("bad nodeType");
  64. }
  65. return $this->_attributes->exists($att);
  66. }
  67. public function attributes() {
  68. if($this->nodeType != Xml::$Element) {
  69. throw new HException("bad nodeType");
  70. }
  71. return $this->_attributes->keys();
  72. }
  73. public function iterator() {
  74. if($this->_children === null) {
  75. throw new HException("bad nodetype");
  76. }
  77. $me = $this;
  78. $it = null;
  79. $it = _hx_anonymous(array("cur" => 0, "x" => $me->_children, "hasNext" => array(new _hx_lambda(array("it" => &$it, "me" => &$me), null, array(), "{
  80. return \$it->cur < _hx_len(\$it->x);
  81. }"), 'execute0'), "next" => array(new _hx_lambda(array("it" => &$it, "me" => &$me), null, array(), "{
  82. return \$it->x[\$it->cur++];
  83. }"), 'execute0')));
  84. return $it;
  85. }
  86. public function elements() {
  87. if($this->_children === null) {
  88. throw new HException("bad nodetype");
  89. }
  90. $me = $this;
  91. $it = null;
  92. $it = _hx_anonymous(array("cur" => 0, "x" => $me->_children, "hasNext" => array(new _hx_lambda(array("it" => &$it, "me" => &$me), null, array(), "{
  93. \$k = \$it->cur;
  94. \$l = _hx_len(\$it->x);
  95. while(\$k < \$l) {
  96. if(_hx_array_get(\$it->x, \$k)->nodeType == Xml::\$Element) {
  97. break;
  98. }
  99. \$k += 1;
  100. ;
  101. }
  102. \$it->cur = \$k;
  103. return \$k < \$l;
  104. }"), 'execute0'), "next" => array(new _hx_lambda(array("it" => &$it, "me" => &$me), null, array(), "{
  105. \$k = \$it->cur;
  106. \$l = _hx_len(\$it->x);
  107. while(\$k < \$l) {
  108. \$n = \$it->x[\$k];
  109. \$k += 1;
  110. if(\$n->nodeType == Xml::\$Element) {
  111. \$it->cur = \$k;
  112. return \$n;
  113. }
  114. unset(\$n);
  115. }
  116. return null;
  117. }"), 'execute0')));
  118. return $it;
  119. }
  120. public function elementsNamed($name) {
  121. if($this->_children === null) {
  122. throw new HException("bad nodetype");
  123. }
  124. $me = $this;
  125. $it = null;
  126. $it = _hx_anonymous(array("cur" => 0, "x" => $me->_children, "hasNext" => array(new _hx_lambda(array("it" => &$it, "me" => &$me, "name" => &$name), null, array(), "{
  127. \$k = \$it->cur;
  128. \$l = _hx_len(\$it->x);
  129. while(\$k < \$l) {
  130. \$n = \$it->x[\$k];
  131. if(\$n->nodeType == Xml::\$Element && \$n->_nodeName == \$name) {
  132. break;
  133. }
  134. \$k++;
  135. unset(\$n);
  136. }
  137. \$it->cur = \$k;
  138. return \$k < \$l;
  139. }"), 'execute0'), "next" => array(new _hx_lambda(array("it" => &$it, "me" => &$me, "name" => &$name), null, array(), "{
  140. \$k = \$it->cur;
  141. \$l = _hx_len(\$it->x);
  142. while(\$k < \$l) {
  143. \$n = \$it->x[\$k];
  144. \$k++;
  145. if(\$n->nodeType == Xml::\$Element && \$n->_nodeName == \$name) {
  146. \$it->cur = \$k;
  147. return \$n;
  148. }
  149. unset(\$n);
  150. }
  151. return null;
  152. }"), 'execute0')));
  153. return $it;
  154. }
  155. public function firstChild() {
  156. if($this->_children === null) {
  157. throw new HException("bad nodetype");
  158. }
  159. if($this->_children->length === 0) {
  160. return null;
  161. }
  162. return $this->_children[0];
  163. }
  164. public function firstElement() {
  165. if($this->_children === null) {
  166. throw new HException("bad nodetype");
  167. }
  168. $cur = 0;
  169. $l = $this->_children->length;
  170. while($cur < $l) {
  171. $n = $this->_children[$cur];
  172. if($n->nodeType == Xml::$Element) {
  173. return $n;
  174. }
  175. $cur++;
  176. unset($n);
  177. }
  178. return null;
  179. }
  180. public function addChild($x) {
  181. if($this->_children === null) {
  182. throw new HException("bad nodetype");
  183. }
  184. if($x->_parent !== null) {
  185. $x->_parent->_children->remove($x);
  186. }
  187. $x->_parent = $this;
  188. $this->_children->push($x);
  189. }
  190. public function removeChild($x) {
  191. if($this->_children === null) {
  192. throw new HException("bad nodetype");
  193. }
  194. $b = $this->_children->remove($x);
  195. if($b) {
  196. $x->_parent = null;
  197. }
  198. return $b;
  199. }
  200. public function insertChild($x, $pos) {
  201. if($this->_children === null) {
  202. throw new HException("bad nodetype");
  203. }
  204. if($x->_parent !== null) {
  205. $x->_parent->_children->remove($x);
  206. }
  207. $x->_parent = $this;
  208. $this->_children->insert($pos, $x);
  209. }
  210. public function toString() {
  211. if($this->nodeType == Xml::$PCData) {
  212. return $this->_nodeValue;
  213. }
  214. if($this->nodeType == Xml::$CData) {
  215. return ("<![CDATA[" . $this->_nodeValue) . "]]>";
  216. }
  217. if($this->nodeType == Xml::$Comment || $this->nodeType == Xml::$DocType || $this->nodeType == Xml::$Prolog) {
  218. return $this->_nodeValue;
  219. }
  220. $s = "";
  221. if($this->nodeType == Xml::$Element) {
  222. $s .= "<";
  223. $s .= $this->_nodeName;
  224. $?it = $this->_attributes->keys();
  225. while($?it->hasNext()) {
  226. $k = $?it->next();
  227. {
  228. $s .= " ";
  229. $s .= $k;
  230. $s .= "=\"";
  231. $s .= $this->_attributes->get($k);
  232. $s .= "\"";
  233. ;
  234. }
  235. }
  236. if($this->_children->length === 0) {
  237. $s .= "/>";
  238. return $s;
  239. }
  240. $s .= ">";
  241. }
  242. $?it2 = $this->iterator();
  243. while($?it2->hasNext()) {
  244. $x = $?it2->next();
  245. $s .= $x->toString();
  246. }
  247. if($this->nodeType == Xml::$Element) {
  248. $s .= "</";
  249. $s .= $this->_nodeName;
  250. $s .= ">";
  251. }
  252. return $s;
  253. }
  254. public function __call($m, $a) {
  255. if(isset($this->$m) && is_callable($this->$m))
  256. return call_user_func_array($this->$m, $a);
  257. else if(isset($this->?dynamics[$m]) && is_callable($this->?dynamics[$m]))
  258. return call_user_func_array($this->?dynamics[$m], $a);
  259. else if('toString' == $m)
  260. return $this->__toString();
  261. else
  262. throw new HException('Unable to call ?'.$m.'?');
  263. }
  264. static $Element;
  265. static $PCData;
  266. static $CData;
  267. static $Comment;
  268. static $DocType;
  269. static $Prolog;
  270. static $Document;
  271. static $build;
  272. static function __start_element_handler($parser, $name, $attribs) {
  273. $node = Xml::createElement($name);
  274. while(list($k, $v) = each($attribs)) $node->set($k, $v);
  275. Xml::$build->addChild($node);
  276. Xml::$build = $node;
  277. }
  278. static function __end_element_handler($parser, $name) {
  279. Xml::$build = Xml::$build->getParent();
  280. }
  281. static function __character_data_handler($parser, $data) {
  282. if((strlen($data) === 1 && htmlentities($data) != $data) || htmlentities($data) == $data) {
  283. Xml::$build->addChild(Xml::createPCData(htmlentities($data)));
  284. }
  285. else {
  286. Xml::$build->addChild(Xml::createCData($data));
  287. }
  288. }
  289. static function __default_handler($parser, $data) {
  290. Xml::$build->addChild(Xml::createPCData($data));
  291. }
  292. static $xmlChecker;
  293. static function parse($str) {
  294. Xml::$build = Xml::createDocument();
  295. $xml_parser = xml_parser_create();
  296. xml_set_element_handler($xml_parser, isset(Xml::$__start_element_handler) ? Xml::$__start_element_handler: array("Xml", "__start_element_handler"), isset(Xml::$__end_element_handler) ? Xml::$__end_element_handler: array("Xml", "__end_element_handler"));
  297. xml_set_character_data_handler($xml_parser, isset(Xml::$__character_data_handler) ? Xml::$__character_data_handler: array("Xml", "__character_data_handler"));
  298. xml_set_default_handler($xml_parser, isset(Xml::$__default_handler) ? Xml::$__default_handler: array("Xml", "__default_handler"));
  299. xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
  300. xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 0);
  301. $isComplete = Xml::$xmlChecker->match($str);
  302. if(!$isComplete) {
  303. $str = ("<doc>" . $str) . "</doc>";
  304. }
  305. if(1 !== xml_parse($xml_parser, $str, true)) {
  306. throw new HException("Xml parse error (" . ((xml_error_string(xml_get_error_code($xml_parser)) . ") line #") . xml_get_current_line_number($xml_parser)));
  307. }
  308. xml_parser_free($xml_parser);
  309. if($isComplete) {
  310. return Xml::$build;
  311. }
  312. else {
  313. Xml::$build = Xml::$build->_children[0];
  314. Xml::$build->_parent = null;
  315. Xml::$build->_nodeName = null;
  316. Xml::$build->nodeType = Xml::$Document;
  317. return Xml::$build;
  318. }
  319. }
  320. static function createElement($name) {
  321. $r = new Xml();
  322. $r->nodeType = Xml::$Element;
  323. $r->_children = new _hx_array(array());
  324. $r->_attributes = new Hash();
  325. $r->setNodeName($name);
  326. return $r;
  327. }
  328. static function createPCData($data) {
  329. $r = new Xml();
  330. $r->nodeType = Xml::$PCData;
  331. $r->setNodeValue($data);
  332. return $r;
  333. }
  334. static function createCData($data) {
  335. $r = new Xml();
  336. $r->nodeType = Xml::$CData;
  337. $r->setNodeValue($data);
  338. return $r;
  339. }
  340. static function createComment($data) {
  341. $r = new Xml();
  342. $r->nodeType = Xml::$Comment;
  343. $r->setNodeValue($data);
  344. return $r;
  345. }
  346. static function createDocType($data) {
  347. $r = new Xml();
  348. $r->nodeType = Xml::$DocType;
  349. $r->setNodeValue($data);
  350. return $r;
  351. }
  352. static function createProlog($data) {
  353. $r = new Xml();
  354. $r->nodeType = Xml::$Prolog;
  355. $r->setNodeValue($data);
  356. return $r;
  357. }
  358. static function createDocument() {
  359. $r = new Xml();
  360. $r->nodeType = Xml::$Document;
  361. $r->_children = new _hx_array(array());
  362. return $r;
  363. }
  364. function __toString() { return $this->toString(); }
  365. }
  366. {
  367. Xml::$Element = "element";
  368. Xml::$PCData = "pcdata";
  369. Xml::$CData = "cdata";
  370. Xml::$Comment = "comment";
  371. Xml::$DocType = "doctype";
  372. Xml::$Prolog = "prolog";
  373. Xml::$Document = "document";
  374. }
  375. Xml::$xmlChecker = new EReg("\\s*(<\\?xml|<!DOCTYPE)", "mi");