PageRenderTime 65ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/std/php/_std/Xml.hx

https://github.com/MarcWeber/haxe-compiler-experiments
Haxe | 360 lines | 281 code | 56 blank | 23 comment | 97 complexity | b37e6f5d69643ced854da4ae4602a828 MD5 | raw file
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. import php.Lib;
  23. enum XmlType {
  24. }
  25. @:coreApi class Xml {
  26. public static var Element(default,null) : XmlType;
  27. public static var PCData(default,null) : XmlType;
  28. public static var CData(default,null) : XmlType;
  29. public static var Comment(default,null) : XmlType;
  30. public static var DocType(default,null) : XmlType;
  31. public static var ProcessingInstruction(default,null) : XmlType;
  32. public static var Document(default,null) : XmlType;
  33. public var nodeType(default,null) : XmlType;
  34. public var nodeName(get,set) : String;
  35. public var nodeValue(get,set) : String;
  36. public var parent(get,null) : Xml;
  37. var _nodeName : String;
  38. var _nodeValue : String;
  39. var _attributes : haxe.ds.StringMap<String>;
  40. var _children : Array<Xml>;
  41. var _parent : Xml;
  42. var _fromCustomParser:Bool;
  43. private static var build : Xml;
  44. private static function __start_element_handler(parser : Dynamic, name : String, attribs : ArrayAccess<String>) : Void {
  45. var node = createElement(name);
  46. untyped __php__("foreach($attribs as $k => $v) $node->set($k, $v)");
  47. build.addChild(node);
  48. build = node;
  49. }
  50. private static function __end_element_handler(parser : Dynamic, name : String) : Void {
  51. build = build.parent;
  52. }
  53. private static function __decodeattr(value : String) : String
  54. {
  55. return untyped __call__("str_replace", "'", '&apos;', __call__("htmlspecialchars", value, __php__('ENT_COMPAT'), 'UTF-8'));
  56. }
  57. private static function __decodeent(value : String) : String
  58. {
  59. return untyped __call__("str_replace", "'", '&apos;', __call__("htmlentities", value, __php__('ENT_COMPAT'), 'UTF-8'));
  60. }
  61. private static function __character_data_handler(parser : Dynamic, data : String) : Void {
  62. var d = __decodeent(data);
  63. if ((untyped __call__("strlen", data) == 1 && d != data) || d == data) {
  64. var last = build._children[build._children.length - 1];
  65. if (null != last && last.nodeType == Xml.PCData)
  66. {
  67. last.nodeValue += d;
  68. } else
  69. build.addChild(createPCData(d));
  70. } else {
  71. build.addChild(createCData(data));
  72. }
  73. }
  74. private static function __default_handler(parser : Dynamic, data : String) : Void {
  75. //On some PHP setups (seems to happen when libexpat is used) we may get called for such "entities" although character_data will correctly be called afterward.
  76. if(data == "<![CDATA[")
  77. return;
  78. if(data == "]]>")
  79. return;
  80. if ("<!--" == data.substr(0, 4))
  81. build.addChild(createComment(data.substr(4, data.length-7)));
  82. else
  83. build.addChild(createPCData(data));
  84. }
  85. static var reHeader = ~/\s*(?:<\?(.+?)\?>)?(?:<!DOCTYPE ([^>]+)>)?/mi;
  86. public static function parse( str : String ) : Xml {
  87. build = createDocument();
  88. var xml_parser = untyped __call__("xml_parser_create");
  89. untyped __call__("xml_set_element_handler", xml_parser, __start_element_handler, __end_element_handler);
  90. untyped __call__("xml_set_character_data_handler", xml_parser, __character_data_handler);
  91. untyped __call__("xml_set_default_handler", xml_parser, __default_handler);
  92. untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_CASE_FOLDING"), 0);
  93. untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_SKIP_WHITE"), 0);
  94. reHeader.match(str);
  95. str = "<doc>"+reHeader.matchedRight()+"</doc>";
  96. if(1 != untyped __call__("xml_parse", xml_parser, str, true)) {
  97. 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);
  98. }
  99. untyped __call__("xml_parser_free", xml_parser);
  100. build = build._children[0];
  101. build._parent = null;
  102. build._nodeName = null;
  103. build.nodeType = Document;
  104. var doctype = reHeader.matched(2);
  105. if (null != doctype)
  106. build.insertChild(createDocType(doctype), 0);
  107. var ProcessingInstruction = reHeader.matched(1);
  108. if (null != ProcessingInstruction)
  109. build.insertChild(createProcessingInstruction(ProcessingInstruction), 0);
  110. return build;
  111. }
  112. private function new(fromCustomParser:Bool = false) : Void {
  113. _fromCustomParser = fromCustomParser;
  114. }
  115. @:allow(haxe.xml.Parser)
  116. static function createPCDataFromCustomParser( data : String ) : Xml {
  117. var r = new Xml(true);
  118. r.nodeType = Xml.PCData;
  119. r.set_nodeValue( data );
  120. return r;
  121. }
  122. public static function createElement( name : String ) : Xml {
  123. var r = new Xml();
  124. r.nodeType = Xml.Element;
  125. r._children = new Array();
  126. r._attributes = new haxe.ds.StringMap();
  127. r.set_nodeName( name );
  128. return r;
  129. }
  130. public static function createPCData( data : String ) : Xml {
  131. var r = new Xml();
  132. r.nodeType = Xml.PCData;
  133. r.set_nodeValue( data );
  134. return r;
  135. }
  136. public static function createCData( data : String ) : Xml {
  137. var r = new Xml();
  138. r.nodeType = Xml.CData;
  139. r.set_nodeValue( data );
  140. return r;
  141. }
  142. public static function createComment( data : String ) : Xml {
  143. var r = new Xml();
  144. r.nodeType = Xml.Comment;
  145. r.set_nodeValue( data );
  146. return r;
  147. }
  148. public static function createDocType( data : String ) : Xml {
  149. var r = new Xml();
  150. r.nodeType = Xml.DocType;
  151. r.set_nodeValue( data );
  152. return r;
  153. }
  154. public static function createProcessingInstruction( data : String ) : Xml {
  155. var r = new Xml();
  156. r.nodeType = Xml.ProcessingInstruction;
  157. r.set_nodeValue( data );
  158. return r;
  159. }
  160. public static function createDocument() : Xml {
  161. var r = new Xml();
  162. r.nodeType = Xml.Document;
  163. r._children = new Array();
  164. return r;
  165. }
  166. private function get_nodeName() : String {
  167. if( nodeType != Xml.Element )
  168. throw "bad nodeType";
  169. return _nodeName;
  170. }
  171. private function set_nodeName( n : String ) : String {
  172. if( nodeType != Xml.Element )
  173. throw "bad nodeType";
  174. return _nodeName = n;
  175. }
  176. private function get_nodeValue() : String {
  177. if( nodeType == Xml.Element || nodeType == Xml.Document )
  178. throw "bad nodeType";
  179. return _nodeValue;
  180. }
  181. private function set_nodeValue( v : String ) : String {
  182. if( nodeType == Xml.Element || nodeType == Xml.Document )
  183. throw "bad nodeType";
  184. return _nodeValue = v;
  185. }
  186. private inline function get_parent() : Xml {
  187. return _parent;
  188. }
  189. public function get( att : String ) : String {
  190. if( nodeType != Xml.Element )
  191. throw "bad nodeType";
  192. return _attributes.get( att );
  193. }
  194. // TODO: check correct transform function
  195. @:ifFeature("Xml.parse")
  196. public function set( att : String, value : String ) : Void {
  197. if( nodeType != Xml.Element )
  198. throw "bad nodeType";
  199. _attributes.set( att, __decodeattr(value) );
  200. }
  201. public function remove( att : String ) : Void{
  202. if( nodeType != Xml.Element )
  203. throw "bad nodeType";
  204. _attributes.remove( att );
  205. }
  206. public function exists( att : String ) : Bool {
  207. if( nodeType != Xml.Element )
  208. throw "bad nodeType";
  209. return _attributes.exists( att );
  210. }
  211. public function attributes() : Iterator<String> {
  212. if( nodeType != Xml.Element )
  213. throw "bad nodeType";
  214. return _attributes.keys();
  215. }
  216. public function iterator() : Iterator<Xml> {
  217. if( _children == null ) throw "bad nodetype";
  218. return _children.iterator();
  219. }
  220. public function elements() : Iterator<Xml> {
  221. if( _children == null ) throw "bad nodetype";
  222. return Lambda.filter(_children, function(child) return child.nodeType == Xml.Element).iterator();
  223. }
  224. public function elementsNamed( name : String ) : Iterator<Xml> {
  225. if( _children == null ) throw "bad nodetype";
  226. return Lambda.filter(_children, function(child) return child.nodeType == Xml.Element && child.nodeName == name).iterator();
  227. }
  228. public function firstChild() : Xml {
  229. if( _children == null ) throw "bad nodetype";
  230. if( _children.length == 0 ) return null;
  231. return _children[0];
  232. }
  233. public function firstElement() : Xml {
  234. if( _children == null ) throw "bad nodetype";
  235. for (child in _children)
  236. if (child.nodeType == Xml.Element)
  237. return child;
  238. return null;
  239. }
  240. public function addChild( x : Xml ) : Void {
  241. if( _children == null ) throw "bad nodetype";
  242. if( x._parent != null ) x._parent._children.remove(x);
  243. x._parent = this;
  244. _children.push( x );
  245. }
  246. public function removeChild( x : Xml ) : Bool {
  247. if( _children == null ) throw "bad nodetype";
  248. var b = _children.remove( x );
  249. if( b )
  250. x._parent = null;
  251. return b;
  252. }
  253. public function insertChild( x : Xml, pos : Int ) : Void {
  254. if( _children == null ) throw "bad nodetype";
  255. if( x._parent != null ) x._parent._children.remove(x);
  256. x._parent = this;
  257. _children.insert( pos, x );
  258. }
  259. public function toString() : String {
  260. if( nodeType == Xml.PCData )
  261. return _fromCustomParser ? StringTools.htmlEscape(_nodeValue) : _nodeValue;
  262. var s = "";
  263. if( nodeType == Xml.Element ) {
  264. s += "<";
  265. s += _nodeName;
  266. for( k in _attributes.keys() ){
  267. s += " ";
  268. s += k;
  269. s += "=\""; // \"
  270. s += _attributes.get(k);
  271. s += "\""; // \"
  272. }
  273. if( _children.length == 0 ) {
  274. s += "/>";
  275. return s;
  276. }
  277. s += ">";
  278. } else if( nodeType == Xml.CData )
  279. return "<![CDATA["+_nodeValue+"]]>";
  280. else if( nodeType == Xml.Comment )
  281. return "<!--"+_nodeValue+"-->";
  282. else if( nodeType == Xml.DocType )
  283. return "<!DOCTYPE "+_nodeValue+">";
  284. else if ( nodeType == Xml.ProcessingInstruction )
  285. return "<?"+_nodeValue+"?>";
  286. for( x in iterator() )
  287. s += x.toString();
  288. if( nodeType == Xml.Element ) {
  289. s += "</";
  290. s += _nodeName;
  291. s += ">";
  292. }
  293. return s;
  294. }
  295. static function __init__() : Void untyped {
  296. Xml.Element = "element";
  297. Xml.PCData = "pcdata";
  298. Xml.CData = "cdata";
  299. Xml.Comment = "comment";
  300. Xml.DocType = "doctype";
  301. Xml.ProcessingInstruction = "processingInstruction";
  302. Xml.Document = "document";
  303. }
  304. }