2
0

NativeXml.hx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Copyright (C)2005-2017 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. package php;
  23. import php.Lib;
  24. enum XmlType {
  25. }
  26. typedef NativeXml = Xml;
  27. 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 ProcessingInstruction(default,null) : XmlType;
  34. public static var Document(default,null) : XmlType;
  35. public var nodeType(default,null) : XmlType;
  36. public var nodeName(get,set) : String;
  37. public var nodeValue(get,set) : String;
  38. public var parent(get,null) : Xml;
  39. var _nodeName : String;
  40. var _nodeValue : String;
  41. var _attributes : haxe.ds.StringMap<String>;
  42. var _children : Array<Xml>;
  43. var _parent : Xml;
  44. var _fromCustomParser:Bool;
  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.parent;
  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. var last = build._children[build._children.length - 1];
  67. if (null != last && last.nodeType == Xml.PCData)
  68. {
  69. last.nodeValue += d;
  70. } else
  71. build.addChild(createPCData(d));
  72. } else {
  73. build.addChild(createCData(data));
  74. }
  75. }
  76. private static function __default_handler(parser : Dynamic, data : String) : Void {
  77. //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.
  78. if(data == "<![CDATA[")
  79. return;
  80. if(data == "]]>")
  81. return;
  82. if ("<!--" == data.substr(0, 4))
  83. build.addChild(createComment(data.substr(4, data.length-7)));
  84. else
  85. build.addChild(createPCData(data));
  86. }
  87. static var reHeader = ~/\s*(?:<\?(.+?)\?>)?(?:<!DOCTYPE ([^>]+)>)?/mi;
  88. public static function parse( str : String ) : Xml {
  89. build = createDocument();
  90. var xml_parser = untyped __call__("xml_parser_create");
  91. untyped __call__("xml_set_element_handler", xml_parser, __start_element_handler, __end_element_handler);
  92. untyped __call__("xml_set_character_data_handler", xml_parser, __character_data_handler);
  93. untyped __call__("xml_set_default_handler", xml_parser, __default_handler);
  94. untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_CASE_FOLDING"), 0);
  95. untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_SKIP_WHITE"), 0);
  96. reHeader.match(str);
  97. str = "<doc>"+reHeader.matchedRight()+"</doc>";
  98. if(1 != untyped __call__("xml_parse", xml_parser, str, true)) {
  99. 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);
  100. }
  101. untyped __call__("xml_parser_free", xml_parser);
  102. build = build._children[0];
  103. build._parent = null;
  104. build._nodeName = null;
  105. build.nodeType = Document;
  106. var doctype = reHeader.matched(2);
  107. if (null != doctype)
  108. build.insertChild(createDocType(doctype), 0);
  109. var ProcessingInstruction = reHeader.matched(1);
  110. if (null != ProcessingInstruction)
  111. build.insertChild(createProcessingInstruction(ProcessingInstruction), 0);
  112. return build;
  113. }
  114. private function new(fromCustomParser:Bool = false) : Void {
  115. _fromCustomParser = fromCustomParser;
  116. }
  117. @:allow(haxe.xml.Parser)
  118. static function createPCDataFromCustomParser( data : String ) : Xml {
  119. var r = new Xml(true);
  120. r.nodeType = Xml.PCData;
  121. r.set_nodeValue( data );
  122. return r;
  123. }
  124. public static function createElement( name : String ) : Xml {
  125. var r = new Xml();
  126. r.nodeType = Xml.Element;
  127. r._children = new Array();
  128. r._attributes = new haxe.ds.StringMap();
  129. r.set_nodeName( name );
  130. return r;
  131. }
  132. public static function createPCData( data : String ) : Xml {
  133. var r = new Xml();
  134. r.nodeType = Xml.PCData;
  135. r.set_nodeValue( data );
  136. return r;
  137. }
  138. public static function createCData( data : String ) : Xml {
  139. var r = new Xml();
  140. r.nodeType = Xml.CData;
  141. r.set_nodeValue( data );
  142. return r;
  143. }
  144. public static function createComment( data : String ) : Xml {
  145. var r = new Xml();
  146. r.nodeType = Xml.Comment;
  147. r.set_nodeValue( data );
  148. return r;
  149. }
  150. public static function createDocType( data : String ) : Xml {
  151. var r = new Xml();
  152. r.nodeType = Xml.DocType;
  153. r.set_nodeValue( data );
  154. return r;
  155. }
  156. public static function createProcessingInstruction( data : String ) : Xml {
  157. var r = new Xml();
  158. r.nodeType = Xml.ProcessingInstruction;
  159. r.set_nodeValue( data );
  160. return r;
  161. }
  162. public static function createDocument() : Xml {
  163. var r = new Xml();
  164. r.nodeType = Xml.Document;
  165. r._children = new Array();
  166. return r;
  167. }
  168. private function get_nodeName() : String {
  169. if( nodeType != Xml.Element )
  170. throw "bad nodeType";
  171. return _nodeName;
  172. }
  173. private function set_nodeName( n : String ) : String {
  174. if( nodeType != Xml.Element )
  175. throw "bad nodeType";
  176. return _nodeName = n;
  177. }
  178. private function get_nodeValue() : String {
  179. if( nodeType == Xml.Element || nodeType == Xml.Document )
  180. throw "bad nodeType";
  181. return _nodeValue;
  182. }
  183. private function set_nodeValue( v : String ) : String {
  184. if( nodeType == Xml.Element || nodeType == Xml.Document )
  185. throw "bad nodeType";
  186. return _nodeValue = v;
  187. }
  188. private inline function get_parent() : Xml {
  189. return _parent;
  190. }
  191. public function get( att : String ) : String {
  192. if( nodeType != Xml.Element )
  193. throw "bad nodeType";
  194. return _attributes.get( att );
  195. }
  196. // TODO: check correct transform function
  197. @:ifFeature("Xml.parse")
  198. public function set( att : String, value : String ) : Void {
  199. if( nodeType != Xml.Element )
  200. throw "bad nodeType";
  201. _attributes.set( att, __decodeattr(value) );
  202. }
  203. public function remove( att : String ) : Void{
  204. if( nodeType != Xml.Element )
  205. throw "bad nodeType";
  206. _attributes.remove( att );
  207. }
  208. public function exists( att : String ) : Bool {
  209. if( nodeType != Xml.Element )
  210. throw "bad nodeType";
  211. return _attributes.exists( att );
  212. }
  213. public function attributes() : Iterator<String> {
  214. if( nodeType != Xml.Element )
  215. throw "bad nodeType";
  216. return _attributes.keys();
  217. }
  218. public function iterator() : Iterator<Xml> {
  219. if( _children == null ) throw "bad nodetype";
  220. return _children.iterator();
  221. }
  222. public function elements() : Iterator<Xml> {
  223. if( _children == null ) throw "bad nodetype";
  224. return Lambda.filter(_children, function(child) return child.nodeType == Xml.Element).iterator();
  225. }
  226. public function elementsNamed( name : String ) : Iterator<Xml> {
  227. if( _children == null ) throw "bad nodetype";
  228. return Lambda.filter(_children, function(child) return child.nodeType == Xml.Element && child.nodeName == name).iterator();
  229. }
  230. public function firstChild() : Xml {
  231. if( _children == null ) throw "bad nodetype";
  232. if( _children.length == 0 ) return null;
  233. return _children[0];
  234. }
  235. public function firstElement() : Xml {
  236. if( _children == null ) throw "bad nodetype";
  237. for (child in _children)
  238. if (child.nodeType == Xml.Element)
  239. return child;
  240. return null;
  241. }
  242. public function addChild( x : Xml ) : Void {
  243. if( _children == null ) throw "bad nodetype";
  244. if( x._parent != null ) x._parent._children.remove(x);
  245. x._parent = this;
  246. _children.push( x );
  247. }
  248. public function removeChild( x : Xml ) : Bool {
  249. if( _children == null ) throw "bad nodetype";
  250. var b = _children.remove( x );
  251. if( b )
  252. x._parent = null;
  253. return b;
  254. }
  255. public function insertChild( x : Xml, pos : Int ) : Void {
  256. if( _children == null ) throw "bad nodetype";
  257. if( x._parent != null ) x._parent._children.remove(x);
  258. x._parent = this;
  259. _children.insert( pos, x );
  260. }
  261. public function toString() : String {
  262. if( nodeType == Xml.PCData )
  263. return _fromCustomParser ? StringTools.htmlEscape(_nodeValue) : _nodeValue;
  264. var s = "";
  265. if( nodeType == Xml.Element ) {
  266. s += "<";
  267. s += _nodeName;
  268. for( k in _attributes.keys() ){
  269. s += " ";
  270. s += k;
  271. s += "=\""; // \"
  272. s += _attributes.get(k);
  273. s += "\""; // \"
  274. }
  275. if( _children.length == 0 ) {
  276. s += "/>";
  277. return s;
  278. }
  279. s += ">";
  280. } else if( nodeType == Xml.CData )
  281. return "<![CDATA["+_nodeValue+"]]>";
  282. else if( nodeType == Xml.Comment )
  283. return "<!--"+_nodeValue+"-->";
  284. else if( nodeType == Xml.DocType )
  285. return "<!DOCTYPE "+_nodeValue+">";
  286. else if ( nodeType == Xml.ProcessingInstruction )
  287. return "<?"+_nodeValue+"?>";
  288. for( x in iterator() )
  289. s += x.toString();
  290. if( nodeType == Xml.Element ) {
  291. s += "</";
  292. s += _nodeName;
  293. s += ">";
  294. }
  295. return s;
  296. }
  297. static function __init__() : Void untyped {
  298. Xml.Element = "element";
  299. Xml.PCData = "pcdata";
  300. Xml.CData = "cdata";
  301. Xml.Comment = "comment";
  302. Xml.DocType = "doctype";
  303. Xml.ProcessingInstruction = "processingInstruction";
  304. Xml.Document = "document";
  305. }
  306. }