Xml.hx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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__("while(list($k, $v) = each($attribs)) $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. // TODO: this function can probably be simplified
  57. // var lc : Xml = (build._children == null || build._children.length == 0) ? null : build._children[build._children.length-1];
  58. // if(lc != null && Xml.PCData == lc.nodeType) {
  59. // lc.nodeValue = lc.nodeValue + untyped __call__("htmlentities", data);
  60. // } else
  61. if((untyped __call__("strlen", data) == 1 && __call__("htmlentities", data) != data) || untyped __call__("htmlentities", data) == data) {
  62. build.addChild(createPCData(untyped __call__("htmlentities", data)));
  63. } else
  64. build.addChild(createCData(data));
  65. }
  66. private static function __default_handler(parser : Dynamic, data : String) : Void {
  67. build.addChild(createPCData(data));
  68. }
  69. static var xmlChecker = new EReg("\\s*(<\\?xml|<!DOCTYPE)", "mi");
  70. public static function parse( str : String ) : Xml {
  71. build = createDocument();
  72. var xml_parser = untyped __call__("xml_parser_create");
  73. untyped __call__("xml_set_element_handler", xml_parser, __start_element_handler, __end_element_handler);
  74. untyped __call__("xml_set_character_data_handler", xml_parser, __character_data_handler);
  75. untyped __call__("xml_set_default_handler", xml_parser, __default_handler);
  76. untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_CASE_FOLDING"), 0);
  77. untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_SKIP_WHITE"), 0);
  78. var isComplete = xmlChecker.match(str);
  79. if(!isComplete)
  80. str = "<doc>"+str+"</doc>";
  81. if(1 != untyped __call__("xml_parse", xml_parser, str, true)) {
  82. 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);
  83. }
  84. untyped __call__("xml_parser_free", xml_parser);
  85. if(isComplete) {
  86. return build;
  87. } else {
  88. build = build._children[0];
  89. build._parent = null;
  90. build._nodeName = null;
  91. build.nodeType = Document;
  92. return build;
  93. }
  94. }
  95. private function new() : Void;
  96. public static function createElement( name : String ) : Xml {
  97. var r = new Xml();
  98. r.nodeType = Xml.Element;
  99. r._children = new Array();
  100. r._attributes = new Hash();
  101. r.setNodeName( name );
  102. return r;
  103. }
  104. public static function createPCData( data : String ) : Xml {
  105. var r = new Xml();
  106. r.nodeType = Xml.PCData;
  107. r.setNodeValue( data );
  108. return r;
  109. }
  110. public static function createCData( data : String ) : Xml {
  111. var r = new Xml();
  112. r.nodeType = Xml.CData;
  113. r.setNodeValue( data );
  114. return r;
  115. }
  116. public static function createComment( data : String ) : Xml {
  117. var r = new Xml();
  118. r.nodeType = Xml.Comment;
  119. r.setNodeValue( data );
  120. return r;
  121. }
  122. public static function createDocType( data : String ) : Xml {
  123. var r = new Xml();
  124. r.nodeType = Xml.DocType;
  125. r.setNodeValue( data );
  126. return r;
  127. }
  128. public static function createProlog( data : String ) : Xml {
  129. var r = new Xml();
  130. r.nodeType = Xml.Prolog;
  131. r.setNodeValue( data );
  132. return r;
  133. }
  134. public static function createDocument() : Xml {
  135. var r = new Xml();
  136. r.nodeType = Xml.Document;
  137. r._children = new Array();
  138. return r;
  139. }
  140. private function getNodeName() : String {
  141. if( nodeType != Xml.Element )
  142. throw "bad nodeType";
  143. return _nodeName;
  144. }
  145. private function setNodeName( n : String ) : String {
  146. if( nodeType != Xml.Element )
  147. throw "bad nodeType";
  148. return _nodeName = n;
  149. }
  150. private function getNodeValue() : String {
  151. if( nodeType == Xml.Element || nodeType == Xml.Document )
  152. throw "bad nodeType";
  153. return _nodeValue;
  154. }
  155. private function setNodeValue( v : String ) : String {
  156. if( nodeType == Xml.Element || nodeType == Xml.Document )
  157. throw "bad nodeType";
  158. return _nodeValue = v;
  159. }
  160. private function getParent() : Xml {
  161. return _parent;
  162. }
  163. public function get( att : String ) : String {
  164. if( nodeType != Xml.Element )
  165. throw "bad nodeType";
  166. return _attributes.get( att );
  167. }
  168. public function set( att : String, value : String ) : Void {
  169. if( nodeType != Xml.Element )
  170. throw "bad nodeType";
  171. _attributes.set( att, untyped __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. }