PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/std/php/_std/Xml.hx

https://github.com/steshaw/haxe-compiler-experiments
Haxe | 405 lines | 324 code | 52 blank | 29 comment | 92 complexity | f2f5d51211b486164d4b2fa36a6b7159 MD5 | raw file
  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. enum XmlType {
  26. }
  27. @:core_api class Xml {
  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) : Xml;
  39. var _nodeName : String;
  40. var _nodeValue : String;
  41. var _attributes : Hash<String>;
  42. var _children : Array<Xml>;
  43. var _parent : Xml;
  44. private static var build : Xml;
  45. private static function __start_element_handler(parser : Dynamic, name : String, attribs : ArrayAccess<String>) : Void {
  46. var node = createElement(name);
  47. untyped __php__("while(list($k, $v) = each($attribs)) $node->set($k, $v)");
  48. build.addChild(node);
  49. build = node;
  50. }
  51. private static function __end_element_handler(parser : Dynamic, name : String) : Void {
  52. build = build.getParent();
  53. }
  54. private static function __character_data_handler(parser : Dynamic, data : String) : Void {
  55. // TODO: this function can probably be simplified
  56. // var lc : Xml = (build._children == null || build._children.length == 0) ? null : build._children[build._children.length-1];
  57. // if(lc != null && Xml.PCData == lc.nodeType) {
  58. // lc.nodeValue = lc.nodeValue + untyped __call__("htmlentities", data);
  59. // } else
  60. if((untyped __call__("strlen", data) == 1 && __call__("htmlentities", data) != data) || untyped __call__("htmlentities", data) == data) {
  61. build.addChild(createPCData(untyped __call__("htmlentities", data)));
  62. } else
  63. build.addChild(createCData(data));
  64. }
  65. private static function __default_handler(parser : Dynamic, data : String) : Void {
  66. build.addChild(createPCData(data));
  67. }
  68. static var xmlChecker = new EReg("\\s*(<\\?xml|<!DOCTYPE)", "mi");
  69. public static function parse( str : String ) : Xml {
  70. build = createDocument();
  71. var xml_parser = untyped __call__("xml_parser_create");
  72. untyped __call__("xml_set_element_handler", xml_parser, __start_element_handler, __end_element_handler);
  73. untyped __call__("xml_set_character_data_handler", xml_parser, __character_data_handler);
  74. untyped __call__("xml_set_default_handler", xml_parser, __default_handler);
  75. untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_CASE_FOLDING"), 0);
  76. untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_SKIP_WHITE"), 0);
  77. var isComplete = xmlChecker.match(str);
  78. if(!isComplete)
  79. str = "<doc>"+str+"</doc>";
  80. if(1 != untyped __call__("xml_parse", xml_parser, str, true)) {
  81. 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);
  82. }
  83. untyped __call__("xml_parser_free", xml_parser);
  84. if(isComplete) {
  85. return build;
  86. } else {
  87. build = build._children[0];
  88. build._parent = null;
  89. build._nodeName = null;
  90. build.nodeType = Document;
  91. return build;
  92. }
  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. 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<Xml> {
  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 < it.x.length;
  196. },
  197. next : function(){
  198. return it.x[it.cur++];
  199. }
  200. }
  201. return cast it;
  202. }
  203. public function elements() : Iterator<Xml> {
  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 = it.x.length;
  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 = it.x.length;
  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<Xml> {
  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 = it.x.length;
  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 = it.x.length;
  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() : Xml {
  273. if( _children == null ) throw "bad nodetype";
  274. if( _children.length == 0 ) return null;
  275. return _children[0];
  276. }
  277. public function firstElement() : Xml {
  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 : Xml ) : 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 : Xml ) : 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 : Xml, 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() : String {
  309. if( nodeType == Xml.PCData )
  310. return _nodeValue;
  311. var s = "";
  312. if( nodeType == Xml.Element ) {
  313. s += "<";
  314. s += _nodeName;
  315. for( k in _attributes.keys() ){
  316. s += " ";
  317. s += k;
  318. s += "=\""; // \"
  319. s += _attributes.get(k);
  320. s += "\""; // \"
  321. }
  322. if( _children.length == 0 ) {
  323. s += "/>";
  324. return s;
  325. }
  326. s += ">";
  327. } else if( nodeType == Xml.CData )
  328. return "<![CDATA["+_nodeValue+"]]>";
  329. else if( nodeType == Xml.Comment )
  330. return "<!--"+_nodeValue+"-->";
  331. else if( nodeType == Xml.DocType )
  332. return "<!DOCTYPE "+_nodeValue+">";
  333. else if( nodeType == Xml.Prolog )
  334. return "<?"+_nodeValue+"?>";
  335. for( x in iterator() )
  336. s += x.toString();
  337. if( nodeType == Xml.Element ) {
  338. s += "</";
  339. s += _nodeName;
  340. s += ">";
  341. }
  342. return s;
  343. }
  344. static function __init__() : Void untyped {
  345. Xml.Element = "element";
  346. Xml.PCData = "pcdata";
  347. Xml.CData = "cdata";
  348. Xml.Comment = "comment";
  349. Xml.DocType = "doctype";
  350. Xml.Prolog = "prolog";
  351. Xml.Document = "document";
  352. }
  353. }