1 2 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 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
/*
* 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 cpp;
enum abstract XmlType(Int) {
/**
Represents an XML element type.
**/
var Element = 0;
/**
Represents XML parsed character data type.
**/
var PCData = 1;
/**
Represents XML character data type.
**/
var CData = 2;
/**
Represents an XML comment type.
**/
var Comment = 3;
/**
Represents an XML doctype element type.
**/
var DocType = 4;
/**
Represents an XML processing instruction type.
**/
var ProcessingInstruction = 5;
/**
Represents an XML document type.
**/
var Document = 6;
}
class NativeXmlState {
var cur:Xml;
public function new(x:Xml) {
x._children = new Array<Xml>();
cur = x;
}
@:keep
public function xml(name:String, att:Dynamic<String>) {
var x = new Xml();
x._parent = cur;
x.nodeType = Xml.Element;
x._nodeName = name;
x._attributes = att;
x._children = new Array<Xml>();
cur.addChild(x);
cur = x;
}
@:keep
public function cdata(text:String) {
var x = new Xml();
x._parent = cur;
x.nodeType = Xml.CData;
x._nodeValue = text;
cur.addChild(x);
}
@:keep
public function pcdata(text:String) {
var x = new Xml();
x._parent = cur;
x.nodeType = Xml.PCData;
x._nodeValue = text;
cur.addChild(x);
}
@:keep
public function comment(text:String) {
var x = new Xml();
x._parent = cur;
if (text.length > 1 && StringTools.fastCodeAt(text, 0) == 63) {
x.nodeType = Xml.ProcessingInstruction;
text = text.substr(1, text.length - 2);
} else {
x.nodeType = Xml.Comment;
}
x._nodeValue = text;
cur.addChild(x);
}
@:keep
public function doctype(text:String) {
var x = new Xml();
x._parent = cur;
x.nodeType = Xml.DocType;
x._nodeValue = text.substr(1);
cur.addChild(x);
}
@:keep
public function done() {
cur = cur._parent;
}
}
private class NativeXmlIterator {
var cur = 0;
var children:Array<Xml>;
public function new(inChildren:Array<Xml>) {
children = inChildren;
cur = 0;
}
public function hasNext():Bool {
var k = cur;
var l = children.length;
while (k < l) {
if (children[k].nodeType == Xml.Element)
break;
k += 1;
}
cur = k;
return k < l;
}
public function next():Xml {
var k = cur;
var l = children.length;
while (k < l) {
var n = children[k];
k += 1;
if (n.nodeType == Xml.Element) {
cur = k;
return n;
}
}
return null;
}
}
private class NativeXmlNamedIterator {
var cur = 0;
var children:Array<Xml>;
var name:String;
public function new(inChildren:Array<Xml>, inName:String) {
children = inChildren;
name = inName;
cur = 0;
}
public function hasNext():Bool {
var k = cur;
var l = children.length;
while (k < l) {
var n = children[k];
if (n.nodeType == Xml.Element && n._nodeName == name)
break;
k++;
}
cur = k;
return k < l;
}
public function next():Xml {
var k = cur;
var l = children.length;
while (k < l) {
var n = children[k];
k++;
if (n.nodeType == Xml.Element && n._nodeName == name) {
cur = k;
return n;
}
}
return null;
}
}
@:cppInclude("./NativeXmlImport.cpp")
@:allow(cpp.NativeXmlState) @:allow(cpp.NativeXmlIterator) @:allow(cpp.NativeXmlNamedIterator)
class Xml {
static inline var Element = XmlType.Element;
static inline var PCData = XmlType.PCData;
static inline var CData = XmlType.CData;
static inline var Comment = XmlType.Comment;
static inline var DocType = XmlType.DocType;
static inline var ProcessingInstruction = XmlType.ProcessingInstruction;
static inline var Document = XmlType.Document;
private var _nodeName:String;
private var _nodeValue:String;
private var _attributes:Dynamic<String>;
private var _children:Array<Xml>;
private var _parent:Xml;
function new():Void {}
@:native("parse_xml")
extern static function parse_xml(str:String, state:NativeXmlState);
public static function parse(str:String):Xml {
var x = new Xml();
var state = new NativeXmlState(x);
parse_xml(str, state);
x.nodeType = Xml.Document;
return x;
}
public static function createElement(name:String):Xml {
var r = new Xml();
r.nodeType = Xml.Element;
r._nodeName = name;
r._attributes = null;
r._children = new Array();
return r;
}
public static function createPCData(data:String):Xml {
var r = new Xml();
r.nodeType = Xml.PCData;
r._nodeValue = data;
return r;
}
public static function createCData(data:String):Xml {
var r = new Xml();
r.nodeType = Xml.CData;
r._nodeValue = data;
return r;
}
public static function createComment(data:String):Xml {
var r = new Xml();
r.nodeType = Xml.Comment;
r._nodeValue = data;
return r;
}
public static function createDocType(data:String):Xml {
var r = new Xml();
r.nodeType = Xml.DocType;
r._nodeValue = data;
return r;
}
public static function createProcessingInstruction(data:String):Xml {
var r = new Xml();
r.nodeType = Xml.ProcessingInstruction;
r._nodeValue = data;
return r;
}
public static function createDocument():Xml {
var r = new Xml();
r.nodeType = Xml.Document;
r._children = new Array();
return r;
}
public var nodeType(default, null):XmlType;
public var nodeName(get, set):String;
public var nodeValue(get, set):String;
private function get_nodeName():String {
if (nodeType != Xml.Element)
throw "bad nodeType";
return _nodeName;
}
private function set_nodeName(n:String):String {
if (nodeType != Xml.Element)
throw "bad nodeType";
return _nodeName = n;
}
private function get_nodeValue():String {
if (nodeType == Xml.Element || nodeType == Xml.Document)
throw "bad nodeType";
return _nodeValue;
}
private function set_nodeValue(v:String):String {
if (nodeType == Xml.Element || nodeType == Xml.Document)
throw "bad nodeType";
return _nodeValue = v;
}
public var parent(get, null):Xml;
private function get_parent():Xml {
return _parent;
}
public function get(att:String):String {
if (nodeType != Xml.Element)
throw "bad nodeType";
return Reflect.field(_attributes, att);
}
public function set(att:String, value:String):Void {
if (nodeType != Xml.Element)
throw "bad nodeType";
if (_attributes == null)
_attributes = {};
Reflect.setField(_attributes, att, value);
return;
}
public function remove(att:String):Void {
if (nodeType != Xml.Element)
throw "bad nodeType";
Reflect.deleteField(_attributes, att);
return;
}
public function exists(att:String):Bool {
if (nodeType != Xml.Element)
throw "bad nodeType";
return Reflect.hasField(_attributes, att);
}
public function attributes():Iterator<String> {
if (nodeType != Xml.Element)
throw "bad nodeType";
return Reflect.fields(_attributes).iterator();
}
public function iterator():Iterator<Xml> {
if (_children == null)
throw "bad nodetype";
return untyped _children.iterator();
}
public function elements():Iterator<Xml> {
if (_children == null)
throw "bad nodetype";
return new NativeXmlIterator(_children);
}
public function elementsNamed(name:String):Iterator<Xml> {
if (_children == null)
throw "bad nodetype";
return new NativeXmlNamedIterator(_children, name);
}
public function firstChild():Xml {
if (_children == null)
throw "bad nodetype";
return _children[0];
}
public function firstElement():Xml {
if (_children == null)
throw "bad nodetype";
for (cur in 0..._children.length) {
var n:Xml = _children[cur];
if (n.nodeType == Xml.Element)
return n;
}
return null;
}
public function addChild(x:Xml):Void {
if (_children == null)
throw "bad nodetype";
if (x._parent != null)
x._parent._children.remove(x);
x._parent = this;
_children.push(x);
return;
}
public function removeChild(x:Xml):Bool {
if (_children == null)
throw "bad nodetype";
var b = _children.remove(x);
if (b)
x._parent = null;
return b;
}
public function insertChild(x:Xml, pos:Int):Void {
if (_children == null)
throw "bad nodetype";
if (x._parent != null)
x._parent._children.remove(x);
x._parent = this;
_children.insert(pos, x);
return;
}
public function toString():String {
var s = new StringBuf();
toStringRec(s);
return s.toString();
}
private function toStringRec(s:StringBuf):Void {
switch (nodeType) {
case Xml.Document:
for (x in _children)
x.toStringRec(s);
case Xml.Element:
s.addChar("<".code);
s.add(_nodeName);
for (k in Reflect.fields(_attributes)) {
s.addChar(" ".code);
s.add(k);
s.addChar("=".code);
s.addChar("\"".code);
s.add(Reflect.field(_attributes, k));
s.addChar("\"".code);
}
if (_children.length == 0) {
s.addChar("/".code);
s.addChar(">".code);
return;
}
s.addChar(">".code);
for (x in _children)
x.toStringRec(s);
s.addChar("<".code);
s.addChar("/".code);
s.add(_nodeName);
s.addChar(">".code);
case Xml.PCData:
s.add(StringTools.htmlEscape(_nodeValue));
case Xml.CData:
s.add("<![CDATA[");
s.add(_nodeValue);
s.add("]]>");
case Xml.Comment:
s.add("<!--");
s.add(_nodeValue);
s.add("-->");
case Xml.DocType:
s.add("<!DOCTYPE ");
s.add(_nodeValue);
s.add(">");
case Xml.ProcessingInstruction:
s.add("<?");
s.add(_nodeValue);
s.add("?>");
}
}
}
|