Xml.hx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 __character_data_handler(parser : Dynamic, data : String) : Void {
  56. if((untyped __call__("strlen", data) == 1 && __call__("htmlentities", data) != data) || untyped __call__("htmlentities", data) == data) {
  57. build.addChild(createPCData(untyped __call__("htmlentities", data)));
  58. } else
  59. build.addChild(createCData(data));
  60. }
  61. private static function __default_handler(parser : Dynamic, data : String) : Void {
  62. if ("<!--" == data.substr(0, 4))
  63. build.addChild(createComment(data.substr(4, data.length-7)));
  64. else
  65. build.addChild(createPCData(data));
  66. }
  67. static var reHeader = ~/\s*(?:<\?(.+?)\?>)?(?:<!DOCTYPE ([^>]+)>)?/mi;
  68. public static function parse( str : String ) : Xml {
  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. reHeader.match(str);
  77. str = "<doc>"+reHeader.matchedRight()+"</doc>";
  78. if(1 != untyped __call__("xml_parse", xml_parser, str, true)) {
  79. 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);
  80. }
  81. untyped __call__("xml_parser_free", xml_parser);
  82. build = build._children[0];
  83. build._parent = null;
  84. build._nodeName = null;
  85. build.nodeType = Document;
  86. var doctype = reHeader.matched(2);
  87. if (null != doctype)
  88. build.insertChild(createDocType(doctype), 0);
  89. var prolog = reHeader.matched(1);
  90. if (null != prolog)
  91. build.insertChild(createProlog(prolog), 0);
  92. return build;
  93. }
  94. private function new() : Void {}
  95. public static function createElement( name : String ) : Xml {
  96. var r = new Xml();
  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 ) : Xml {
  104. var r = new Xml();
  105. r.nodeType = Xml.PCData;
  106. r.setNodeValue( data );
  107. return r;
  108. }
  109. public static function createCData( data : String ) : Xml {
  110. var r = new Xml();
  111. r.nodeType = Xml.CData;
  112. r.setNodeValue( data );
  113. return r;
  114. }
  115. public static function createComment( data : String ) : Xml {
  116. var r = new Xml();
  117. r.nodeType = Xml.Comment;
  118. r.setNodeValue( data );
  119. return r;
  120. }
  121. public static function createDocType( data : String ) : Xml {
  122. var r = new Xml();
  123. r.nodeType = Xml.DocType;
  124. r.setNodeValue( data );
  125. return r;
  126. }
  127. public static function createProlog( data : String ) : Xml {
  128. var r = new Xml();
  129. r.nodeType = Xml.Prolog;
  130. r.setNodeValue( data );
  131. return r;
  132. }
  133. public static function createDocument() : Xml {
  134. var r = new Xml();
  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() : Xml {
  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. // TODO: check correct transform function
  168. public function set( att : String, value : String ) : Void {
  169. if( nodeType != Xml.Element )
  170. throw "bad nodeType";
  171. _attributes.set( att, untyped __call__("str_replace", "'", '&apos;', __call__("htmlspecialchars", value, __php__('ENT_COMPAT'), 'UTF-8')));
  172. }
  173. public function remove( att : String ) : Void{
  174. if( nodeType != Xml.Element )
  175. throw "bad nodeType";
  176. _attributes.remove( att );
  177. }
  178. public function exists( att : String ) : Bool {
  179. if( nodeType != Xml.Element )
  180. throw "bad nodeType";
  181. return _attributes.exists( att );
  182. }
  183. public function attributes() : Iterator<String> {
  184. if( nodeType != Xml.Element )
  185. throw "bad nodeType";
  186. return _attributes.keys();
  187. }
  188. public function iterator() : Iterator<Xml> {
  189. if( _children == null ) throw "bad nodetype";
  190. var me = this;
  191. var it = null;
  192. it = untyped {
  193. cur: 0,
  194. x: me._children,
  195. hasNext : function(){
  196. return it.cur < it.x.length;
  197. },
  198. next : function(){
  199. return it.x[it.cur++];
  200. }
  201. }
  202. return cast it;
  203. }
  204. public function elements() : Iterator<Xml> {
  205. if( _children == null ) throw "bad nodetype";
  206. var me = this;
  207. var it = null;
  208. it = untyped {
  209. cur: 0,
  210. x: me._children,
  211. hasNext : function() {
  212. var k = it.cur;
  213. var l = it.x.length;
  214. while( k < l ) {
  215. if( it.x[k].nodeType == Xml.Element )
  216. __php__("break");
  217. k += 1;
  218. }
  219. it.cur = k;
  220. return k < l;
  221. },
  222. next : function() {
  223. var k = it.cur;
  224. var l = it.x.length;
  225. while( k < l ) {
  226. var n = it.x[k];
  227. k += 1;
  228. if( n.nodeType == Xml.Element ) {
  229. it.cur = k;
  230. return n;
  231. }
  232. }
  233. return null;
  234. }
  235. }
  236. return cast it;
  237. }
  238. public function elementsNamed( name : String ) : Iterator<Xml> {
  239. if( _children == null ) throw "bad nodetype";
  240. var me = this;
  241. var it = null;
  242. it = untyped {
  243. cur: 0,
  244. x: me._children,
  245. hasNext : function() {
  246. var k = it.cur;
  247. var l = it.x.length;
  248. while( k < l ) {
  249. var n = it.x[k];
  250. if( n.nodeType == Xml.Element && n._nodeName == name )
  251. __php__("break");
  252. k++;
  253. }
  254. it.cur = k;
  255. return k < l;
  256. },
  257. next : function() {
  258. var k = it.cur;
  259. var l = it.x.length;
  260. while( k < l ) {
  261. var n = it.x[k];
  262. k++;
  263. if( n.nodeType == Xml.Element && n._nodeName == name ) {
  264. it.cur = k;
  265. return n;
  266. }
  267. }
  268. return null;
  269. }
  270. }
  271. return cast it;
  272. }
  273. public function firstChild() : Xml {
  274. if( _children == null ) throw "bad nodetype";
  275. if( _children.length == 0 ) return null;
  276. return _children[0];
  277. }
  278. public function firstElement() : Xml {
  279. if( _children == null ) throw "bad nodetype";
  280. var cur = 0;
  281. var l = _children.length;
  282. while( cur < l ) {
  283. var n = _children[cur];
  284. if( n.nodeType == Xml.Element )
  285. return n;
  286. cur++;
  287. }
  288. return null;
  289. }
  290. public function addChild( x : Xml ) : Void {
  291. if( _children == null ) throw "bad nodetype";
  292. if( x._parent != null ) x._parent._children.remove(x);
  293. x._parent = this;
  294. _children.push( x );
  295. }
  296. public function removeChild( x : Xml ) : Bool {
  297. if( _children == null ) throw "bad nodetype";
  298. var b = _children.remove( x );
  299. if( b )
  300. x._parent = null;
  301. return b;
  302. }
  303. public function insertChild( x : Xml, pos : Int ) : Void {
  304. if( _children == null ) throw "bad nodetype";
  305. if( x._parent != null ) x._parent._children.remove(x);
  306. x._parent = this;
  307. _children.insert( pos, x );
  308. }
  309. public function toString() : String {
  310. if( nodeType == Xml.PCData )
  311. return _nodeValue;
  312. var s = "";
  313. if( nodeType == Xml.Element ) {
  314. s += "<";
  315. s += _nodeName;
  316. for( k in _attributes.keys() ){
  317. s += " ";
  318. s += k;
  319. s += "=\""; // \"
  320. s += _attributes.get(k);
  321. s += "\""; // \"
  322. }
  323. if( _children.length == 0 ) {
  324. s += "/>";
  325. return s;
  326. }
  327. s += ">";
  328. } else if( nodeType == Xml.CData )
  329. return "<![CDATA["+_nodeValue+"]]>";
  330. else if( nodeType == Xml.Comment )
  331. return "<!--"+_nodeValue+"-->";
  332. else if( nodeType == Xml.DocType )
  333. return "<!DOCTYPE "+_nodeValue+">";
  334. else if ( nodeType == Xml.Prolog )
  335. return "<?"+_nodeValue+"?>";
  336. for( x in iterator() )
  337. s += x.toString();
  338. if( nodeType == Xml.Element ) {
  339. s += "</";
  340. s += _nodeName;
  341. s += ">";
  342. }
  343. return s;
  344. }
  345. static function __init__() : Void untyped {
  346. Xml.Element = "element";
  347. Xml.PCData = "pcdata";
  348. Xml.CData = "cdata";
  349. Xml.Comment = "comment";
  350. Xml.DocType = "doctype";
  351. Xml.Prolog = "prolog";
  352. Xml.Document = "document";
  353. }
  354. }