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
|
/*
* Copyright (C)2005-2015 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.
*/
@:enum abstract XmlType(Int) {
var Element = 0;
var PCData = 1;
var CData = 2;
var Comment = 3;
var DocType = 4;
var ProcessingInstruction = 5;
var Document = 6;
}
class Xml {
static public var Element(default,null) = XmlType.Element;
static public var PCData(default,null) = XmlType.PCData;
static public var CData(default,null) = XmlType.CData;
static public var Comment(default,null) = XmlType.Comment;
static public var DocType(default,null) = XmlType.DocType;
static public var ProcessingInstruction(default,null) = XmlType.ProcessingInstruction;
static public var Document(default,null) = XmlType.Document;
static public function parse( str : String ) : Xml {
return haxe.xml.Parser.parse(str);
}
/**
Returns the type of the Xml Node. This should be used before
accessing other functions since some might raise an exception
if the node type is not correct.
**/
public var nodeType(default, null) : XmlType;
/**
Returns the node name of an Element.
**/
@:isVar public var nodeName(get, set) : String;
/**
Returns the node value. Only works if the Xml node is not an Element or a Document.
**/
@:isVar public var nodeValue(get, set) : String;
/**
Returns the parent object in the Xml hierarchy.
The parent can be [null], an Element or a Document.
**/
public var parent(default, null) : Xml;
var children:Array<Xml>;
var attributeMap:Map<String, String>;
inline function get_nodeName() {
if (nodeType != Element) {
throw 'Bad node type, expected Element but found $nodeType';
}
return nodeName;
}
inline function set_nodeName(v) {
if (nodeType != Element) {
throw 'Bad node type, expected Element but found $nodeType';
}
return this.nodeName = v;
}
inline function get_nodeValue() {
if (nodeType == Document || nodeType == Element) {
throw 'Bad node type, unexpected $nodeType';
}
return nodeValue;
}
inline function set_nodeValue(v) {
if (nodeType == Document || nodeType == Element) {
throw 'Bad node type, unexpected $nodeType';
}
return this.nodeValue = v;
}
/**
Creates a node of the given type.
**/
static public function createElement( name : String ) : Xml {
var xml = new Xml(Element);
xml.nodeName = name;
return xml;
}
/**
Creates a node of the given type.
**/
static public function createPCData( data : String ) : Xml {
var xml = new Xml(PCData);
xml.nodeValue = data;
return xml;
}
/**
Creates a node of the given type.
**/
static public function createCData( data : String ) : Xml {
var xml = new Xml(CData);
xml.nodeValue = data;
return xml;
}
/**
Creates a node of the given type.
**/
static public function createComment( data : String ) : Xml {
var xml = new Xml(Comment);
xml.nodeValue = data;
return xml;
}
/**
Creates a node of the given type.
**/
static public function createDocType( data : String ) : Xml {
var xml = new Xml(DocType);
xml.nodeValue = data;
return xml;
}
/**
Creates a node of the given type.
**/
static public function createProcessingInstruction( data : String ) : Xml {
var xml = new Xml(ProcessingInstruction);
xml.nodeValue = data;
return xml;
}
/**
Creates a node of the given type.
**/
static public function createDocument() : Xml {
return new Xml(Document);
}
/**
Get the given attribute of an Element node. Returns [null] if not found.
Attributes are case-sensitive.
**/
public function get( att : String ) : String {
if (nodeType != Element) {
throw 'Bad node type, expected Element but found $nodeType';
}
return attributeMap[att];
}
/**
Set the given attribute value for an Element node.
Attributes are case-sensitive.
**/
public function set( att : String, value : String ) : Void {
if (nodeType != Element) {
throw 'Bad node type, expected Element but found $nodeType';
}
attributeMap.set(att, value);
}
/**
Removes an attribute for an Element node.
Attributes are case-sensitive.
**/
public function remove( att : String ) : Void {
if (nodeType != Element) {
throw 'Bad node type, expected Element but found $nodeType';
}
attributeMap.remove(att);
}
/**
Tells if the Element node has a given attribute.
Attributes are case-sensitive.
**/
public function exists( att : String ) : Bool {
if (nodeType != Element) {
throw 'Bad node type, expected Element but found $nodeType';
}
return attributeMap.exists(att);
}
/**
Returns an [Iterator] on all the attribute names.
**/
public function attributes() : Iterator<String> {
if (nodeType != Element) {
throw 'Bad node type, expected Element but found $nodeType';
}
return attributeMap.keys();
}
/**
Returns an iterator of all child nodes.
Only works if the current node is an Element or a Document.
**/
public inline function iterator() : Iterator<Xml> {
ensureElementType();
return children.iterator();
}
/**
Returns an iterator of all child nodes which are Elements.
Only works if the current node is an Element or a Document.
**/
public function elements() : Iterator<Xml> {
ensureElementType();
var ret = [for (child in children) if (child.nodeType == Element) child];
return ret.iterator();
}
/**
Returns an iterator of all child nodes which are Elements with the given nodeName.
Only works if the current node is an Element or a Document.
**/
public function elementsNamed( name : String ) : Iterator<Xml> {
ensureElementType();
var ret = [for (child in children) if (child.nodeType == Element && child.nodeName == name) child];
return ret.iterator();
}
/**
Returns the first child node.
**/
public inline function firstChild() : Xml {
ensureElementType();
return children[0];
}
/**
Returns the first child node which is an Element.
**/
public function firstElement() : Xml {
ensureElementType();
for (child in children) {
if (child.nodeType == Element) {
return child;
}
}
return null;
}
/**
Adds a child node to the Document or Element.
A child node can only be inside one given parent node, which is indicated by the [parent] property.
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.
If the child node was previously inside a different node, it will be moved to this Document or Element.
**/
public function addChild( x : Xml ) : Void {
ensureElementType();
if (x.parent != null) {
x.parent.removeChild(x);
}
children.push(x);
x.parent = this;
}
/**
Removes a child from the Document or Element.
Returns true if the child was successfuly removed.
**/
public function removeChild( x : Xml ) : Bool {
ensureElementType();
if (children.remove(x)) {
x.parent = null;
return true;
}
return false;
}
/**
Inserts a child at the given position among the other childs.
A child node can only be inside one given parent node, which is indicated by the [parent] property.
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.
If the child node was previously inside a different node, it will be moved to this Document or Element.
**/
public function insertChild( x : Xml, pos : Int ) : Void {
ensureElementType();
if (x.parent != null) {
x.parent.children.remove(x);
}
children.insert(pos, x);
x.parent = this;
}
/**
Returns a String representation of the Xml node.
**/
public inline function toString() : String {
return haxe.xml.Printer.print(this);
}
function new(nodeType:XmlType) {
this.nodeType = nodeType;
children = [];
attributeMap = new Map();
}
inline function ensureElementType() {
if (nodeType != Document && nodeType != Element) {
throw 'Bad node type, expected Element or Document but found $nodeType';
}
}
}
|