PhpXml__.hx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package php;
  26. import Xml;
  27. class PhpXml__ {
  28. public static var Element(default,null) : XmlType;
  29. public static var PCData(default,null) : XmlType;
  30. public static var CData(default,null) : XmlType;
  31. public static var Comment(default,null) : XmlType;
  32. public static var DocType(default,null) : XmlType;
  33. public static var Prolog(default,null) : XmlType;
  34. public static var Document(default,null) : XmlType;
  35. public var nodeType(default,null) : XmlType;
  36. public var nodeName(getNodeName,setNodeName) : String;
  37. public var nodeValue(getNodeValue,setNodeValue) : String;
  38. public var parent(getParent,null) : PhpXml__;
  39. public var _nodeName : String;
  40. public var _nodeValue : String;
  41. public var _attributes : Hash<String>;
  42. public var _children : Array<PhpXml__>;
  43. public var _parent : PhpXml__;
  44. private static var build : PhpXml__;
  45. private static function __start_element_handler(parser : Dynamic, name : String, attribs : Array<String>) {
  46. var node = createElement(name);
  47. untyped __php__("foreach($attribs as $key => $value) $node->set($key, $value)");
  48. build.addChild(node);
  49. build = node;
  50. }
  51. private static function __end_element_handler(parser : Dynamic, name : String) {
  52. build = build.getParent();
  53. }
  54. private static function __character_data_handler(parser : Dynamic, data : String) {
  55. // TODO: this function can probably be simplified
  56. var lc : PhpXml__ = (build._children == null || untyped __call__("count", build._children) == 0) ? null : build._children[untyped __call__("count", build._children) -1];
  57. if(lc != null && Xml.PCData == lc.nodeType) {
  58. lc.nodeValue = lc.nodeValue + untyped __call__("htmlentities", data);
  59. } else if((untyped __call__("strlen", data) == 1 && __call__("htmlentities", data) != data) || untyped __call__("htmlentities", data) == data) {
  60. build.addChild(createPCData(untyped __call__("htmlentities", data)));
  61. } else
  62. build.addChild(createCData(data));
  63. }
  64. private static function __default_handler(parser : Dynamic, data : String) {
  65. build.addChild(createPCData(data));
  66. }
  67. static var xmlChecker = new EReg("\\s*(<\\?xml|<!DOCTYPE)", "mi");
  68. public static function parse( str : String ) : PhpXml__ {
  69. build = createDocument();
  70. var xml_parser = untyped __call__("xml_parser_create");
  71. untyped __call__("xml_set_element_handler", xml_parser, __start_element_handler, __end_element_handler);
  72. untyped __call__("xml_set_character_data_handler", xml_parser, __character_data_handler);
  73. untyped __call__("xml_set_default_handler", xml_parser, __default_handler);
  74. untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_CASE_FOLDING"), 0);
  75. untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_SKIP_WHITE"), 0);
  76. var isComplete = xmlChecker.match(str);
  77. if(!isComplete)
  78. str = "<doc>"+str+"</doc>";
  79. if(untyped __call__("xml_parse", xml_parser, str, true) != 1) {
  80. throw "Xml parse error ("+untyped __call__("xml_error_string", xml_parser) + ") line #" + __call__("xml_get_current_line_number", xml_parser);
  81. }
  82. untyped __call__("xml_parser_free", xml_parser);
  83. if(isComplete) {
  84. return build;
  85. } else {
  86. build = build._children[0];
  87. build._parent = null;
  88. build._nodeName = null;
  89. build.nodeType = Document;
  90. return build;
  91. }
  92. }
  93. private function new(){
  94. }
  95. public static function createElement( name : String ) : PhpXml__ {
  96. var r = new PhpXml__();
  97. r.nodeType = Xml.Element;
  98. r._children = new Array();
  99. r._attributes = new Hash();
  100. r.setNodeName( name );
  101. return r;
  102. }
  103. public static function createPCData( data : String ) : PhpXml__ {
  104. var r = new PhpXml__();
  105. r.nodeType = Xml.PCData;
  106. r.setNodeValue( data );
  107. return r;
  108. }
  109. public static function createCData( data : String ) : PhpXml__ {
  110. var r = new PhpXml__();
  111. r.nodeType = Xml.CData;
  112. r.setNodeValue( data );
  113. return r;
  114. }
  115. public static function createComment( data : String ) : PhpXml__ {
  116. var r = new PhpXml__();
  117. r.nodeType = Xml.Comment;
  118. r.setNodeValue( data );
  119. return r;
  120. }
  121. public static function createDocType( data : String ) : PhpXml__ {
  122. var r = new PhpXml__();
  123. r.nodeType = Xml.DocType;
  124. r.setNodeValue( data );
  125. return r;
  126. }
  127. public static function createProlog( data : String ) : PhpXml__ {
  128. var r = new PhpXml__();
  129. r.nodeType = Xml.Prolog;
  130. r.setNodeValue( data );
  131. return r;
  132. }
  133. public static function createDocument() : PhpXml__ {
  134. var r = new PhpXml__();
  135. r.nodeType = Xml.Document;
  136. r._children = new Array();
  137. return r;
  138. }
  139. private function getNodeName() : String {
  140. if( nodeType != Xml.Element )
  141. throw "bad nodeType";
  142. return _nodeName;
  143. }
  144. private function setNodeName( n : String ) : String {
  145. if( nodeType != Xml.Element )
  146. throw "bad nodeType";
  147. return _nodeName = n;
  148. }
  149. private function getNodeValue() : String {
  150. if( nodeType == Xml.Element || nodeType == Xml.Document )
  151. throw "bad nodeType";
  152. return _nodeValue;
  153. }
  154. private function setNodeValue( v : String ) : String {
  155. if( nodeType == Xml.Element || nodeType == Xml.Document )
  156. throw "bad nodeType";
  157. return _nodeValue = v;
  158. }
  159. private function getParent() {
  160. return _parent;
  161. }
  162. public function get( att : String ) : String {
  163. if( nodeType != Xml.Element )
  164. throw "bad nodeType";
  165. return _attributes.get( att );
  166. }
  167. public function set( att : String, value : String ) : Void {
  168. if( nodeType != Xml.Element )
  169. throw "bad nodeType";
  170. _attributes.set( att, untyped __call__("htmlspecialchars", value, __php__('ENT_COMPAT'), 'UTF-8'));
  171. }
  172. public function remove( att : String ) : Void{
  173. if( nodeType != Xml.Element )
  174. throw "bad nodeType";
  175. _attributes.remove( att );
  176. }
  177. public function exists( att : String ) : Bool {
  178. if( nodeType != Xml.Element )
  179. throw "bad nodeType";
  180. return _attributes.exists( att );
  181. }
  182. public function attributes() : Iterator<String> {
  183. if( nodeType != Xml.Element )
  184. throw "bad nodeType";
  185. return _attributes.keys();
  186. }
  187. public function iterator() : Iterator<PhpXml__> {
  188. if( _children == null ) throw "bad nodetype";
  189. var me = this;
  190. var it = null;
  191. it = untyped {
  192. cur: 0,
  193. x: me._children,
  194. hasNext : function(){
  195. return it.cur < __call__("count", it.x);
  196. },
  197. next : function(){
  198. return it.x[it.cur++];
  199. }
  200. }
  201. return cast it;
  202. }
  203. public function elements() : Iterator<PhpXml__> {
  204. if( _children == null ) throw "bad nodetype";
  205. var me = this;
  206. var it = null;
  207. it = untyped {
  208. cur: 0,
  209. x: me._children,
  210. hasNext : function() {
  211. var k = it.cur;
  212. var l = __call__("count", it.x);
  213. while( k < l ) {
  214. if( it.x[k].nodeType == Xml.Element )
  215. __php__("break");
  216. k += 1;
  217. }
  218. it.cur = k;
  219. return k < l;
  220. },
  221. next : function() {
  222. var k = it.cur;
  223. var l = __call__("count", it.x);
  224. while( k < l ) {
  225. var n = it.x[k];
  226. k += 1;
  227. if( n.nodeType == Xml.Element ) {
  228. it.cur = k;
  229. return n;
  230. }
  231. }
  232. return null;
  233. }
  234. }
  235. return cast it;
  236. }
  237. public function elementsNamed( name : String ) : Iterator<PhpXml__> {
  238. if( _children == null ) throw "bad nodetype";
  239. var me = this;
  240. var it = null;
  241. it = untyped {
  242. cur: 0,
  243. x: me._children,
  244. hasNext : function() {
  245. var k = it.cur;
  246. var l = __call__("count", it.x);
  247. while( k < l ) {
  248. var n = it.x[k];
  249. if( n.nodeType == Xml.Element && n._nodeName == name )
  250. __php__("break");
  251. k++;
  252. }
  253. it.cur = k;
  254. return k < l;
  255. },
  256. next : function() {
  257. var k = it.cur;
  258. var l = __call__("count", it.x);
  259. while( k < l ) {
  260. var n = it.x[k];
  261. k++;
  262. if( n.nodeType == Xml.Element && n._nodeName == name ) {
  263. it.cur = k;
  264. return n;
  265. }
  266. }
  267. return null;
  268. }
  269. }
  270. return cast it;
  271. }
  272. public function firstChild() : PhpXml__ {
  273. if( _children == null ) throw "bad nodetype";
  274. if( _children.length == 0 ) return null;
  275. return _children[0];
  276. }
  277. public function firstElement() : PhpXml__ {
  278. if( _children == null ) throw "bad nodetype";
  279. var cur = 0;
  280. var l = _children.length;
  281. while( cur < l ) {
  282. var n = _children[cur];
  283. if( n.nodeType == Xml.Element )
  284. return n;
  285. cur++;
  286. }
  287. return null;
  288. }
  289. public function addChild( x : PhpXml__ ) : Void {
  290. if( _children == null ) throw "bad nodetype";
  291. if( x._parent != null ) x._parent._children.remove(x);
  292. x._parent = this;
  293. _children.push( x );
  294. }
  295. public function removeChild( x : PhpXml__ ) : Bool {
  296. if( _children == null ) throw "bad nodetype";
  297. var b = _children.remove( x );
  298. if( b )
  299. x._parent = null;
  300. return b;
  301. }
  302. public function insertChild( x : PhpXml__, pos : Int ) : Void {
  303. if( _children == null ) throw "bad nodetype";
  304. if( x._parent != null ) x._parent._children.remove(x);
  305. x._parent = this;
  306. _children.insert( pos, x );
  307. }
  308. public function toString() {
  309. if( nodeType == Xml.PCData )
  310. return _nodeValue;
  311. if( nodeType == Xml.CData )
  312. return "<![CDATA["+_nodeValue+"]]>";
  313. if( nodeType == Xml.Comment || nodeType == Xml.DocType || nodeType == Xml.Prolog )
  314. return _nodeValue;
  315. var s = "";
  316. if( nodeType == Xml.Element ) {
  317. s += "<";
  318. s += _nodeName;
  319. for( k in _attributes.keys() ){
  320. s += " ";
  321. s += k;
  322. s += "=\""; // \"
  323. s += _attributes.get(k);
  324. s += "\""; // \"
  325. }
  326. if( _children.length == 0 ) {
  327. s += "/>";
  328. return s;
  329. }
  330. s += ">";
  331. }
  332. for( x in iterator() )
  333. s += x.toString();
  334. if( nodeType == Xml.Element ) {
  335. s += "</";
  336. s += _nodeName;
  337. s += ">";
  338. }
  339. return s;
  340. }
  341. static function __init__() : Void untyped {
  342. PhpXml__.Element = "element";
  343. PhpXml__.PCData = "pcdata";
  344. PhpXml__.CData = "cdata";
  345. PhpXml__.Comment = "comment";
  346. PhpXml__.DocType = "doctype";
  347. PhpXml__.Prolog = "prolog";
  348. PhpXml__.Document = "document";
  349. }
  350. }