Xml.hx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. if ("<!--" == data.substr(0, 4))
  72. build.addChild(createComment(data.substr(4, data.length-7)));
  73. else
  74. build.addChild(createPCData(data));
  75. }
  76. static var reHeader = ~/\s*(?:<\?(.+?)\?>)?(?:<!DOCTYPE ([^>]+)>)?/mi;
  77. public static function parse( str : String ) : Xml {
  78. build = createDocument();
  79. var xml_parser = untyped __call__("xml_parser_create");
  80. untyped __call__("xml_set_element_handler", xml_parser, __start_element_handler, __end_element_handler);
  81. untyped __call__("xml_set_character_data_handler", xml_parser, __character_data_handler);
  82. untyped __call__("xml_set_default_handler", xml_parser, __default_handler);
  83. untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_CASE_FOLDING"), 0);
  84. untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_SKIP_WHITE"), 0);
  85. reHeader.match(str);
  86. str = "<doc>"+reHeader.matchedRight()+"</doc>";
  87. if(1 != untyped __call__("xml_parse", xml_parser, str, true)) {
  88. 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);
  89. }
  90. untyped __call__("xml_parser_free", xml_parser);
  91. build = build._children[0];
  92. build._parent = null;
  93. build._nodeName = null;
  94. build.nodeType = Document;
  95. var doctype = reHeader.matched(2);
  96. if (null != doctype)
  97. build.insertChild(createDocType(doctype), 0);
  98. var prolog = reHeader.matched(1);
  99. if (null != prolog)
  100. build.insertChild(createProlog(prolog), 0);
  101. return build;
  102. }
  103. private function new() : Void {}
  104. public static function createElement( name : String ) : Xml {
  105. var r = new Xml();
  106. r.nodeType = Xml.Element;
  107. r._children = new Array();
  108. r._attributes = new Hash();
  109. r.setNodeName( name );
  110. return r;
  111. }
  112. public static function createPCData( data : String ) : Xml {
  113. var r = new Xml();
  114. r.nodeType = Xml.PCData;
  115. r.setNodeValue( data );
  116. return r;
  117. }
  118. public static function createCData( data : String ) : Xml {
  119. var r = new Xml();
  120. r.nodeType = Xml.CData;
  121. r.setNodeValue( data );
  122. return r;
  123. }
  124. public static function createComment( data : String ) : Xml {
  125. var r = new Xml();
  126. r.nodeType = Xml.Comment;
  127. r.setNodeValue( data );
  128. return r;
  129. }
  130. public static function createDocType( data : String ) : Xml {
  131. var r = new Xml();
  132. r.nodeType = Xml.DocType;
  133. r.setNodeValue( data );
  134. return r;
  135. }
  136. public static function createProlog( data : String ) : Xml {
  137. var r = new Xml();
  138. r.nodeType = Xml.Prolog;
  139. r.setNodeValue( data );
  140. return r;
  141. }
  142. public static function createDocument() : Xml {
  143. var r = new Xml();
  144. r.nodeType = Xml.Document;
  145. r._children = new Array();
  146. return r;
  147. }
  148. private function getNodeName() : String {
  149. if( nodeType != Xml.Element )
  150. throw "bad nodeType";
  151. return _nodeName;
  152. }
  153. private function setNodeName( n : String ) : String {
  154. if( nodeType != Xml.Element )
  155. throw "bad nodeType";
  156. return _nodeName = n;
  157. }
  158. private function getNodeValue() : String {
  159. if( nodeType == Xml.Element || nodeType == Xml.Document )
  160. throw "bad nodeType";
  161. return _nodeValue;
  162. }
  163. private function setNodeValue( v : String ) : String {
  164. if( nodeType == Xml.Element || nodeType == Xml.Document )
  165. throw "bad nodeType";
  166. return _nodeValue = v;
  167. }
  168. private function getParent() : Xml {
  169. return _parent;
  170. }
  171. public function get( att : String ) : String {
  172. if( nodeType != Xml.Element )
  173. throw "bad nodeType";
  174. return _attributes.get( att );
  175. }
  176. // TODO: check correct transform function
  177. public function set( att : String, value : String ) : Void {
  178. if( nodeType != Xml.Element )
  179. throw "bad nodeType";
  180. _attributes.set( att, __decodeattr(value) );
  181. }
  182. public function remove( att : String ) : Void{
  183. if( nodeType != Xml.Element )
  184. throw "bad nodeType";
  185. _attributes.remove( att );
  186. }
  187. public function exists( att : String ) : Bool {
  188. if( nodeType != Xml.Element )
  189. throw "bad nodeType";
  190. return _attributes.exists( att );
  191. }
  192. public function attributes() : Iterator<String> {
  193. if( nodeType != Xml.Element )
  194. throw "bad nodeType";
  195. return _attributes.keys();
  196. }
  197. public function iterator() : Iterator<Xml> {
  198. if( _children == null ) throw "bad nodetype";
  199. var me = this;
  200. var it = null;
  201. it = untyped {
  202. cur: 0,
  203. x: me._children,
  204. hasNext : function(){
  205. return it.cur < it.x.length;
  206. },
  207. next : function(){
  208. return it.x[it.cur++];
  209. }
  210. }
  211. return cast it;
  212. }
  213. public function elements() : Iterator<Xml> {
  214. if( _children == null ) throw "bad nodetype";
  215. var me = this;
  216. var it = null;
  217. it = untyped {
  218. cur: 0,
  219. x: me._children,
  220. hasNext : function() {
  221. var k = it.cur;
  222. var l = it.x.length;
  223. while( k < l ) {
  224. if( it.x[k].nodeType == Xml.Element )
  225. __php__("break");
  226. k += 1;
  227. }
  228. it.cur = k;
  229. return k < l;
  230. },
  231. next : function() {
  232. var k = it.cur;
  233. var l = it.x.length;
  234. while( k < l ) {
  235. var n = it.x[k];
  236. k += 1;
  237. if( n.nodeType == Xml.Element ) {
  238. it.cur = k;
  239. return n;
  240. }
  241. }
  242. return null;
  243. }
  244. }
  245. return cast it;
  246. }
  247. public function elementsNamed( name : String ) : Iterator<Xml> {
  248. if( _children == null ) throw "bad nodetype";
  249. var me = this;
  250. var it = null;
  251. it = untyped {
  252. cur: 0,
  253. x: me._children,
  254. hasNext : function() {
  255. var k = it.cur;
  256. var l = it.x.length;
  257. while( k < l ) {
  258. var n = it.x[k];
  259. if( n.nodeType == Xml.Element && n._nodeName == name )
  260. __php__("break");
  261. k++;
  262. }
  263. it.cur = k;
  264. return k < l;
  265. },
  266. next : function() {
  267. var k = it.cur;
  268. var l = it.x.length;
  269. while( k < l ) {
  270. var n = it.x[k];
  271. k++;
  272. if( n.nodeType == Xml.Element && n._nodeName == name ) {
  273. it.cur = k;
  274. return n;
  275. }
  276. }
  277. return null;
  278. }
  279. }
  280. return cast it;
  281. }
  282. public function firstChild() : Xml {
  283. if( _children == null ) throw "bad nodetype";
  284. if( _children.length == 0 ) return null;
  285. return _children[0];
  286. }
  287. public function firstElement() : Xml {
  288. if( _children == null ) throw "bad nodetype";
  289. var cur = 0;
  290. var l = _children.length;
  291. while( cur < l ) {
  292. var n = _children[cur];
  293. if( n.nodeType == Xml.Element )
  294. return n;
  295. cur++;
  296. }
  297. return null;
  298. }
  299. public function addChild( x : Xml ) : Void {
  300. if( _children == null ) throw "bad nodetype";
  301. if( x._parent != null ) x._parent._children.remove(x);
  302. x._parent = this;
  303. _children.push( x );
  304. }
  305. public function removeChild( x : Xml ) : Bool {
  306. if( _children == null ) throw "bad nodetype";
  307. var b = _children.remove( x );
  308. if( b )
  309. x._parent = null;
  310. return b;
  311. }
  312. public function insertChild( x : Xml, pos : Int ) : Void {
  313. if( _children == null ) throw "bad nodetype";
  314. if( x._parent != null ) x._parent._children.remove(x);
  315. x._parent = this;
  316. _children.insert( pos, x );
  317. }
  318. public function toString() : String {
  319. if( nodeType == Xml.PCData )
  320. return _nodeValue;
  321. var s = "";
  322. if( nodeType == Xml.Element ) {
  323. s += "<";
  324. s += _nodeName;
  325. for( k in _attributes.keys() ){
  326. s += " ";
  327. s += k;
  328. s += "=\""; // \"
  329. s += _attributes.get(k);
  330. s += "\""; // \"
  331. }
  332. if( _children.length == 0 ) {
  333. s += "/>";
  334. return s;
  335. }
  336. s += ">";
  337. } else if( nodeType == Xml.CData )
  338. return "<![CDATA["+_nodeValue+"]]>";
  339. else if( nodeType == Xml.Comment )
  340. return "<!--"+_nodeValue+"-->";
  341. else if( nodeType == Xml.DocType )
  342. return "<!DOCTYPE "+_nodeValue+">";
  343. else if ( nodeType == Xml.Prolog )
  344. return "<?"+_nodeValue+"?>";
  345. for( x in iterator() )
  346. s += x.toString();
  347. if( nodeType == Xml.Element ) {
  348. s += "</";
  349. s += _nodeName;
  350. s += ">";
  351. }
  352. return s;
  353. }
  354. static function __init__() : Void untyped {
  355. Xml.Element = "element";
  356. Xml.PCData = "pcdata";
  357. Xml.CData = "cdata";
  358. Xml.Comment = "comment";
  359. Xml.DocType = "doctype";
  360. Xml.Prolog = "prolog";
  361. Xml.Document = "document";
  362. }
  363. }