Xml.hx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (C)2005-2012 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. @:native("_Xml.RealXmlType")
  23. extern enum XmlType {
  24. }
  25. private enum RealXmlType {
  26. Element;
  27. PCData;
  28. CData;
  29. Comment;
  30. DocType;
  31. ProcessingInstruction;
  32. Document;
  33. }
  34. @:coreApi class Xml {
  35. public static var Element(default,null) : XmlType;
  36. public static var PCData(default,null) : XmlType;
  37. public static var CData(default,null) : XmlType;
  38. public static var Comment(default,null) : XmlType;
  39. public static var DocType(default,null) : XmlType;
  40. public static var ProcessingInstruction(default,null) : XmlType;
  41. public static var Document(default,null) : XmlType;
  42. public var nodeType(default,null) : XmlType;
  43. public var nodeName(get,set) : String;
  44. public var nodeValue(get,set) : String;
  45. public var parent(get,null) : Xml;
  46. var _nodeName : String;
  47. var _nodeValue : String;
  48. var _attributes : haxe.ds.StringMap<String>;
  49. var _children : Array<Xml>;
  50. var _parent : Xml;
  51. public static function parse( str : String ) : Xml {
  52. return haxe.xml.Parser.parse(str);
  53. }
  54. private function new() : Void {
  55. }
  56. public static function createElement( name : String ) : Xml {
  57. var r = new Xml();
  58. r.nodeType = Xml.Element;
  59. r._children = new Array();
  60. r._attributes = new haxe.ds.StringMap();
  61. r.set_nodeName( name );
  62. return r;
  63. }
  64. public static function createPCData( data : String ) : Xml {
  65. var r = new Xml();
  66. r.nodeType = Xml.PCData;
  67. r.set_nodeValue( data );
  68. return r;
  69. }
  70. public static function createCData( data : String ) : Xml {
  71. var r = new Xml();
  72. r.nodeType = Xml.CData;
  73. r.set_nodeValue( data );
  74. return r;
  75. }
  76. public static function createComment( data : String ) : Xml {
  77. var r = new Xml();
  78. r.nodeType = Xml.Comment;
  79. r.set_nodeValue( data );
  80. return r;
  81. }
  82. public static function createDocType( data : String ) : Xml {
  83. var r = new Xml();
  84. r.nodeType = Xml.DocType;
  85. r.set_nodeValue( data );
  86. return r;
  87. }
  88. public static function createProcessingInstruction( data : String ) : Xml {
  89. var r = new Xml();
  90. r.nodeType = Xml.ProcessingInstruction;
  91. r.set_nodeValue( data );
  92. return r;
  93. }
  94. public static function createDocument() : Xml {
  95. var r = new Xml();
  96. r.nodeType = Xml.Document;
  97. r._children = new Array();
  98. return r;
  99. }
  100. private function get_nodeName() : String {
  101. if( nodeType != Xml.Element )
  102. throw "bad nodeType";
  103. return _nodeName;
  104. }
  105. private function set_nodeName( n : String ) : String {
  106. if( nodeType != Xml.Element )
  107. throw "bad nodeType";
  108. return _nodeName = n;
  109. }
  110. private function get_nodeValue() : String {
  111. if( nodeType == Xml.Element || nodeType == Xml.Document )
  112. throw "bad nodeType";
  113. return _nodeValue;
  114. }
  115. private function set_nodeValue( v : String ) : String {
  116. if( nodeType == Xml.Element || nodeType == Xml.Document )
  117. throw "bad nodeType";
  118. return _nodeValue = v;
  119. }
  120. private function get_parent() : Xml {
  121. return _parent;
  122. }
  123. public function get( att : String ) : String {
  124. if( nodeType != Xml.Element )
  125. throw "bad nodeType";
  126. return _attributes.get( att );
  127. }
  128. public function set( att : String, value : String ) : Void {
  129. if( nodeType != Xml.Element )
  130. throw "bad nodeType";
  131. _attributes.set( att, value );
  132. }
  133. public function remove( att : String ) : Void{
  134. if( nodeType != Xml.Element )
  135. throw "bad nodeType";
  136. _attributes.remove( att );
  137. }
  138. public function exists( att : String ) : Bool {
  139. if( nodeType != Xml.Element )
  140. throw "bad nodeType";
  141. return _attributes.exists( att );
  142. }
  143. public function attributes() : Iterator<String> {
  144. if( nodeType != Xml.Element )
  145. throw "bad nodeType";
  146. return _attributes.keys();
  147. }
  148. public function iterator() : Iterator<Xml> {
  149. if ( _children == null ) throw "bad nodetype";
  150. var cur = 0;
  151. var x = this._children;
  152. return {
  153. hasNext : function(){
  154. return cur < x.length;
  155. },
  156. next : function(){
  157. return x[cur++];
  158. }
  159. }
  160. }
  161. public function elements() : Iterator<Xml> {
  162. if ( _children == null ) throw "bad nodetype";
  163. var cur = 0;
  164. var x = _children;
  165. return untyped {
  166. hasNext : function() {
  167. var k = cur;
  168. var l = x.length;
  169. while( k < l ) {
  170. if( x[k].nodeType == Xml.Element )
  171. break;
  172. k += 1;
  173. }
  174. cur = k;
  175. return k < l;
  176. },
  177. next : function() {
  178. var k = cur;
  179. var l = x.length;
  180. while( k < l ) {
  181. var n = x[k];
  182. k += 1;
  183. if( n.nodeType == Xml.Element ) {
  184. cur = k;
  185. return n;
  186. }
  187. }
  188. return null;
  189. }
  190. }
  191. }
  192. public function elementsNamed( name : String ) : Iterator<Xml> {
  193. if ( _children == null ) throw "bad nodetype";
  194. var x = _children;
  195. var cur = 0;
  196. return untyped {
  197. cur: 0,
  198. x: this._children,
  199. hasNext : function() {
  200. var k = cur;
  201. var l = x.length;
  202. while( k < l ) {
  203. var n = x[k];
  204. if( n.nodeType == Xml.Element && n._nodeName == name )
  205. break;
  206. k++;
  207. }
  208. cur = k;
  209. return k < l;
  210. },
  211. next : function() {
  212. var k = cur;
  213. var l = x.length;
  214. while( k < l ) {
  215. var n = x[k];
  216. k++;
  217. if( n.nodeType == Xml.Element && n._nodeName == name ) {
  218. cur = k;
  219. return n;
  220. }
  221. }
  222. return null;
  223. }
  224. }
  225. }
  226. public function firstChild() : Xml {
  227. if( _children == null ) throw "bad nodetype";
  228. return _children[0];
  229. }
  230. public function firstElement() : Xml {
  231. if( _children == null ) throw "bad nodetype";
  232. var cur = 0;
  233. var l = _children.length;
  234. while( cur < l ) {
  235. var n = _children[cur];
  236. if( n.nodeType == Xml.Element )
  237. return n;
  238. cur++;
  239. }
  240. return null;
  241. }
  242. public function addChild( x : Xml ) : Void {
  243. if( _children == null ) throw "bad nodetype";
  244. if( x._parent != null ) x._parent._children.remove(x);
  245. x._parent = this;
  246. _children.push( x );
  247. }
  248. public function removeChild( x : Xml ) : Bool {
  249. if( _children == null ) throw "bad nodetype";
  250. var b = _children.remove( x );
  251. if( b )
  252. x._parent = null;
  253. return b;
  254. }
  255. public function insertChild( x : Xml, pos : Int ) : Void {
  256. if( _children == null ) throw "bad nodetype";
  257. if( x._parent != null ) x._parent._children.remove(x);
  258. x._parent = this;
  259. _children.insert( pos, x );
  260. }
  261. public function toString() : String {
  262. if( nodeType == Xml.PCData )
  263. return StringTools.htmlEscape(_nodeValue);
  264. if( nodeType == Xml.CData )
  265. return "<![CDATA["+_nodeValue+"]]>";
  266. if( nodeType == Xml.Comment )
  267. return "<!--"+_nodeValue+"-->";
  268. if( nodeType == Xml.DocType )
  269. return "<!DOCTYPE "+_nodeValue+">";
  270. if( nodeType == Xml.ProcessingInstruction )
  271. return "<?"+_nodeValue+"?>";
  272. var s = new StringBuf();
  273. if( nodeType == Xml.Element ) {
  274. s.add("<");
  275. s.add(_nodeName);
  276. for( k in _attributes.keys() ){
  277. s.add(" ");
  278. s.add(k);
  279. s.add("=\"");
  280. s.add(_attributes.get(k));
  281. s.add("\"");
  282. }
  283. if( _children.length == 0 ) {
  284. s.add("/>");
  285. return s.toString();
  286. }
  287. s.add(">");
  288. }
  289. for( x in iterator() )
  290. s.add(x.toString());
  291. if( nodeType == Xml.Element ) {
  292. s.add("</");
  293. s.add(_nodeName);
  294. s.add(">");
  295. }
  296. return s.toString();
  297. }
  298. static function __init__() : Void untyped {
  299. Xml.Element = cast RealXmlType.Element;
  300. Xml.PCData = cast RealXmlType.PCData;
  301. Xml.CData = cast RealXmlType.CData;
  302. Xml.Comment = cast RealXmlType.Comment;
  303. Xml.DocType = cast RealXmlType.DocType;
  304. Xml.ProcessingInstruction = cast RealXmlType.ProcessingInstruction;
  305. Xml.Document = cast RealXmlType.Document;
  306. }
  307. }