Xml.hx 8.0 KB

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