Xml.hx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (C)2005-2019 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. /**
  23. Xml node types.
  24. @see https://haxe.org/manual/std-Xml.html
  25. **/
  26. enum abstract XmlType(Int) {
  27. /**
  28. Represents an XML element type.
  29. **/
  30. var Element = 0;
  31. /**
  32. Represents XML parsed character data type.
  33. **/
  34. var PCData = 1;
  35. /**
  36. Represents XML character data type.
  37. **/
  38. var CData = 2;
  39. /**
  40. Represents an XML comment type.
  41. **/
  42. var Comment = 3;
  43. /**
  44. Represents an XML doctype element type.
  45. **/
  46. var DocType = 4;
  47. /**
  48. Represents an XML processing instruction type.
  49. **/
  50. var ProcessingInstruction = 5;
  51. /**
  52. Represents an XML document type.
  53. **/
  54. var Document = 6;
  55. public function toString():String {
  56. return switch (cast this : XmlType) {
  57. case Element: "Element";
  58. case PCData: "PCData";
  59. case CData: "CData";
  60. case Comment: "Comment";
  61. case DocType: "DocType";
  62. case ProcessingInstruction: "ProcessingInstruction";
  63. case Document: "Document";
  64. };
  65. }
  66. }
  67. /**
  68. Cross-platform Xml API.
  69. @see https://haxe.org/manual/std-Xml.html
  70. **/
  71. class Xml {
  72. /**
  73. XML element type.
  74. **/
  75. static public var Element(default, never) = XmlType.Element;
  76. /**
  77. XML parsed character data type.
  78. **/
  79. static public var PCData(default, never) = XmlType.PCData;
  80. /**
  81. XML character data type.
  82. **/
  83. static public var CData(default, never) = XmlType.CData;
  84. /**
  85. XML comment type.
  86. **/
  87. static public var Comment(default, never) = XmlType.Comment;
  88. /**
  89. XML doctype element type.
  90. **/
  91. static public var DocType(default, never) = XmlType.DocType;
  92. /**
  93. XML processing instruction type.
  94. **/
  95. static public var ProcessingInstruction(default, never) = XmlType.ProcessingInstruction;
  96. /**
  97. XML document type.
  98. **/
  99. static public var Document(default, never) = XmlType.Document;
  100. /**
  101. Parses the String into an Xml document.
  102. **/
  103. static public function parse(str:String):Xml {
  104. return haxe.xml.Parser.parse(str);
  105. }
  106. /**
  107. Returns the type of the Xml Node. This should be used before
  108. accessing other functions since some might raise an exception
  109. if the node type is not correct.
  110. **/
  111. public var nodeType(default, null):XmlType;
  112. /**
  113. Returns the node name of an Element.
  114. **/
  115. @:isVar public var nodeName(get, set):String;
  116. /**
  117. Returns the node value. Only works if the Xml node is not an Element or a Document.
  118. **/
  119. @:isVar public var nodeValue(get, set):String;
  120. /**
  121. Returns the parent object in the Xml hierarchy.
  122. The parent can be `null`, an Element or a Document.
  123. **/
  124. public var parent(default, null):Xml;
  125. var children:Array<Xml>;
  126. var attributeMap:Map<String, String>;
  127. #if !cppia inline #end function get_nodeName() {
  128. if (nodeType != Element) {
  129. throw 'Bad node type, expected Element but found $nodeType';
  130. }
  131. return nodeName;
  132. }
  133. #if !cppia inline #end function set_nodeName(v) {
  134. if (nodeType != Element) {
  135. throw 'Bad node type, expected Element but found $nodeType';
  136. }
  137. return this.nodeName = v;
  138. }
  139. #if !cppia inline #end function get_nodeValue() {
  140. if (nodeType == Document || nodeType == Element) {
  141. throw 'Bad node type, unexpected $nodeType';
  142. }
  143. return nodeValue;
  144. }
  145. #if !cppia inline #end function set_nodeValue(v) {
  146. if (nodeType == Document || nodeType == Element) {
  147. throw 'Bad node type, unexpected $nodeType';
  148. }
  149. return this.nodeValue = v;
  150. }
  151. /**
  152. Creates a node of the given type.
  153. **/
  154. static public function createElement(name:String):Xml {
  155. var xml = new Xml(Element);
  156. xml.nodeName = name;
  157. return xml;
  158. }
  159. /**
  160. Creates a node of the given type.
  161. **/
  162. static public function createPCData(data:String):Xml {
  163. var xml = new Xml(PCData);
  164. xml.nodeValue = data;
  165. return xml;
  166. }
  167. /**
  168. Creates a node of the given type.
  169. **/
  170. static public function createCData(data:String):Xml {
  171. var xml = new Xml(CData);
  172. xml.nodeValue = data;
  173. return xml;
  174. }
  175. /**
  176. Creates a node of the given type.
  177. **/
  178. static public function createComment(data:String):Xml {
  179. var xml = new Xml(Comment);
  180. xml.nodeValue = data;
  181. return xml;
  182. }
  183. /**
  184. Creates a node of the given type.
  185. **/
  186. static public function createDocType(data:String):Xml {
  187. var xml = new Xml(DocType);
  188. xml.nodeValue = data;
  189. return xml;
  190. }
  191. /**
  192. Creates a node of the given type.
  193. **/
  194. static public function createProcessingInstruction(data:String):Xml {
  195. var xml = new Xml(ProcessingInstruction);
  196. xml.nodeValue = data;
  197. return xml;
  198. }
  199. /**
  200. Creates a node of the given type.
  201. **/
  202. static public function createDocument():Xml {
  203. return new Xml(Document);
  204. }
  205. /**
  206. Get the given attribute of an Element node. Returns `null` if not found.
  207. Attributes are case-sensitive.
  208. **/
  209. public function get(att:String):String {
  210. if (nodeType != Element) {
  211. throw 'Bad node type, expected Element but found $nodeType';
  212. }
  213. return attributeMap[att];
  214. }
  215. /**
  216. Set the given attribute value for an Element node.
  217. Attributes are case-sensitive.
  218. **/
  219. public function set(att:String, value:String):Void {
  220. if (nodeType != Element) {
  221. throw 'Bad node type, expected Element but found $nodeType';
  222. }
  223. attributeMap.set(att, value);
  224. }
  225. /**
  226. Removes an attribute for an Element node.
  227. Attributes are case-sensitive.
  228. **/
  229. public function remove(att:String):Void {
  230. if (nodeType != Element) {
  231. throw 'Bad node type, expected Element but found $nodeType';
  232. }
  233. attributeMap.remove(att);
  234. }
  235. /**
  236. Tells if the Element node has a given attribute.
  237. Attributes are case-sensitive.
  238. **/
  239. public function exists(att:String):Bool {
  240. if (nodeType != Element) {
  241. throw 'Bad node type, expected Element but found $nodeType';
  242. }
  243. return attributeMap.exists(att);
  244. }
  245. /**
  246. Returns an `Iterator` on all the attribute names.
  247. **/
  248. public function attributes():Iterator<String> {
  249. if (nodeType != Element) {
  250. throw 'Bad node type, expected Element but found $nodeType';
  251. }
  252. return attributeMap.keys();
  253. }
  254. /**
  255. Returns an iterator of all child nodes.
  256. Only works if the current node is an Element or a Document.
  257. **/
  258. public #if !cppia inline #end function iterator():Iterator<Xml> {
  259. ensureElementType();
  260. return children.iterator();
  261. }
  262. /**
  263. Returns an iterator of all child nodes which are Elements.
  264. Only works if the current node is an Element or a Document.
  265. **/
  266. public function elements():Iterator<Xml> {
  267. ensureElementType();
  268. var ret = [for (child in children) if (child.nodeType == Element) child];
  269. return ret.iterator();
  270. }
  271. /**
  272. Returns an iterator of all child nodes which are Elements with the given nodeName.
  273. Only works if the current node is an Element or a Document.
  274. **/
  275. public function elementsNamed(name:String):Iterator<Xml> {
  276. ensureElementType();
  277. var ret = [
  278. for (child in children)
  279. if (child.nodeType == Element && child.nodeName == name) child
  280. ];
  281. return ret.iterator();
  282. }
  283. /**
  284. Returns the first child node.
  285. **/
  286. public #if !cppia inline #end function firstChild():Xml {
  287. ensureElementType();
  288. return children[0];
  289. }
  290. /**
  291. Returns the first child node which is an Element.
  292. **/
  293. public function firstElement():Xml {
  294. ensureElementType();
  295. for (child in children) {
  296. if (child.nodeType == Element) {
  297. return child;
  298. }
  299. }
  300. return null;
  301. }
  302. /**
  303. Adds a child node to the Document or Element.
  304. A child node can only be inside one given parent node, which is indicated by the `parent` property.
  305. If the child is already inside this Document or Element, it will be moved to the last position among the Document or Element's children.
  306. If the child node was previously inside a different node, it will be moved to this Document or Element.
  307. **/
  308. public function addChild(x:Xml):Void {
  309. ensureElementType();
  310. if (x.parent != null) {
  311. x.parent.removeChild(x);
  312. }
  313. children.push(x);
  314. x.parent = this;
  315. }
  316. /**
  317. Removes a child from the Document or Element.
  318. Returns true if the child was successfuly removed.
  319. **/
  320. public function removeChild(x:Xml):Bool {
  321. ensureElementType();
  322. if (children.remove(x)) {
  323. x.parent = null;
  324. return true;
  325. }
  326. return false;
  327. }
  328. /**
  329. Inserts a child at the given position among the other childs.
  330. A child node can only be inside one given parent node, which is indicated by the [parent] property.
  331. If the child is already inside this Document or Element, it will be moved to the new position among the Document or Element's children.
  332. If the child node was previously inside a different node, it will be moved to this Document or Element.
  333. **/
  334. public function insertChild(x:Xml, pos:Int):Void {
  335. ensureElementType();
  336. if (x.parent != null) {
  337. x.parent.children.remove(x);
  338. }
  339. children.insert(pos, x);
  340. x.parent = this;
  341. }
  342. /**
  343. Returns a String representation of the Xml node.
  344. **/
  345. public #if !cppia inline #end function toString():String {
  346. return haxe.xml.Printer.print(this);
  347. }
  348. function new(nodeType:XmlType) {
  349. this.nodeType = nodeType;
  350. children = [];
  351. attributeMap = new Map();
  352. }
  353. inline function ensureElementType() {
  354. if (nodeType != Document && nodeType != Element) {
  355. throw 'Bad node type, expected Element or Document but found $nodeType';
  356. }
  357. }
  358. }