Xml.hx 8.3 KB

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