NativeXml.hx 10 KB

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