Xml.hx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. static var enode = ~/^<([a-zA-Z0-9:._-]+)/;
  36. static var ecdata = ~/^<!\[CDATA\[/i;
  37. static var edoctype = ~/^<!DOCTYPE /i;
  38. static var eend = ~/^<\/([a-zA-Z0-9:._-]+)>/;
  39. static var epcdata = ~/^[^<]+/;
  40. static var ecomment = ~/^<!--/;
  41. static var eprolog = ~/^<\?[^\?]+\?>/;
  42. static var eattribute = ~/^\s*([a-zA-Z0-9:_-]+)\s*=\s*(["'])([^\2]*?)\2/; //"
  43. static var eclose = ~/^[ \r\n\t]*(>|(\/>))/;
  44. static var ecdata_end = ~/\]\]>/;
  45. static var edoctype_elt = ~/[\[|\]>]/;
  46. static var ecomment_end = ~/-->/;
  47. public var nodeType(default,null) : XmlType;
  48. public var nodeName(getNodeName,setNodeName) : String;
  49. public var nodeValue(getNodeValue,setNodeValue) : String;
  50. public var parent(getParent,null) : Xml;
  51. var _nodeName : String;
  52. var _nodeValue : String;
  53. var _attributes : Hash<String>;
  54. var _children : Array<Xml>;
  55. var _parent : Xml;
  56. public static function parse( str : String ) : Xml {
  57. var rules = [enode,epcdata,eend,ecdata,edoctype,ecomment,eprolog];
  58. var nrules = rules.length;
  59. var current = Xml.createDocument();
  60. var stack = new List();
  61. while( str.length > 0 ) {
  62. var i = 0;
  63. while( i < nrules ) {
  64. var r = rules[i];
  65. if( r.match(str) ) {
  66. switch( i ) {
  67. case 0: // Node
  68. var x = Xml.createElement(r.matched(1));
  69. current.addChild(x);
  70. str = r.matchedRight();
  71. while( eattribute.match(str) ) {
  72. x.set(eattribute.matched(1),eattribute.matched(3));
  73. str = eattribute.matchedRight();
  74. }
  75. if( !eclose.match(str) ) {
  76. i = nrules;
  77. break;
  78. }
  79. if( eclose.matched(1) == ">" ) {
  80. stack.push(current);
  81. current = x;
  82. }
  83. str = eclose.matchedRight();
  84. case 1: // PCData
  85. var x = Xml.createPCData(r.matched(0));
  86. current.addChild(x);
  87. str = r.matchedRight();
  88. case 2: // End Node
  89. untyped if( current._children != null && current._children.length == 0 ) {
  90. var e = Xml.createPCData("");
  91. current.addChild(e);
  92. }
  93. untyped if( r.matched(1) != current._nodeName || stack.isEmpty() ) {
  94. i = nrules;
  95. break;
  96. }
  97. current = stack.pop();
  98. str = r.matchedRight();
  99. case 3: // CData
  100. str = r.matchedRight();
  101. if( !ecdata_end.match(str) )
  102. throw "End of CDATA section not found";
  103. var x = Xml.createCData(ecdata_end.matchedLeft());
  104. current.addChild(x);
  105. str = ecdata_end.matchedRight();
  106. case 4: // DocType
  107. var pos = 0;
  108. var count = 0;
  109. var old = str;
  110. while( true ) {
  111. if( !edoctype_elt.match(str) )
  112. throw "End of DOCTYPE section not found";
  113. var p = edoctype_elt.matchedPos();
  114. pos += p.pos + p.len;
  115. str = edoctype_elt.matchedRight();
  116. switch( edoctype_elt.matched(0) ) {
  117. case "[": count++;
  118. case "]": count--; if( count < 0 ) throw "Invalid ] found in DOCTYPE declaration";
  119. default:
  120. if( count == 0 )
  121. break;
  122. }
  123. }
  124. var x = Xml.createDocType(old.substr(10,pos-11));
  125. current.addChild(x);
  126. case 5: // Comment
  127. if( !ecomment_end.match(str) )
  128. throw "Unclosed Comment";
  129. var p = ecomment_end.matchedPos();
  130. var x = Xml.createComment(str.substr(4,p.pos+p.len-7));
  131. current.addChild(x);
  132. str = ecomment_end.matchedRight();
  133. case 6: // Prolog
  134. var prolog = r.matched(0);
  135. var x = Xml.createProlog(prolog.substr(2,prolog.length - 4));
  136. current.addChild(x);
  137. str = r.matchedRight();
  138. }
  139. break;
  140. }
  141. i += 1;
  142. }
  143. if( i == nrules ) {
  144. if( str.length > 10 )
  145. throw ("Xml parse error : Unexpected "+str.substr(0,10)+"...");
  146. else
  147. throw ("Xml parse error : Unexpected "+str);
  148. }
  149. }
  150. if( !stack.isEmpty() )
  151. throw "Xml parse error : Unclosed "+stack.last().nodeName;
  152. untyped return current;
  153. }
  154. private function new() : Void {
  155. }
  156. public static function createElement( name : String ) : Xml {
  157. var r = new Xml();
  158. r.nodeType = Xml.Element;
  159. r._children = new Array();
  160. r._attributes = new Hash();
  161. r.setNodeName( name );
  162. return r;
  163. }
  164. public static function createPCData( data : String ) : Xml {
  165. var r = new Xml();
  166. r.nodeType = Xml.PCData;
  167. r.setNodeValue( data );
  168. return r;
  169. }
  170. public static function createCData( data : String ) : Xml {
  171. var r = new Xml();
  172. r.nodeType = Xml.CData;
  173. r.setNodeValue( data );
  174. return r;
  175. }
  176. public static function createComment( data : String ) : Xml {
  177. var r = new Xml();
  178. r.nodeType = Xml.Comment;
  179. r.setNodeValue( data );
  180. return r;
  181. }
  182. public static function createDocType( data : String ) : Xml {
  183. var r = new Xml();
  184. r.nodeType = Xml.DocType;
  185. r.setNodeValue( data );
  186. return r;
  187. }
  188. public static function createProlog( data : String ) : Xml {
  189. var r = new Xml();
  190. r.nodeType = Xml.Prolog;
  191. r.setNodeValue( data );
  192. return r;
  193. }
  194. public static function createDocument() : Xml {
  195. var r = new Xml();
  196. r.nodeType = Xml.Document;
  197. r._children = new Array();
  198. return r;
  199. }
  200. private function getNodeName() : String {
  201. if( nodeType != Xml.Element )
  202. throw "bad nodeType";
  203. return _nodeName;
  204. }
  205. private function setNodeName( n : String ) : String {
  206. if( nodeType != Xml.Element )
  207. throw "bad nodeType";
  208. return _nodeName = n;
  209. }
  210. private function getNodeValue() : String {
  211. if( nodeType == Xml.Element || nodeType == Xml.Document )
  212. throw "bad nodeType";
  213. return _nodeValue;
  214. }
  215. private function setNodeValue( v : String ) : String {
  216. if( nodeType == Xml.Element || nodeType == Xml.Document )
  217. throw "bad nodeType";
  218. return _nodeValue = v;
  219. }
  220. private function getParent() : Xml {
  221. return _parent;
  222. }
  223. public function get( att : String ) : String {
  224. if( nodeType != Xml.Element )
  225. throw "bad nodeType";
  226. return _attributes.get( att );
  227. }
  228. public function set( att : String, value : String ) : Void {
  229. if( nodeType != Xml.Element )
  230. throw "bad nodeType";
  231. _attributes.set( att, value );
  232. }
  233. public function remove( att : String ) : Void{
  234. if( nodeType != Xml.Element )
  235. throw "bad nodeType";
  236. _attributes.remove( att );
  237. }
  238. public function exists( att : String ) : Bool {
  239. if( nodeType != Xml.Element )
  240. throw "bad nodeType";
  241. return _attributes.exists( att );
  242. }
  243. public function attributes() : Iterator<String> {
  244. if( nodeType != Xml.Element )
  245. throw "bad nodeType";
  246. return _attributes.keys();
  247. }
  248. public function iterator() : Iterator<Xml> {
  249. if( _children == null ) throw "bad nodetype";
  250. return untyped {
  251. cur: 0,
  252. x: this._children,
  253. hasNext : function(){
  254. return __this__.cur < __this__.x.length;
  255. },
  256. next : function(){
  257. return __this__.x[__this__.cur++];
  258. }
  259. }
  260. }
  261. public function elements() : Iterator<Xml> {
  262. if( _children == null ) throw "bad nodetype";
  263. return untyped {
  264. cur: 0,
  265. x: this._children,
  266. hasNext : function() {
  267. var k = __this__.cur;
  268. var l = __this__.x.length;
  269. while( k < l ) {
  270. if( __this__.x[k].nodeType == Xml.Element )
  271. break;
  272. k += 1;
  273. }
  274. __this__.cur = k;
  275. return k < l;
  276. },
  277. next : function() {
  278. var k = __this__.cur;
  279. var l = __this__.x.length;
  280. while( k < l ) {
  281. var n = __this__.x[k];
  282. k += 1;
  283. if( n.nodeType == Xml.Element ) {
  284. __this__.cur = k;
  285. return n;
  286. }
  287. }
  288. return null;
  289. }
  290. }
  291. }
  292. public function elementsNamed( name : String ) : Iterator<Xml> {
  293. if( _children == null ) throw "bad nodetype";
  294. return untyped {
  295. cur: 0,
  296. x: this._children,
  297. hasNext : function() {
  298. var k = __this__.cur;
  299. var l = __this__.x.length;
  300. while( k < l ) {
  301. var n = __this__.x[k];
  302. if( n.nodeType == Xml.Element && n._nodeName == name )
  303. break;
  304. k++;
  305. }
  306. __this__.cur = k;
  307. return k < l;
  308. },
  309. next : function() {
  310. var k = __this__.cur;
  311. var l = __this__.x.length;
  312. while( k < l ) {
  313. var n = __this__.x[k];
  314. k++;
  315. if( n.nodeType == Xml.Element && n._nodeName == name ) {
  316. __this__.cur = k;
  317. return n;
  318. }
  319. }
  320. return null;
  321. }
  322. }
  323. }
  324. public function firstChild() : Xml {
  325. if( _children == null ) throw "bad nodetype";
  326. return _children[0];
  327. }
  328. public function firstElement() : Xml {
  329. if( _children == null ) throw "bad nodetype";
  330. var cur = 0;
  331. var l = _children.length;
  332. while( cur < l ) {
  333. var n = _children[cur];
  334. if( n.nodeType == Xml.Element )
  335. return n;
  336. cur++;
  337. }
  338. return null;
  339. }
  340. public function addChild( x : Xml ) : Void {
  341. if( _children == null ) throw "bad nodetype";
  342. if( x._parent != null ) x._parent._children.remove(x);
  343. x._parent = this;
  344. _children.push( x );
  345. }
  346. public function removeChild( x : Xml ) : Bool {
  347. if( _children == null ) throw "bad nodetype";
  348. var b = _children.remove( x );
  349. if( b )
  350. x._parent = null;
  351. return b;
  352. }
  353. public function insertChild( x : Xml, pos : Int ) : Void {
  354. if( _children == null ) throw "bad nodetype";
  355. if( x._parent != null ) x._parent._children.remove(x);
  356. x._parent = this;
  357. _children.insert( pos, x );
  358. }
  359. public function toString() : String {
  360. if( nodeType == Xml.PCData )
  361. return _nodeValue;
  362. if( nodeType == Xml.CData )
  363. return "<![CDATA["+_nodeValue+"]]>";
  364. if( nodeType == Xml.Comment )
  365. return "<!--"+_nodeValue+"-->";
  366. if( nodeType == Xml.DocType )
  367. return "<!DOCTYPE "+_nodeValue+">";
  368. if( nodeType == Xml.Prolog )
  369. return "<?"+_nodeValue+"?>";
  370. var s = new StringBuf();
  371. if( nodeType == Xml.Element ) {
  372. s.add("<");
  373. s.add(_nodeName);
  374. for( k in _attributes.keys() ){
  375. s.add(" ");
  376. s.add(k);
  377. s.add("=\"");
  378. s.add(_attributes.get(k));
  379. s.add("\"");
  380. }
  381. if( _children.length == 0 ) {
  382. s.add("/>");
  383. return s.toString();
  384. }
  385. s.add(">");
  386. }
  387. for( x in iterator() )
  388. s.add(x.toString());
  389. if( nodeType == Xml.Element ) {
  390. s.add("</");
  391. s.add(_nodeName);
  392. s.add(">");
  393. }
  394. return s.toString();
  395. }
  396. static function __init__() : Void untyped {
  397. Xml.Element = "element";
  398. Xml.PCData = "pcdata";
  399. Xml.CData = "cdata";
  400. Xml.Comment = "comment";
  401. Xml.DocType = "doctype";
  402. Xml.Prolog = "prolog";
  403. Xml.Document = "document";
  404. }
  405. }