Xml.hx 10 KB

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