Xml.hx 12 KB

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