Xml.hx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. enum XmlType {
  26. }
  27. @:core_api class Xml {
  28. public static var Element(default,null) : XmlType;
  29. public static var PCData(default,null) : XmlType;
  30. public static var CData(default,null) : XmlType;
  31. public static var Comment(default,null) : XmlType;
  32. public static var DocType(default,null) : XmlType;
  33. public static var Prolog(default,null) : XmlType;
  34. public static var Document(default,null) : XmlType;
  35. private var _nodeName : String;
  36. private var _nodeValue : String;
  37. private var _attributes : Dynamic<String>;
  38. private var _children : Array<Dynamic>;
  39. private var _parent : Xml;
  40. function new() : Void {
  41. }
  42. private static var _parse = cpp.Lib.load("std","parse_xml",2);
  43. public static function parse( str : String ) : Xml {
  44. var x = new Xml();
  45. x._children = new Array();
  46. var parser = {
  47. cur : x,
  48. xml : function(name,att) {
  49. var x : Dynamic = new Xml();
  50. x._parent = untyped __this__.cur;
  51. x.nodeType = Xml.Element;
  52. x._nodeName = new String(name);
  53. x._attributes = att;
  54. x._children = new Array();
  55. untyped {
  56. var i = 0;
  57. __this__.cur.addChild(x);
  58. __this__.cur = x;
  59. }
  60. },
  61. cdata : function(text) {
  62. var x = new Xml();
  63. x._parent = untyped __this__.cur;
  64. x.nodeType = Xml.CData;
  65. x._nodeValue = new String(text);
  66. untyped __this__.cur.addChild(x);
  67. },
  68. pcdata : function(text) {
  69. var x = new Xml();
  70. x._parent = untyped __this__.cur;
  71. x.nodeType = Xml.PCData;
  72. x._nodeValue = new String(text);
  73. untyped __this__.cur.addChild(x);
  74. },
  75. comment : function(text:String) {
  76. var x = new Xml();
  77. x._parent = untyped __this__.cur;
  78. if( untyped text.cca(0) == 63 ) {
  79. x.nodeType = Xml.Prolog;
  80. text = new String(text);
  81. text = text.substr(1, text.length - 2);
  82. } else {
  83. x.nodeType = Xml.Comment;
  84. text = new String(text);
  85. }
  86. x._nodeValue = text;
  87. untyped __this__.cur.addChild(x);
  88. },
  89. doctype : function(text) {
  90. var x = new Xml();
  91. x._parent = untyped __this__.cur;
  92. x.nodeType = Xml.DocType;
  93. x._nodeValue = (new String(text)).substr(1);
  94. var p : Xml = untyped __this__.cur;
  95. p.addChild(x);
  96. },
  97. done : function() {
  98. untyped __this__.cur = __this__.cur._parent;
  99. }
  100. };
  101. untyped _parse(str,parser);
  102. x.nodeType = Xml.Document;
  103. return x;
  104. }
  105. public static function createElement( name : String ) : Xml {
  106. var r = new Xml();
  107. r.nodeType = Xml.Element;
  108. r._nodeName = name;
  109. r._attributes = null;
  110. r._children = new Array();
  111. return r;
  112. }
  113. public static function createPCData( data : String ) : Xml {
  114. var r = new Xml();
  115. r.nodeType = Xml.PCData;
  116. r._nodeValue = data;
  117. return r;
  118. }
  119. public static function createCData( data : String ) : Xml {
  120. var r = new Xml();
  121. r.nodeType = Xml.CData;
  122. r._nodeValue = data;
  123. return r;
  124. }
  125. public static function createComment( data : String ) : Xml {
  126. var r = new Xml();
  127. r.nodeType = Xml.Comment;
  128. r._nodeValue = data;
  129. return r;
  130. }
  131. public static function createDocType( data : String ) : Xml {
  132. var r = new Xml();
  133. r.nodeType = Xml.DocType;
  134. r._nodeValue = data;
  135. return r;
  136. }
  137. public static function createProlog( data : String ) : Xml {
  138. var r = new Xml();
  139. r.nodeType = Xml.Prolog;
  140. r._nodeValue = data;
  141. return r;
  142. }
  143. public static function createDocument() : Xml {
  144. var r = new Xml();
  145. r.nodeType = Xml.Document;
  146. r._children = new Array();
  147. return r;
  148. }
  149. public var nodeType(default,null) : XmlType;
  150. public var nodeName(getNodeName,setNodeName) : String;
  151. public var nodeValue(getNodeValue,setNodeValue) : String;
  152. private function getNodeName() : String {
  153. if( nodeType != Xml.Element )
  154. throw "bad nodeType";
  155. return _nodeName;
  156. }
  157. private function setNodeName( n : String ) : String {
  158. if( nodeType != Xml.Element )
  159. throw "bad nodeType";
  160. return _nodeName = n;
  161. }
  162. private function getNodeValue() : String {
  163. if( nodeType == Xml.Element || nodeType == Xml.Document )
  164. throw "bad nodeType";
  165. return _nodeValue;
  166. }
  167. private function setNodeValue( v : String ) : String {
  168. if( nodeType == Xml.Element || nodeType == Xml.Document )
  169. throw "bad nodeType";
  170. return _nodeValue = v;
  171. }
  172. public var parent(getParent,null) : Xml;
  173. private function getParent() : Xml {
  174. return _parent;
  175. }
  176. public function get( att : String ) : String {
  177. if( nodeType != Xml.Element )
  178. throw "bad nodeType";
  179. return Reflect.field( _attributes, att );
  180. }
  181. public function set( att : String, value : String ) : Void {
  182. if( nodeType != Xml.Element )
  183. throw "bad nodeType";
  184. if (_attributes==null)
  185. _attributes = {};
  186. Reflect.setField (_attributes, att, value );
  187. return null;
  188. }
  189. public function remove( att : String ) : Void{
  190. if( nodeType != Xml.Element )
  191. throw "bad nodeType";
  192. Reflect.deleteField( _attributes, att );
  193. return null;
  194. }
  195. public function exists( att : String ) : Bool {
  196. if( nodeType != Xml.Element )
  197. throw "bad nodeType";
  198. return Reflect.hasField( _attributes, att );
  199. }
  200. public function attributes() : Iterator<String> {
  201. if( nodeType != Xml.Element )
  202. throw "bad nodeType";
  203. return Reflect.fields( _attributes ).iterator();
  204. }
  205. public function iterator() : Iterator<Xml> {
  206. if( _children == null )
  207. throw "bad nodetype";
  208. return untyped _children.iterator();
  209. }
  210. public function elements(): Iterator<Xml> {
  211. if( _children == null )
  212. throw "bad nodetype";
  213. var children = _children;
  214. return untyped {
  215. cur: 0,
  216. hasNext : function() {
  217. var k:Int = __this__.cur;
  218. var l = children.length;
  219. while( k < l ) {
  220. if( children[k].nodeType == Xml.Element )
  221. break;
  222. k += 1;
  223. }
  224. __this__.cur = k;
  225. return k < l;
  226. },
  227. next : function() {
  228. var k = __this__.cur;
  229. var l = children.length;
  230. while( k < l ) {
  231. var n = children[k];
  232. k += 1;
  233. if( n.nodeType == Xml.Element ) {
  234. __this__.cur = k;
  235. return n;
  236. }
  237. }
  238. return null;
  239. }
  240. }
  241. }
  242. public function elementsNamed( name : String ) : Iterator<Xml> {
  243. if( _children == null )
  244. throw "bad nodetype";
  245. var children = _children;
  246. return untyped {
  247. cur: 0,
  248. hasNext : function() {
  249. var k = __this__.cur;
  250. var l = children.length;
  251. while( k < l ) {
  252. var n = children[k];
  253. if( n.nodeType == Xml.Element && n._nodeName == name )
  254. break;
  255. k++;
  256. }
  257. __this__.cur = k;
  258. return k < l;
  259. },
  260. next : function() {
  261. var k = __this__.cur;
  262. var l = children.length;
  263. while( k < l ) {
  264. var n = children[k];
  265. k++;
  266. if( n.nodeType == Xml.Element && n._nodeName == name ) {
  267. __this__.cur = k;
  268. return n;
  269. }
  270. }
  271. return null;
  272. }
  273. }
  274. }
  275. public function firstChild() : Xml {
  276. if( _children == null )
  277. throw "bad nodetype";
  278. return _children[0];
  279. }
  280. public function firstElement() : Xml {
  281. if( _children == null )
  282. throw "bad nodetype";
  283. for( cur in 0..._children.length ) {
  284. var n:Xml = _children[cur];
  285. if( n.nodeType == Xml.Element )
  286. return n;
  287. }
  288. return null;
  289. }
  290. public function addChild( x : Xml ) : Void {
  291. if( _children == null )
  292. throw "bad nodetype";
  293. if( x._parent != null ) x._parent._children.remove(x);
  294. x._parent = this;
  295. _children.push( x );
  296. return null;
  297. }
  298. public function removeChild( x : Xml ) : Bool {
  299. if( _children == null )
  300. throw "bad nodetype";
  301. var b = _children.remove( x );
  302. if( b ) x._parent = null;
  303. return b;
  304. }
  305. public function insertChild( x : Xml, pos : Int ) : Void {
  306. if( _children == null )
  307. throw "bad nodetype";
  308. if( x._parent != null ) x._parent._children.remove(x);
  309. x._parent = this;
  310. _children.insert( pos, x );
  311. return null;
  312. }
  313. public function toString() : String {
  314. var s = new StringBuf();
  315. toStringRec(s);
  316. return s.toString();
  317. }
  318. private function toStringRec(s: StringBuf) : Void {
  319. switch( nodeType ) {
  320. case Xml.Document:
  321. for( x in _children )
  322. x.toStringRec(s);
  323. case Xml.Element:
  324. s.addChar("<".code);
  325. s.add(_nodeName);
  326. for( k in Reflect.fields(_attributes) ) {
  327. s.addChar(" ".code);
  328. s.add(k);
  329. s.addChar("=".code);
  330. s.addChar("\"".code);
  331. s.add(Reflect.field(_attributes,k));
  332. s.addChar("\"".code);
  333. }
  334. if( _children.length == 0 ) {
  335. s.addChar("/".code);
  336. s.addChar(">".code);
  337. return;
  338. }
  339. s.addChar(">".code);
  340. for( x in _children )
  341. x.toStringRec(s);
  342. s.addChar("<".code);
  343. s.addChar("/".code);
  344. s.add(_nodeName);
  345. s.addChar(">".code);
  346. case Xml.PCData:
  347. s.add(_nodeValue);
  348. case Xml.CData:
  349. s.add("<![CDATA[");
  350. s.add(_nodeValue);
  351. s.add("]]>");
  352. case Xml.Comment:
  353. s.add("<!--");
  354. s.add(_nodeValue);
  355. s.add("-->");
  356. case Xml.DocType:
  357. s.add("<!DOCTYPE ");
  358. s.add(_nodeValue);
  359. s.add(">");
  360. case Xml.Prolog:
  361. s.add("<?");
  362. s.add(_nodeValue);
  363. s.add("?>");
  364. }
  365. }
  366. static function __init__() : Void untyped {
  367. PCData = Type.createEnum(XmlType,"__");
  368. Element = Type.createEnum(XmlType,"__");
  369. CData = Type.createEnum(XmlType,"__");
  370. Comment = Type.createEnum(XmlType,"__");
  371. DocType = Type.createEnum(XmlType,"__");
  372. Prolog = Type.createEnum(XmlType,"__");
  373. Document = Type.createEnum(XmlType,"__");
  374. __global__.__hxcpp_enum_force(PCData , "pcdata", 0);
  375. __global__.__hxcpp_enum_force(Element , "element", 1);
  376. __global__.__hxcpp_enum_force(CData , "cdata", 2);
  377. __global__.__hxcpp_enum_force(Comment , "comment", 3);
  378. __global__.__hxcpp_enum_force(DocType , "doctype", 4);
  379. __global__.__hxcpp_enum_force(Prolog , "prolog", 5);
  380. __global__.__hxcpp_enum_force(Document , "document", 6);
  381. }
  382. }