NativeXml.hx 10 KB

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