NativeXml.hx 10 KB

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