Xml.hx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. untyped this.cur.addChild(x);
  95. },
  96. done : function() {
  97. untyped this.cur = this.cur._parent;
  98. }
  99. };
  100. untyped _parse(str,parser);
  101. x.nodeType = Xml.Document;
  102. return x;
  103. }
  104. public static function createElement( name : String ) : Xml {
  105. var r = new Xml();
  106. r.nodeType = Xml.Element;
  107. r._nodeName = name;
  108. r._attributes = null;
  109. r._children = new Array();
  110. return r;
  111. }
  112. public static function createPCData( data : String ) : Xml {
  113. var r = new Xml();
  114. r.nodeType = Xml.PCData;
  115. r._nodeValue = data;
  116. return r;
  117. }
  118. public static function createCData( data : String ) : Xml {
  119. var r = new Xml();
  120. r.nodeType = Xml.CData;
  121. r._nodeValue = data;
  122. return r;
  123. }
  124. public static function createComment( data : String ) : Xml {
  125. var r = new Xml();
  126. r.nodeType = Xml.Comment;
  127. r._nodeValue = data;
  128. return r;
  129. }
  130. public static function createDocType( data : String ) : Xml {
  131. var r = new Xml();
  132. r.nodeType = Xml.DocType;
  133. r._nodeValue = data;
  134. return r;
  135. }
  136. public static function createProlog( data : String ) : Xml {
  137. var r = new Xml();
  138. r.nodeType = Xml.Prolog;
  139. r._nodeValue = data;
  140. return r;
  141. }
  142. public static function createDocument() : Xml {
  143. var r = new Xml();
  144. r.nodeType = Xml.Document;
  145. r._children = new Array();
  146. return r;
  147. }
  148. public var nodeType(default,null) : XmlType;
  149. public var nodeName(getNodeName,setNodeName) : String;
  150. public var nodeValue(getNodeValue,setNodeValue) : String;
  151. private function getNodeName() : String {
  152. if( nodeType != Xml.Element )
  153. throw "bad nodeType";
  154. return _nodeName;
  155. }
  156. private function setNodeName( n : String ) : String {
  157. if( nodeType != Xml.Element )
  158. throw "bad nodeType";
  159. return _nodeName = n;
  160. }
  161. private function getNodeValue() : String {
  162. if( nodeType == Xml.Element || nodeType == Xml.Document )
  163. throw "bad nodeType";
  164. return _nodeValue;
  165. }
  166. private function setNodeValue( v : String ) : String {
  167. if( nodeType == Xml.Element || nodeType == Xml.Document )
  168. throw "bad nodeType";
  169. return _nodeValue = v;
  170. }
  171. public var parent(getParent,null) : Xml;
  172. private function getParent() : Xml {
  173. return _parent;
  174. }
  175. public function get( att : String ) : String {
  176. if( nodeType != Xml.Element )
  177. throw "bad nodeType";
  178. return Reflect.field( _attributes, att );
  179. }
  180. public function set( att : String, value : String ) : Void {
  181. if( nodeType != Xml.Element )
  182. throw "bad nodeType";
  183. if (_attributes==null)
  184. _attributes = {};
  185. Reflect.setField (_attributes, att, value );
  186. return null;
  187. }
  188. public function remove( att : String ) : Void{
  189. if( nodeType != Xml.Element )
  190. throw "bad nodeType";
  191. Reflect.deleteField( _attributes, att );
  192. return null;
  193. }
  194. public function exists( att : String ) : Bool {
  195. if( nodeType != Xml.Element )
  196. throw "bad nodeType";
  197. return Reflect.hasField( _attributes, att );
  198. }
  199. public function attributes() : Iterator<String> {
  200. if( nodeType != Xml.Element )
  201. throw "bad nodeType";
  202. return Reflect.fields( _attributes ).iterator();
  203. }
  204. public function iterator() : Iterator<Xml> {
  205. if( _children == null )
  206. throw "bad nodetype";
  207. return untyped _children.iterator();
  208. }
  209. public function elements(): Iterator<Xml> {
  210. if( _children == null )
  211. throw "bad nodetype";
  212. var children = _children;
  213. return untyped {
  214. cur: 0,
  215. hasNext : function() {
  216. var k:Int = this.cur;
  217. var l = children.length;
  218. while( k < l ) {
  219. if( children[k].nodeType == Xml.Element )
  220. break;
  221. k += 1;
  222. }
  223. this.cur = k;
  224. return k < l;
  225. },
  226. next : function() {
  227. var k = this.cur;
  228. var l = children.length;
  229. while( k < l ) {
  230. var n = children[k];
  231. k += 1;
  232. if( n.nodeType == Xml.Element ) {
  233. this.cur = k;
  234. return n;
  235. }
  236. }
  237. return null;
  238. }
  239. }
  240. }
  241. public function elementsNamed( name : String ) : Iterator<Xml> {
  242. if( _children == null )
  243. throw "bad nodetype";
  244. var children = _children;
  245. return untyped {
  246. cur: 0,
  247. hasNext : function() {
  248. var k = this.cur;
  249. var l = children.length;
  250. while( k < l ) {
  251. var n = children[k];
  252. if( n.nodeType == Xml.Element && n._nodeName == name )
  253. break;
  254. k++;
  255. }
  256. this.cur = k;
  257. return k < l;
  258. },
  259. next : function() {
  260. var k = this.cur;
  261. var l = children.length;
  262. while( k < l ) {
  263. var n = children[k];
  264. k++;
  265. if( n.nodeType == Xml.Element && n._nodeName == name ) {
  266. this.cur = k;
  267. return n;
  268. }
  269. }
  270. return null;
  271. }
  272. }
  273. }
  274. public function firstChild() : Xml {
  275. if( _children == null )
  276. throw "bad nodetype";
  277. return _children[0];
  278. }
  279. public function firstElement() : Xml {
  280. if( _children == null )
  281. throw "bad nodetype";
  282. for( cur in 0..._children.length ) {
  283. var n:Xml = _children[cur];
  284. if( n.nodeType == Xml.Element )
  285. return n;
  286. }
  287. return null;
  288. }
  289. public function addChild( x : Xml ) : Void {
  290. if( _children == null )
  291. throw "bad nodetype";
  292. if( x._parent != null ) x._parent._children.remove(x);
  293. x._parent = this;
  294. _children.push( x );
  295. return null;
  296. }
  297. public function removeChild( x : Xml ) : Bool {
  298. if( _children == null )
  299. throw "bad nodetype";
  300. var b = _children.remove( x );
  301. if( b ) x._parent = null;
  302. return b;
  303. }
  304. public function insertChild( x : Xml, pos : Int ) : Void {
  305. if( _children == null )
  306. throw "bad nodetype";
  307. if( x._parent != null ) x._parent._children.remove(x);
  308. x._parent = this;
  309. _children.insert( pos, x );
  310. return null;
  311. }
  312. public function toString() : String {
  313. var s = new StringBuf();
  314. toStringRec(s);
  315. return s.toString();
  316. }
  317. private function toStringRec(s: StringBuf) : Void {
  318. switch( nodeType ) {
  319. case Xml.Document:
  320. for( x in _children )
  321. x.toStringRec(s);
  322. case Xml.Element:
  323. s.addChar("<".code);
  324. s.add(_nodeName);
  325. for( k in Reflect.fields(_attributes) ) {
  326. s.addChar(" ".code);
  327. s.add(k);
  328. s.addChar("=".code);
  329. s.addChar("\"".code);
  330. s.add(Reflect.field(_attributes,k));
  331. s.addChar("\"".code);
  332. }
  333. if( _children.length == 0 ) {
  334. s.addChar("/".code);
  335. s.addChar(">".code);
  336. return;
  337. }
  338. s.addChar(">".code);
  339. for( x in _children )
  340. x.toStringRec(s);
  341. s.addChar("<".code);
  342. s.addChar("/".code);
  343. s.add(_nodeName);
  344. s.addChar(">".code);
  345. case Xml.PCData:
  346. s.add(_nodeValue);
  347. case Xml.CData:
  348. s.add("<![CDATA[");
  349. s.add(_nodeValue);
  350. s.add("]]>");
  351. case Xml.Comment:
  352. s.add("<!--");
  353. s.add(_nodeValue);
  354. s.add("-->");
  355. case Xml.DocType:
  356. s.add("<!DOCTYPE ");
  357. s.add(_nodeValue);
  358. s.add(">");
  359. case Xml.Prolog:
  360. s.add("<?");
  361. s.add(_nodeValue);
  362. s.add("?>");
  363. }
  364. }
  365. static function __init__() : Void untyped {
  366. PCData = Type.createEnum(XmlType,"__");
  367. Element = Type.createEnum(XmlType,"__");
  368. CData = Type.createEnum(XmlType,"__");
  369. Comment = Type.createEnum(XmlType,"__");
  370. DocType = Type.createEnum(XmlType,"__");
  371. Prolog = Type.createEnum(XmlType,"__");
  372. Document = Type.createEnum(XmlType,"__");
  373. __global__.__hxcpp_enum_force(PCData , "pcdata", 0);
  374. __global__.__hxcpp_enum_force(Element , "element", 1);
  375. __global__.__hxcpp_enum_force(CData , "cdata", 2);
  376. __global__.__hxcpp_enum_force(Comment , "comment", 3);
  377. __global__.__hxcpp_enum_force(DocType , "doctype", 4);
  378. __global__.__hxcpp_enum_force(Prolog , "prolog", 5);
  379. __global__.__hxcpp_enum_force(Document , "document", 6);
  380. }
  381. }