Xml.hx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (C)2005-2015 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 abstract XmlType(Int) {
  23. var Element = 0;
  24. var PCData = 1;
  25. var CData = 2;
  26. var Comment = 3;
  27. var DocType = 4;
  28. var ProcessingInstruction = 5;
  29. var Document = 6;
  30. }
  31. class Xml {
  32. static public var Element(default,null) = XmlType.Element;
  33. static public var PCData(default,null) = XmlType.PCData;
  34. static public var CData(default,null) = XmlType.CData;
  35. static public var Comment(default,null) = XmlType.Comment;
  36. static public var DocType(default,null) = XmlType.DocType;
  37. static public var ProcessingInstruction(default,null) = XmlType.ProcessingInstruction;
  38. static public var Document(default,null) = XmlType.Document;
  39. static public function parse( str : String ) : Xml {
  40. return haxe.xml.Parser.parse(str);
  41. }
  42. /**
  43. Returns the type of the Xml Node. This should be used before
  44. accessing other functions since some might raise an exception
  45. if the node type is not correct.
  46. **/
  47. public var nodeType(default, null) : XmlType;
  48. /**
  49. Returns the node name of an Element.
  50. **/
  51. @:isVar public var nodeName(get, set) : String;
  52. /**
  53. Returns the node value. Only works if the Xml node is not an Element or a Document.
  54. **/
  55. @:isVar public var nodeValue(get, set) : String;
  56. /**
  57. Returns the parent object in the Xml hierarchy.
  58. The parent can be [null], an Element or a Document.
  59. **/
  60. public var parent(default, null) : Xml;
  61. var children:Array<Xml>;
  62. var attributeMap:Map<String, String>;
  63. inline function get_nodeName() {
  64. if (nodeType != Element) {
  65. throw 'Bad node type, expected Element but found $nodeType';
  66. }
  67. return nodeName;
  68. }
  69. inline function set_nodeName(v) {
  70. if (nodeType != Element) {
  71. throw 'Bad node type, expected Element but found $nodeType';
  72. }
  73. return this.nodeName = v;
  74. }
  75. inline function get_nodeValue() {
  76. if (nodeType == Document || nodeType == Element) {
  77. throw 'Bad node type, unexpected $nodeType';
  78. }
  79. return nodeValue;
  80. }
  81. inline function set_nodeValue(v) {
  82. if (nodeType == Document || nodeType == Element) {
  83. throw 'Bad node type, unexpected $nodeType';
  84. }
  85. return this.nodeValue = v;
  86. }
  87. /**
  88. Creates a node of the given type.
  89. **/
  90. static public function createElement( name : String ) : Xml {
  91. var xml = new Xml(Element);
  92. xml.nodeName = name;
  93. return xml;
  94. }
  95. /**
  96. Creates a node of the given type.
  97. **/
  98. static public function createPCData( data : String ) : Xml {
  99. var xml = new Xml(PCData);
  100. xml.nodeValue = data;
  101. return xml;
  102. }
  103. /**
  104. Creates a node of the given type.
  105. **/
  106. static public function createCData( data : String ) : Xml {
  107. var xml = new Xml(CData);
  108. xml.nodeValue = data;
  109. return xml;
  110. }
  111. /**
  112. Creates a node of the given type.
  113. **/
  114. static public function createComment( data : String ) : Xml {
  115. var xml = new Xml(Comment);
  116. xml.nodeValue = data;
  117. return xml;
  118. }
  119. /**
  120. Creates a node of the given type.
  121. **/
  122. static public function createDocType( data : String ) : Xml {
  123. var xml = new Xml(DocType);
  124. xml.nodeValue = data;
  125. return xml;
  126. }
  127. /**
  128. Creates a node of the given type.
  129. **/
  130. static public function createProcessingInstruction( data : String ) : Xml {
  131. var xml = new Xml(ProcessingInstruction);
  132. xml.nodeValue = data;
  133. return xml;
  134. }
  135. /**
  136. Creates a node of the given type.
  137. **/
  138. static public function createDocument() : Xml {
  139. return new Xml(Document);
  140. }
  141. /**
  142. Get the given attribute of an Element node. Returns [null] if not found.
  143. Attributes are case-sensitive.
  144. **/
  145. public function get( att : String ) : String {
  146. if (nodeType != Element) {
  147. throw 'Bad node type, expected Element but found $nodeType';
  148. }
  149. return attributeMap[att];
  150. }
  151. /**
  152. Set the given attribute value for an Element node.
  153. Attributes are case-sensitive.
  154. **/
  155. public function set( att : String, value : String ) : Void {
  156. if (nodeType != Element) {
  157. throw 'Bad node type, expected Element but found $nodeType';
  158. }
  159. attributeMap.set(att, value);
  160. }
  161. /**
  162. Removes an attribute for an Element node.
  163. Attributes are case-sensitive.
  164. **/
  165. public function remove( att : String ) : Void {
  166. if (nodeType != Element) {
  167. throw 'Bad node type, expected Element but found $nodeType';
  168. }
  169. attributeMap.remove(att);
  170. }
  171. /**
  172. Tells if the Element node has a given attribute.
  173. Attributes are case-sensitive.
  174. **/
  175. public function exists( att : String ) : Bool {
  176. if (nodeType != Element) {
  177. throw 'Bad node type, expected Element but found $nodeType';
  178. }
  179. return attributeMap.exists(att);
  180. }
  181. /**
  182. Returns an [Iterator] on all the attribute names.
  183. **/
  184. public function attributes() : Iterator<String> {
  185. if (nodeType != Element) {
  186. throw 'Bad node type, expected Element but found $nodeType';
  187. }
  188. return attributeMap.keys();
  189. }
  190. /**
  191. Returns an iterator of all child nodes.
  192. Only works if the current node is an Element or a Document.
  193. **/
  194. public inline function iterator() : Iterator<Xml> {
  195. ensureElementType();
  196. return children.iterator();
  197. }
  198. /**
  199. Returns an iterator of all child nodes which are Elements.
  200. Only works if the current node is an Element or a Document.
  201. **/
  202. public function elements() : Iterator<Xml> {
  203. ensureElementType();
  204. var ret = [for (child in children) if (child.nodeType == Element) child];
  205. return ret.iterator();
  206. }
  207. /**
  208. Returns an iterator of all child nodes which are Elements with the given nodeName.
  209. Only works if the current node is an Element or a Document.
  210. **/
  211. public function elementsNamed( name : String ) : Iterator<Xml> {
  212. ensureElementType();
  213. var ret = [for (child in children) if (child.nodeType == Element && child.nodeName == name) child];
  214. return ret.iterator();
  215. }
  216. /**
  217. Returns the first child node.
  218. **/
  219. public inline function firstChild() : Xml {
  220. ensureElementType();
  221. return children[0];
  222. }
  223. /**
  224. Returns the first child node which is an Element.
  225. **/
  226. public function firstElement() : Xml {
  227. ensureElementType();
  228. for (child in children) {
  229. if (child.nodeType == Element) {
  230. return child;
  231. }
  232. }
  233. return null;
  234. }
  235. /**
  236. Adds a child node to the Document or Element.
  237. One node can only be inside one given node which is indicated by the [parent] property.
  238. **/
  239. public function addChild( x : Xml ) : Void {
  240. ensureElementType();
  241. if (x.parent == this) {
  242. return;
  243. } else if (x.parent != null) {
  244. x.parent.removeChild(x);
  245. }
  246. children.push(x);
  247. x.parent = this;
  248. }
  249. /**
  250. Removes a child from the Document or Element.
  251. Returns true if the child was successfuly removed.
  252. **/
  253. public function removeChild( x : Xml ) : Bool {
  254. ensureElementType();
  255. return children.remove(x);
  256. }
  257. /**
  258. Inserts a child at the given position among the other childs.
  259. **/
  260. public function insertChild( x : Xml, pos : Int ) : Void {
  261. ensureElementType();
  262. children.insert(pos, x);
  263. }
  264. /**
  265. Returns a String representation of the Xml node.
  266. **/
  267. public inline function toString() : String {
  268. return haxe.xml.Printer.print(this);
  269. }
  270. function new(nodeType:XmlType) {
  271. this.nodeType = nodeType;
  272. children = [];
  273. attributeMap = new Map();
  274. }
  275. inline function ensureElementType() {
  276. if (nodeType != Document && nodeType != Element) {
  277. throw 'Bad node type, expected Element or Document but found $nodeType';
  278. }
  279. }
  280. }