PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/bundle/haxe-src/std/php/_std/Xml.hx

https://github.com/fullofcaffeine/vimpuccino
Haxe | 408 lines | 329 code | 55 blank | 24 comment | 95 complexity | 52d0a46bc3e87f255c2d37b7b673dafd MD5 | raw file
  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. 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. 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. }