Xml.hx 10 KB

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