JsXml__.hx 11 KB

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