| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 
 | /*
 * Copyright (C)2005-2019 Haxe Foundation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
package flash;
import flash.xml.XML;
import flash.xml.XMLList;
extern enum XmlType {}
typedef NativeXml = Xml;
class Xml {
	public static var Element(default, null):XmlType;
	public static var PCData(default, null):XmlType;
	public static var CData(default, null):XmlType;
	public static var Comment(default, null):XmlType;
	public static var DocType(default, null):XmlType;
	public static var ProcessingInstruction(default, null):XmlType;
	public static var Document(default, null):XmlType;
	public var nodeType(default, null):XmlType;
	public var nodeName(get, set):String;
	public var nodeValue(get, set):String;
	public var parent(get, null):Xml;
	var _node:flash.xml.XML;
	public static function parse(str:String):Xml {
		XML.ignoreWhitespace = false;
		XML.ignoreProcessingInstructions = false;
		XML.ignoreComments = false;
		var prefix = "<__document";
		var root = null;
		while (root == null) {
			try {
				root = new flash.xml.XML(prefix + ">" + str + "</__document>");
			} catch (e:flash.errors.TypeError) {
				// if we miss a namespace, let's add it !
				var r = ~/"([^"]+)"/; // "
				if (e.errorID == 1083 && r.match(e.message)) {
					var ns = r.matched(1);
					prefix += " xmlns:" + ns + '="@' + ns + '"';
				} else
					throw e;
			}
		}
		return wrap(root, Xml.Document);
	}
	@:keep static function compare(a:Xml, b:Xml):Bool {
		return a == null ? b == null : (b == null ? false : a._node == b._node);
	}
	private function new():Void {}
	public static function createElement(name:String):Xml {
		return wrap(new flash.xml.XML("<" + name + "/>"), Xml.Element);
	}
	public static function createPCData(data:String):Xml {
		XML.ignoreWhitespace = false;
		return wrap(new flash.xml.XML(data), Xml.PCData);
	}
	public static function createCData(data:String):Xml {
		return wrap(new flash.xml.XML("<![CDATA[" + data + "]]>"), Xml.CData);
	}
	public static function createComment(data:String):Xml {
		XML.ignoreComments = false;
		return wrap(new flash.xml.XML("<!--" + data + "-->"), Xml.Comment);
	}
	public static function createDocType(data:String):Xml {
		return wrap(new flash.xml.XML("<!DOCTYPE " + data + ">"), Xml.DocType);
	}
	public static function createProcessingInstruction(data:String):Xml {
		XML.ignoreProcessingInstructions = false;
		return wrap(new flash.xml.XML("<?" + data + "?>"), Xml.ProcessingInstruction);
	}
	public static function createDocument():Xml {
		return wrap(new flash.xml.XML("<__document/>"), Xml.Document);
	}
	private static function getNodeType(node:flash.xml.XML):XmlType {
		switch (node.nodeKind()) {
			case "element":
				return Xml.Element;
			case "text":
				return Xml.PCData;
			case "processing-instruction":
				return Xml.ProcessingInstruction;
			case "comment":
				return Xml.Comment;
			default:
				throw "unimplemented node type: " + node.nodeType;
		}
	}
	private function get_nodeName():String {
		if (nodeType != Xml.Element)
			throw "bad nodeType";
		var ns = _node.namespace();
		return (ns.prefix == "") ? _node.localName() : ns.prefix + ":" + _node.localName();
	}
	private function set_nodeName(n:String):String {
		if (nodeType != Xml.Element)
			throw "bad nodeType";
		var ns = n.split(":");
		if (ns.length == 1)
			_node.setLocalName(n);
		else {
			_node.setLocalName(ns[1]);
			_node.setNamespace(_node.namespace(ns[0]));
		}
		return n;
	}
	private function get_nodeValue():String {
		var nodeType = nodeType;
		if (nodeType == Xml.Element || nodeType == Xml.Document)
			throw "bad nodeType";
		if (nodeType == Xml.Comment)
			return _node.toString().substr(4, -7);
		return _node.toString();
	}
	private function set_nodeValue(v:String):String {
		var nodeType = nodeType;
		var x = null;
		if (nodeType == Xml.Element || nodeType == Xml.Document)
			throw "bad nodeType";
		else if (nodeType == Xml.PCData)
			x = createPCData(v);
		else if (nodeType == Xml.CData)
			x = createCData(v);
		else if (nodeType == Xml.Comment)
			x = createComment(v);
		else if (nodeType == Xml.DocType)
			x = createDocType(v);
		else
			x = createProcessingInstruction(v);
		var p = _node.parent();
		if (p != null) {
			p.insertChildAfter(_node, x._node);
			var i = _node.childIndex();
			var children = p.children();
			untyped __delete__(children, Reflect.fields(children)[i]);
		}
		_node = x._node;
		return v;
	}
	private function get_parent():Xml {
		var p = _node.parent();
		return p == null ? null : wrap(p);
	}
	private static function wrap(node:XML, ?type:XmlType):Xml {
		var x = new Xml();
		x._node = node;
		x.nodeType = (type != null) ? type : getNodeType(node);
		return x;
	}
	private function wraps(xList:XMLList):Array<Xml> {
		var out = new Array<Xml>();
		for (i in 0...xList.length())
			out.push(wrap(xList[i]));
		return out;
	}
	function getAttribNS(cur:XML, ns:Array<String>):XMLList {
		var n = cur.namespace(ns[0]);
		if (n == null) {
			var parent = cur.parent();
			if (parent == null) {
				n = new flash.utils.Namespace(ns[0], "@" + ns[0]);
				cur.addNamespace(n);
			} else
				return getAttribNS(parent, ns);
		}
		return _node.attribute(new flash.utils.QName(n, ns[1]));
	}
	public function get(att:String):String {
		if (nodeType != Xml.Element)
			throw "bad nodeType";
		var ns = att.split(":");
		if (ns[0] == "xmlns") {
			var n = _node.namespace((ns[1] == null) ? "" : ns[1]);
			return (n == null) ? null : n.uri;
		}
		if (ns.length == 1) {
			if (!Reflect.hasField(_node, "@" + att))
				return null;
			return Reflect.field(_node, "@" + att);
		}
		var a = getAttribNS(_node, ns);
		return (a.length() == 0) ? null : a.toString();
	}
	public function set(att:String, value:String):Void {
		if (nodeType != Xml.Element)
			throw "bad nodeType";
		var ns = att.split(":");
		if (ns[0] == "xmlns") {
			var n = _node.namespace((ns[1] == null) ? "" : ns[1]);
			if (n != null)
				throw "Can't modify namespace";
			if (ns[1] == null)
				throw "Can't set default namespace";
			_node.addNamespace(new flash.utils.Namespace(ns[1], value));
			return;
		}
		if (ns.length == 1)
			Reflect.setField(_node, "@" + att, value);
		else {
			var a = getAttribNS(_node, ns);
			untyped a[0] = value;
		}
	}
	public function remove(att:String):Void {
		if (nodeType != Xml.Element)
			throw "bad nodeType";
		var ns = att.split(":");
		if (ns.length == 1)
			Reflect.deleteField(_node, "@" + att);
		else
			untyped __delete__(getAttribNS(_node, ns), 0);
	}
	public function exists(att:String):Bool {
		if (nodeType != Xml.Element)
			throw "bad nodeType";
		var ns = att.split(":");
		if (ns[0] == "xmlns")
			return _node.namespace((ns[1] == null) ? "" : ns[1]) != null;
		if (ns.length == 1)
			return Reflect.hasField(_node, "@" + att);
		return getAttribNS(_node, ns).length() > 0;
	}
	public function attributes():Iterator<String> {
		if (nodeType != Xml.Element)
			throw "bad nodeType";
		var attributes:XMLList = _node.attributes();
		var names = Reflect.fields(attributes);
		var cur = 0;
		var nss = _node.namespaceDeclarations();
		return {
			hasNext: function() {
				return cur < names.length + nss.length;
			},
			next: function() {
				if (cur < names.length) {
					return attributes[Std.parseInt(names[cur++])].name();
				} else {
					var ns:flash.utils.Namespace = nss[cur++ - names.length];
					return "xmlns:" + ns.prefix;
				}
			}
		}
	}
	public function iterator():Iterator<Xml> {
		if (nodeType != Xml.Element && nodeType != Xml.Document)
			throw "bad nodeType";
		var children:XMLList = _node.children();
		var wrappers:Array<Xml> = wraps(children);
		var cur = 0;
		return {
			hasNext: function() {
				return cur < wrappers.length;
			},
			next: function() {
				return wrappers[cur++];
			}
		};
	}
	public function elements():Iterator<Xml> {
		if (nodeType != Xml.Element && nodeType != Xml.Document)
			throw "bad nodeType";
		var elements:XMLList = _node.elements();
		var wrappers:Array<Xml> = wraps(elements);
		var cur = 0;
		return {
			hasNext: function() {
				return cur < wrappers.length;
			},
			next: function() {
				return wrappers[cur++];
			}
		};
	}
	public function elementsNamed(name:String):Iterator<Xml> {
		if (nodeType != Xml.Element && nodeType != Xml.Document)
			throw "bad nodeType";
		var ns = name.split(":");
		var elements:XMLList;
		if (ns.length == 1)
			elements = _node.elements(name);
		else
			elements = _node.elements();
		var wrappers:Array<Xml> = wraps(elements);
		if (ns.length != 1)
			for (w in wrappers.copy())
				if (w._node.localName() != ns[1] || w._node.namespace().prefix != ns[0])
					wrappers.remove(w);
		var cur = 0;
		return {
			hasNext: function() {
				return cur < wrappers.length;
			},
			next: function() {
				return wrappers[cur++];
			}
		};
	}
	public function firstChild():Xml {
		if (nodeType != Xml.Element && nodeType != Xml.Document)
			throw "bad nodeType";
		var children:XMLList = _node.children();
		if (children.length() == 0)
			return null;
		return wrap(children[0]);
	}
	public function firstElement():Xml {
		if (nodeType != Xml.Element && nodeType != Xml.Document)
			throw "bad nodeType";
		var elements:XMLList = _node.elements();
		if (elements.length() == 0)
			return null;
		return wrap(elements[0]);
	}
	public function addChild(x:Xml):Void {
		if (nodeType != Xml.Element && nodeType != Xml.Document)
			throw "bad nodeType";
		if (x.parent != null)
			x.parent.removeChild(x);
		var children:XMLList = _node.children();
		_node.appendChild(x._node);
	}
	public function removeChild(x:Xml):Bool {
		if (nodeType != Xml.Element && nodeType != Xml.Document)
			throw "bad nodeType";
		var children:XMLList = _node.children();
		if (_node != x._node.parent())
			return false;
		var i = x._node.childIndex();
		untyped __delete__(children, Reflect.fields(children)[i]);
		return true;
	}
	public function insertChild(x:Xml, pos:Int):Void {
		if (nodeType != Xml.Element && nodeType != Xml.Document)
			throw "bad nodeType";
		if (x.parent != null)
			x.parent.removeChild(x);
		var children:XMLList = _node.children();
		if (pos < children.length())
			_node.insertChildBefore(children[pos], x._node);
		else
			_node.appendChild(x._node);
	}
	public function toString():String {
		XML.prettyPrinting = false;
		if (nodeType == Xml.Document) {
			var str = _node.toXMLString();
			// remove <__document xmlns....>STR</__document> wrapper
			str = str.substr(str.indexOf(">") + 1);
			str = str.substr(0, str.length - 13);
			return str;
		}
		return _node.toXMLString();
	}
	static function __init__():Void
		untyped {
			Element = "element";
			PCData = "pcdata";
			CData = "cdata";
			Comment = "comment";
			DocType = "doctype";
			ProcessingInstruction = "processingInstruction";
			Document = "document";
		}
}
 |