2
0

NativeXml.hx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * Copyright (C)2005-2015 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 cpp;
  23. enum XmlType {
  24. }
  25. typedef NativeXml = Xml;
  26. class Xml {
  27. public static var Element(default,null) : XmlType;
  28. public static var PCData(default,null) : XmlType;
  29. public static var CData(default,null) : XmlType;
  30. public static var Comment(default,null) : XmlType;
  31. public static var DocType(default,null) : XmlType;
  32. public static var ProcessingInstruction(default,null) : XmlType;
  33. public static var Document(default,null) : XmlType;
  34. private var _nodeName : String;
  35. private var _nodeValue : String;
  36. private var _attributes : Dynamic<String>;
  37. private var _children : Array<Xml>;
  38. private var _parent : Xml;
  39. function new() : Void {
  40. }
  41. private static var _parse = cpp.Lib.load("std","parse_xml",2);
  42. @:analyzer(ignore) public static function parse( str : String ) : Xml {
  43. var x = new Xml();
  44. x._children = new Array();
  45. var parser = {
  46. cur : x,
  47. xml : function(name,att) {
  48. var x = new Xml();
  49. x._parent = untyped __this__.cur;
  50. x.nodeType = Xml.Element;
  51. x._nodeName = new String(name);
  52. x._attributes = att;
  53. x._children = new Array();
  54. untyped {
  55. var i = 0;
  56. __this__.cur.addChild(x);
  57. __this__.cur = x;
  58. }
  59. },
  60. cdata : function(text) {
  61. var x = new Xml();
  62. x._parent = untyped __this__.cur;
  63. x.nodeType = Xml.CData;
  64. x._nodeValue = new String(text);
  65. untyped __this__.cur.addChild(x);
  66. },
  67. pcdata : function(text) {
  68. var x = new Xml();
  69. x._parent = untyped __this__.cur;
  70. x.nodeType = Xml.PCData;
  71. x._nodeValue = new String(text);
  72. untyped __this__.cur.addChild(x);
  73. },
  74. comment : function(text:String) {
  75. var x = new Xml();
  76. x._parent = untyped __this__.cur;
  77. if( untyped text.cca(0) == 63 ) {
  78. x.nodeType = Xml.ProcessingInstruction;
  79. text = new String(text);
  80. text = text.substr(1, text.length - 2);
  81. } else {
  82. x.nodeType = Xml.Comment;
  83. text = new String(text);
  84. }
  85. x._nodeValue = text;
  86. untyped __this__.cur.addChild(x);
  87. },
  88. doctype : function(text) {
  89. var x = new Xml();
  90. x._parent = untyped __this__.cur;
  91. x.nodeType = Xml.DocType;
  92. x._nodeValue = (new String(text)).substr(1);
  93. var p : Xml = untyped __this__.cur;
  94. p.addChild(x);
  95. },
  96. done : function() {
  97. untyped __this__.cur = __this__.cur._parent;
  98. }
  99. };
  100. untyped _parse(str,parser);
  101. x.nodeType = Xml.Document;
  102. return x;
  103. }
  104. public static function createElement( name : String ) : Xml {
  105. var r = new Xml();
  106. r.nodeType = Xml.Element;
  107. r._nodeName = name;
  108. r._attributes = null;
  109. r._children = new Array();
  110. return r;
  111. }
  112. public static function createPCData( data : String ) : Xml {
  113. var r = new Xml();
  114. r.nodeType = Xml.PCData;
  115. r._nodeValue = data;
  116. return r;
  117. }
  118. public static function createCData( data : String ) : Xml {
  119. var r = new Xml();
  120. r.nodeType = Xml.CData;
  121. r._nodeValue = data;
  122. return r;
  123. }
  124. public static function createComment( data : String ) : Xml {
  125. var r = new Xml();
  126. r.nodeType = Xml.Comment;
  127. r._nodeValue = data;
  128. return r;
  129. }
  130. public static function createDocType( data : String ) : Xml {
  131. var r = new Xml();
  132. r.nodeType = Xml.DocType;
  133. r._nodeValue = data;
  134. return r;
  135. }
  136. public static function createProcessingInstruction( data : String ) : Xml {
  137. var r = new Xml();
  138. r.nodeType = Xml.ProcessingInstruction;
  139. r._nodeValue = data;
  140. return r;
  141. }
  142. public static function createDocument() : Xml {
  143. var r = new Xml();
  144. r.nodeType = Xml.Document;
  145. r._children = new Array();
  146. return r;
  147. }
  148. public var nodeType(default,null) : XmlType;
  149. public var nodeName(get,set) : String;
  150. public var nodeValue(get,set) : String;
  151. private function get_nodeName() : String {
  152. if( nodeType != Xml.Element )
  153. throw "bad nodeType";
  154. return _nodeName;
  155. }
  156. private function set_nodeName( n : String ) : String {
  157. if( nodeType != Xml.Element )
  158. throw "bad nodeType";
  159. return _nodeName = n;
  160. }
  161. private function get_nodeValue() : String {
  162. if( nodeType == Xml.Element || nodeType == Xml.Document )
  163. throw "bad nodeType";
  164. return _nodeValue;
  165. }
  166. private function set_nodeValue( v : String ) : String {
  167. if( nodeType == Xml.Element || nodeType == Xml.Document )
  168. throw "bad nodeType";
  169. return _nodeValue = v;
  170. }
  171. public var parent(get,null) : Xml;
  172. private function get_parent() : Xml {
  173. return _parent;
  174. }
  175. public function get( att : String ) : String {
  176. if( nodeType != Xml.Element )
  177. throw "bad nodeType";
  178. return Reflect.field( _attributes, att );
  179. }
  180. public function set( att : String, value : String ) : Void {
  181. if( nodeType != Xml.Element )
  182. throw "bad nodeType";
  183. if (_attributes==null)
  184. _attributes = {};
  185. Reflect.setField (_attributes, att, value );
  186. return null;
  187. }
  188. public function remove( att : String ) : Void{
  189. if( nodeType != Xml.Element )
  190. throw "bad nodeType";
  191. Reflect.deleteField( _attributes, att );
  192. return null;
  193. }
  194. public function exists( att : String ) : Bool {
  195. if( nodeType != Xml.Element )
  196. throw "bad nodeType";
  197. return Reflect.hasField( _attributes, att );
  198. }
  199. public function attributes() : Iterator<String> {
  200. if( nodeType != Xml.Element )
  201. throw "bad nodeType";
  202. return Reflect.fields( _attributes ).iterator();
  203. }
  204. public function iterator() : Iterator<Xml> {
  205. if( _children == null )
  206. throw "bad nodetype";
  207. return untyped _children.iterator();
  208. }
  209. @:analyzer(ignore) public function elements(): Iterator<Xml> {
  210. if( _children == null )
  211. throw "bad nodetype";
  212. var children = _children;
  213. return untyped {
  214. cur: 0,
  215. hasNext : function() {
  216. var k:Int = __this__.cur;
  217. var l = children.length;
  218. while( k < l ) {
  219. if( children[k].nodeType == Xml.Element )
  220. break;
  221. k += 1;
  222. }
  223. __this__.cur = k;
  224. return k < l;
  225. },
  226. next : function() {
  227. var k = __this__.cur;
  228. var l = children.length;
  229. while( k < l ) {
  230. var n = children[k];
  231. k += 1;
  232. if( n.nodeType == Xml.Element ) {
  233. __this__.cur = k;
  234. return n;
  235. }
  236. }
  237. return null;
  238. }
  239. }
  240. }
  241. @:analyzer(ignore) public function elementsNamed( name : String ) : Iterator<Xml> {
  242. if( _children == null )
  243. throw "bad nodetype";
  244. var children = _children;
  245. return untyped {
  246. cur: 0,
  247. hasNext : function() {
  248. var k = __this__.cur;
  249. var l = children.length;
  250. while( k < l ) {
  251. var n = children[k];
  252. if( n.nodeType == Xml.Element && n._nodeName == name )
  253. break;
  254. k++;
  255. }
  256. __this__.cur = k;
  257. return k < l;
  258. },
  259. next : function() {
  260. var k = __this__.cur;
  261. var l = children.length;
  262. while( k < l ) {
  263. var n = children[k];
  264. k++;
  265. if( n.nodeType == Xml.Element && n._nodeName == name ) {
  266. __this__.cur = k;
  267. return n;
  268. }
  269. }
  270. return null;
  271. }
  272. }
  273. }
  274. public function firstChild() : Xml {
  275. if( _children == null )
  276. throw "bad nodetype";
  277. return _children[0];
  278. }
  279. public function firstElement() : Xml {
  280. if( _children == null )
  281. throw "bad nodetype";
  282. for( cur in 0..._children.length ) {
  283. var n:Xml = _children[cur];
  284. if( n.nodeType == Xml.Element )
  285. return n;
  286. }
  287. return null;
  288. }
  289. public function addChild( x : Xml ) : Void {
  290. if( _children == null )
  291. throw "bad nodetype";
  292. if( x._parent != null ) x._parent._children.remove(x);
  293. x._parent = this;
  294. _children.push( x );
  295. return null;
  296. }
  297. public function removeChild( x : Xml ) : Bool {
  298. if( _children == null )
  299. throw "bad nodetype";
  300. var b = _children.remove( x );
  301. if( b ) x._parent = null;
  302. return b;
  303. }
  304. public function insertChild( x : Xml, pos : Int ) : Void {
  305. if( _children == null )
  306. throw "bad nodetype";
  307. if( x._parent != null ) x._parent._children.remove(x);
  308. x._parent = this;
  309. _children.insert( pos, x );
  310. return null;
  311. }
  312. public function toString() : String {
  313. var s = new StringBuf();
  314. toStringRec(s);
  315. return s.toString();
  316. }
  317. private function toStringRec(s: StringBuf) : Void {
  318. switch( nodeType ) {
  319. case Xml.Document:
  320. for( x in _children )
  321. x.toStringRec(s);
  322. case Xml.Element:
  323. s.addChar("<".code);
  324. s.add(_nodeName);
  325. for( k in Reflect.fields(_attributes) ) {
  326. s.addChar(" ".code);
  327. s.add(k);
  328. s.addChar("=".code);
  329. s.addChar("\"".code);
  330. s.add(Reflect.field(_attributes,k));
  331. s.addChar("\"".code);
  332. }
  333. if( _children.length == 0 ) {
  334. s.addChar("/".code);
  335. s.addChar(">".code);
  336. return;
  337. }
  338. s.addChar(">".code);
  339. for( x in _children )
  340. x.toStringRec(s);
  341. s.addChar("<".code);
  342. s.addChar("/".code);
  343. s.add(_nodeName);
  344. s.addChar(">".code);
  345. case Xml.PCData:
  346. s.add(StringTools.htmlEscape(_nodeValue));
  347. case Xml.CData:
  348. s.add("<![CDATA[");
  349. s.add(_nodeValue);
  350. s.add("]]>");
  351. case Xml.Comment:
  352. s.add("<!--");
  353. s.add(_nodeValue);
  354. s.add("-->");
  355. case Xml.DocType:
  356. s.add("<!DOCTYPE ");
  357. s.add(_nodeValue);
  358. s.add(">");
  359. case Xml.ProcessingInstruction:
  360. s.add("<?");
  361. s.add(_nodeValue);
  362. s.add("?>");
  363. }
  364. }
  365. static function __init__() : Void untyped {
  366. PCData = Type.createEnum(XmlType,"__");
  367. Element = Type.createEnum(XmlType,"__");
  368. CData = Type.createEnum(XmlType,"__");
  369. Comment = Type.createEnum(XmlType,"__");
  370. DocType = Type.createEnum(XmlType,"__");
  371. ProcessingInstruction = Type.createEnum(XmlType,"__");
  372. Document = Type.createEnum(XmlType,"__");
  373. __global__.__hxcpp_enum_force(PCData , "pcdata", 0);
  374. __global__.__hxcpp_enum_force(Element , "element", 1);
  375. __global__.__hxcpp_enum_force(CData , "cdata", 2);
  376. __global__.__hxcpp_enum_force(Comment , "comment", 3);
  377. __global__.__hxcpp_enum_force(DocType , "doctype", 4);
  378. __global__.__hxcpp_enum_force(ProcessingInstruction , "processingInstruction", 5);
  379. __global__.__hxcpp_enum_force(Document , "document", 6);
  380. }
  381. }