Xml.hx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. import flash.xml.XML;
  26. import flash.xml.XMLList;
  27. enum XmlType {
  28. }
  29. @:core_api class Xml {
  30. public static var Element(default,null) : XmlType;
  31. public static var PCData(default,null) : XmlType;
  32. public static var CData(default,null) : XmlType;
  33. public static var Comment(default,null) : XmlType;
  34. public static var DocType(default,null) : XmlType;
  35. public static var Prolog(default,null) : XmlType;
  36. public static var Document(default,null) : XmlType;
  37. public var nodeType(default,null) : XmlType;
  38. public var nodeName(getNodeName,setNodeName) : String;
  39. public var nodeValue(getNodeValue,setNodeValue) : String;
  40. public var parent(getParent,null) : Xml;
  41. var _map : flash.utils.Dictionary;
  42. var _node : flash.xml.XML;
  43. public static function parse( str : String ) : Xml {
  44. XML.ignoreWhitespace = false;
  45. XML.ignoreProcessingInstructions = false;
  46. XML.ignoreComments = false;
  47. var prefix = "<__document";
  48. var root = null;
  49. while( root == null ) {
  50. try {
  51. root = new flash.xml.XML(prefix+">" + str + "</__document>");
  52. } catch( e : flash.errors.TypeError ) {
  53. // if we miss a namespace, let's add it !
  54. var r = ~/"([^"]+)"/; //"
  55. if( e.errorID == 1083 && r.match(e.message) ) {
  56. var ns = r.matched(1);
  57. prefix += " xmlns:" + ns + '="@' + ns + '"';
  58. } else
  59. throw e;
  60. }
  61. }
  62. return wrap( null, root, Xml.Document );
  63. }
  64. private function new() : Void {}
  65. public static function createElement( name : String ) : Xml {
  66. return wrap( null, new flash.xml.XML("<"+name+"/>"), Xml.Element );
  67. }
  68. public static function createPCData( data : String ) : Xml {
  69. XML.ignoreWhitespace = false;
  70. return wrap( null, new flash.xml.XML(data), Xml.PCData );
  71. }
  72. public static function createCData( data : String ) : Xml {
  73. return wrap( null, new flash.xml.XML("<![CDATA["+data+"]]>"), Xml.CData );
  74. }
  75. public static function createComment( data : String ) : Xml {
  76. XML.ignoreComments = false;
  77. return wrap( null, new flash.xml.XML("<!--"+data+"-->"), Xml.Comment );
  78. }
  79. public static function createDocType( data : String ) : Xml {
  80. return wrap( null, new flash.xml.XML("<!DOCTYPE "+data+">"), Xml.DocType );
  81. }
  82. public static function createProlog( data : String ) : Xml {
  83. XML.ignoreProcessingInstructions = false;
  84. return wrap( null, new flash.xml.XML("<?"+data+"?>"), Xml.Prolog );
  85. }
  86. public static function createDocument() : Xml {
  87. return wrap( null, new flash.xml.XML("<__document/>"), Xml.Document );
  88. }
  89. private static function getNodeType( node : flash.xml.XML ) : XmlType {
  90. switch( node.nodeKind() ) {
  91. case "element":
  92. return Xml.Element;
  93. case "text":
  94. return Xml.PCData;
  95. case "processing-instruction":
  96. return Xml.Prolog;
  97. case "comment":
  98. return Xml.Comment;
  99. default :
  100. throw "unimplemented node type: " + node.nodeType;
  101. }
  102. return null;
  103. }
  104. private function getNodeName() : String {
  105. if( nodeType != Xml.Element )
  106. throw "bad nodeType";
  107. var ns = _node.namespace();
  108. return (ns.prefix == "") ? _node.localName() : ns.prefix+":"+_node.localName();
  109. }
  110. private function setNodeName( n : String ) : String {
  111. if( nodeType != Xml.Element )
  112. throw "bad nodeType";
  113. var ns = n.split(":");
  114. if( ns.length == 1 )
  115. _node.setLocalName(n);
  116. else {
  117. _node.setLocalName(ns[1]);
  118. _node.setNamespace(_node.namespace(ns[0]));
  119. }
  120. return n;
  121. }
  122. private function getNodeValue() : String {
  123. var nodeType = nodeType;
  124. if( nodeType == Xml.Element || nodeType == Xml.Document )
  125. throw "bad nodeType";
  126. if( nodeType == Xml.Comment )
  127. return _node.toString().substr(4,-7);
  128. return _node.toString();
  129. }
  130. private function setNodeValue( v : String ) : String {
  131. var nodeType = nodeType;
  132. var x = null;
  133. if( nodeType == Xml.Element || nodeType == Xml.Document )
  134. throw "bad nodeType";
  135. else if( nodeType == Xml.PCData )
  136. x = createPCData(v);
  137. else if( nodeType == Xml.CData )
  138. x = createCData(v);
  139. else if( nodeType == Xml.Comment )
  140. x = createComment(v);
  141. else if( nodeType == Xml.DocType )
  142. x = createDocType(v);
  143. else
  144. x = createProlog(v);
  145. var p = _node.parent();
  146. if( p != null ) {
  147. p.insertChildAfter(_node, x._node);
  148. var i = _node.childIndex();
  149. var children = p.children();
  150. untyped __delete__(children, Reflect.fields(children)[i]);
  151. }
  152. _node = x._node;
  153. untyped _map[_node] = this;
  154. return v;
  155. }
  156. private function getParent() :Xml {
  157. return wrap( _map, _node.parent() );
  158. }
  159. private static function wrap( map : flash.utils.Dictionary, node : XML, ?type : XmlType ) : Xml {
  160. if( map == null )
  161. map = new flash.utils.Dictionary();
  162. var x : Xml = untyped map[node];
  163. if( x == null ) {
  164. x = new Xml();
  165. x._node = node;
  166. x._map = map;
  167. x.nodeType = (type != null) ? type : getNodeType( node );
  168. untyped map[node] = x;
  169. }
  170. return x;
  171. }
  172. private function wraps( xList : XMLList ) : Array<Xml> {
  173. var out = new Array<Xml>();
  174. for( i in 0...xList.length() )
  175. out.push( wrap(_map,xList[i]) );
  176. return out;
  177. }
  178. function getAttribNS( cur : XML, ns : Array<String> ) : XMLList {
  179. var n = cur.namespace(ns[0]);
  180. if( n == null ) {
  181. var parent = cur.parent();
  182. if( parent == null ) {
  183. n = new flash.utils.Namespace(ns[0], "@"+ns[0]);
  184. cur.addNamespace(n);
  185. } else
  186. return getAttribNS(parent, ns);
  187. }
  188. return _node.attribute(new flash.utils.QName(n,ns[1]));
  189. }
  190. public function get( att : String ) : String {
  191. if( nodeType != Xml.Element )
  192. throw "bad nodeType";
  193. var ns = att.split(":");
  194. if( ns[0] == "xmlns" ) {
  195. var n = _node.namespace((ns[1] == null) ? "" : ns[1]);
  196. return (n == null) ? null : n.uri;
  197. }
  198. if( ns.length == 1 ) {
  199. if( !Reflect.hasField(_node,"@"+att) )
  200. return null;
  201. return Reflect.field(_node, "@"+att);
  202. }
  203. var a = getAttribNS(_node,ns);
  204. return (a.length() == 0) ? null : a.toString();
  205. }
  206. public function set( att : String, value : String ) : Void {
  207. if( nodeType != Xml.Element )
  208. throw "bad nodeType";
  209. var ns = att.split(":");
  210. if( ns[0] == "xmlns" ) {
  211. var n = _node.namespace((ns[1] == null) ? "" : ns[1]);
  212. if( n != null )
  213. throw "Can't modify namespace";
  214. if( ns[1] == null )
  215. throw "Can't set default namespace";
  216. _node.addNamespace(new flash.utils.Namespace(ns[1], value));
  217. return;
  218. }
  219. if( ns.length == 1 )
  220. Reflect.setField(_node, "@"+att, value);
  221. else {
  222. var a = getAttribNS(_node,ns);
  223. untyped a[0] = value;
  224. }
  225. }
  226. public function remove( att : String ) : Void{
  227. if( nodeType != Xml.Element )
  228. throw "bad nodeType";
  229. var ns = att.split(":");
  230. if( ns.length == 1 )
  231. Reflect.deleteField(_node, "@"+att);
  232. else
  233. untyped __delete__(getAttribNS(_node,ns),0);
  234. }
  235. public function exists( att : String ) : Bool {
  236. if( nodeType != Xml.Element )
  237. throw "bad nodeType";
  238. var ns = att.split(":");
  239. if( ns[0] == "xmlns" )
  240. return _node.namespace((ns[1] == null) ? "" : ns[1]) != null;
  241. if( ns.length == 1 )
  242. return Reflect.hasField(_node, "@"+att);
  243. return getAttribNS(_node,ns).length() > 0;
  244. }
  245. public function attributes() : Iterator<String> {
  246. if( nodeType != Xml.Element )
  247. throw "bad nodeType";
  248. var attributes :XMLList = _node.attributes();
  249. var names = Reflect.fields(attributes);
  250. var cur = 0;
  251. return {
  252. hasNext : function(){
  253. return cur < names.length;
  254. },
  255. next : function(){
  256. return attributes[Std.parseInt(names[cur++])].name();
  257. }
  258. }
  259. }
  260. public function iterator() : Iterator<Xml> {
  261. var children:XMLList = _node.children();
  262. if( children == null )
  263. throw "bad nodetype";
  264. var wrappers :Array<Xml> = wraps(children);
  265. var cur = 0;
  266. return {
  267. hasNext : function(){
  268. return cur < wrappers.length;
  269. },
  270. next : function(){
  271. return wrappers[cur++];
  272. }
  273. };
  274. }
  275. public function elements() : Iterator<Xml> {
  276. var elements:XMLList = _node.elements();
  277. if( elements == null )
  278. throw "bad nodetype";
  279. var wrappers :Array<Xml> = wraps(elements);
  280. var cur = 0;
  281. return {
  282. hasNext : function(){
  283. return cur < wrappers.length;
  284. },
  285. next : function(){
  286. return wrappers[cur++];
  287. }
  288. };
  289. }
  290. public function elementsNamed( name : String ) : Iterator<Xml> {
  291. var ns = name.split(":");
  292. var elements:XMLList;
  293. if( ns.length == 1 )
  294. elements = _node.elements(name);
  295. else
  296. elements = _node.elements();
  297. if( elements == null )
  298. throw "bad nodetype";
  299. var wrappers :Array<Xml> = wraps(elements);
  300. if( ns.length != 1 )
  301. for( w in wrappers.copy() )
  302. if( w._node.localName() != ns[1] || w._node.namespace().prefix != ns[0] )
  303. wrappers.remove(w);
  304. var cur = 0;
  305. return {
  306. hasNext : function(){
  307. return cur < wrappers.length;
  308. },
  309. next : function(){
  310. return wrappers[cur++];
  311. }
  312. };
  313. }
  314. public function firstChild() : Xml {
  315. var children:XMLList = _node.children();
  316. if( children == null )
  317. throw "bad nodetype";
  318. if( children.length() == 0 )
  319. return null;
  320. return wrap( _map, children[0] );
  321. }
  322. public function firstElement() : Xml {
  323. var elements:XMLList = _node.elements();
  324. if( elements == null )
  325. throw "bad nodetype";
  326. if( elements.length() == 0 )
  327. return null;
  328. return wrap( _map, elements[0] );
  329. }
  330. public function addChild( x : Xml ) : Void {
  331. var children:XMLList = _node.children();
  332. if( children == null )
  333. throw "bad nodetype";
  334. _node.appendChild(x._node);
  335. }
  336. public function removeChild( x : Xml ) : Bool {
  337. var children:XMLList = _node.children();
  338. if( children == null )
  339. throw "bad nodetype";
  340. if( _node != x._node.parent() )
  341. return false;
  342. var i = x._node.childIndex();
  343. untyped __delete__(children, Reflect.fields(children)[i]);
  344. return true;
  345. }
  346. public function insertChild( x : Xml, pos : Int ) : Void {
  347. var children:XMLList = _node.children();
  348. if( children == null )
  349. throw "bad nodetype";
  350. if( pos < children.length() )
  351. _node.insertChildBefore(children[pos], x._node);
  352. else
  353. _node.appendChild(x._node);
  354. }
  355. public function toString() : String {
  356. XML.prettyPrinting = false;
  357. if( nodeType == Xml.Document ) {
  358. var str = _node.toXMLString();
  359. // remove <__document xmlns....>STR</__document> wrapper
  360. str = str.substr(str.indexOf(">") + 1);
  361. str = str.substr(0, str.length - 13);
  362. return str;
  363. }
  364. return _node.toXMLString();
  365. }
  366. static function __init__() : Void untyped {
  367. Element = "element";
  368. PCData = "pcdata";
  369. CData = "cdata";
  370. Comment = "comment";
  371. DocType = "doctype";
  372. Prolog = "prolog";
  373. Document = "document";
  374. }
  375. }