Xml.hx 11 KB

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