NativeXml.hx 12 KB

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