|
@@ -1,5 +1,5 @@
|
|
/*
|
|
/*
|
|
- * Copyright (C)2005-2012 Haxe Foundation
|
|
|
|
|
|
+ * Copyright (C)2005-2015 Haxe Foundation
|
|
*
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
@@ -19,201 +19,298 @@
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
* DEALINGS IN THE SOFTWARE.
|
|
* DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
*/
|
|
-/**
|
|
|
|
- An abstract type representing the type of the Xml
|
|
|
|
- Node. You can compare it to `Xml` statics and can
|
|
|
|
- use `Std.string` to get a string reprensation
|
|
|
|
- of the type.
|
|
|
|
-**/
|
|
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-enum XmlType {
|
|
|
|
|
|
+@:enum abstract XmlType(String) {
|
|
|
|
+ var Element = "element";
|
|
|
|
+ var PCData = "pcdata";
|
|
|
|
+ var CData = "cdata";
|
|
|
|
+ var Comment = "comment";
|
|
|
|
+ var DocType = "doctype";
|
|
|
|
+ var ProcessingInstruction = "processinginstruction";
|
|
|
|
+ var Document = "document";
|
|
}
|
|
}
|
|
|
|
|
|
-/**
|
|
|
|
- The standard Xml class and parsing.
|
|
|
|
- More API to manipulate XML are available in the [haxe.xml] package.
|
|
|
|
-**/
|
|
|
|
|
|
+class Xml {
|
|
|
|
|
|
-extern 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;
|
|
|
|
|
|
- /**
|
|
|
|
- A type of Xml node.
|
|
|
|
- **/
|
|
|
|
- static var Element(default,null) : XmlType;
|
|
|
|
|
|
+ static public function parse( str : String ) : Xml {
|
|
|
|
+ return haxe.xml.Parser.parse(str);
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
- A type of Xml node.
|
|
|
|
|
|
+ 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.
|
|
**/
|
|
**/
|
|
- static var PCData(default,null) : XmlType;
|
|
|
|
|
|
+ public var nodeType(default, null) : XmlType;
|
|
|
|
|
|
/**
|
|
/**
|
|
- A type of Xml node.
|
|
|
|
|
|
+ Returns the node name of an Element.
|
|
**/
|
|
**/
|
|
- static var CData(default,null) : XmlType;
|
|
|
|
|
|
+ @:isVar public var nodeName(get, set) : String;
|
|
|
|
|
|
/**
|
|
/**
|
|
- A type of Xml node.
|
|
|
|
|
|
+ Returns the node value. Only works if the Xml node is not an Element or a Document.
|
|
**/
|
|
**/
|
|
- static var Comment(default,null) : XmlType;
|
|
|
|
|
|
+ @:isVar public var nodeValue(get, set) : String;
|
|
|
|
|
|
/**
|
|
/**
|
|
- A type of Xml node.
|
|
|
|
|
|
+ Returns the parent object in the Xml hierarchy.
|
|
|
|
+ The parent can be [null], an Element or a Document.
|
|
**/
|
|
**/
|
|
- static var DocType(default,null) : XmlType;
|
|
|
|
|
|
+ public var parent(default, null) : Xml;
|
|
|
|
|
|
- /**
|
|
|
|
- A type of Xml node.
|
|
|
|
- **/
|
|
|
|
- static var ProcessingInstruction(default,null) : XmlType;
|
|
|
|
|
|
+ var children:Array<Xml>;
|
|
|
|
+ var attributeMap:Map<String, String>;
|
|
|
|
|
|
- /**
|
|
|
|
- A type of Xml node.
|
|
|
|
- **/
|
|
|
|
- static var Document(default,null) : XmlType;
|
|
|
|
|
|
+ inline function get_nodeName() {
|
|
|
|
+ if (nodeType != Element) {
|
|
|
|
+ throw 'Bad node type, expected Element but found $nodeType';
|
|
|
|
+ }
|
|
|
|
+ return nodeName;
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- Parse a String into an Xml object.
|
|
|
|
- **/
|
|
|
|
- static function parse( str : String ) : Xml;
|
|
|
|
|
|
+ inline function set_nodeName(v) {
|
|
|
|
+ if (nodeType != Element) {
|
|
|
|
+ throw 'Bad node type, expected Element but found $nodeType';
|
|
|
|
+ }
|
|
|
|
+ return this.nodeName = v;
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- Creates a node of the given type.
|
|
|
|
- **/
|
|
|
|
- static function createElement( name : String ) : Xml;
|
|
|
|
|
|
+ inline function get_nodeValue() {
|
|
|
|
+ if (nodeType == Document || nodeType == Element) {
|
|
|
|
+ throw 'Bad node type, unexpected $nodeType';
|
|
|
|
+ }
|
|
|
|
+ return nodeValue;
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
- Creates a node of the given type.
|
|
|
|
- **/
|
|
|
|
- static function createPCData( data : String ) : Xml;
|
|
|
|
|
|
+ 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.
|
|
Creates a node of the given type.
|
|
**/
|
|
**/
|
|
- static function createCData( data : String ) : Xml;
|
|
|
|
|
|
+ static public function createElement( name : String ) : Xml {
|
|
|
|
+ var xml = new Xml(Element);
|
|
|
|
+ xml.nodeName = name;
|
|
|
|
+ return xml;
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
Creates a node of the given type.
|
|
Creates a node of the given type.
|
|
**/
|
|
**/
|
|
- static function createComment( data : String ) : Xml;
|
|
|
|
|
|
+ static public function createPCData( data : String ) : Xml {
|
|
|
|
+ var xml = new Xml(PCData);
|
|
|
|
+ xml.nodeValue = data;
|
|
|
|
+ return xml;
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
Creates a node of the given type.
|
|
Creates a node of the given type.
|
|
**/
|
|
**/
|
|
- static function createDocType( data : String ) : Xml;
|
|
|
|
|
|
+ static public function createCData( data : String ) : Xml {
|
|
|
|
+ var xml = new Xml(CData);
|
|
|
|
+ xml.nodeValue = data;
|
|
|
|
+ return xml;
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
Creates a node of the given type.
|
|
Creates a node of the given type.
|
|
**/
|
|
**/
|
|
- static function createProcessingInstruction( data : String ) : Xml;
|
|
|
|
|
|
+ static public function createComment( data : String ) : Xml {
|
|
|
|
+ var xml = new Xml(Comment);
|
|
|
|
+ xml.nodeValue = data;
|
|
|
|
+ return xml;
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
Creates a node of the given type.
|
|
Creates a node of the given type.
|
|
**/
|
|
**/
|
|
- static function createDocument() : Xml;
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- 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.
|
|
|
|
- **/
|
|
|
|
- var nodeType(default,null) : XmlType;
|
|
|
|
|
|
+ static public function createDocType( data : String ) : Xml {
|
|
|
|
+ var xml = new Xml(DocType);
|
|
|
|
+ xml.nodeValue = data;
|
|
|
|
+ return xml;
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
- Returns the node name of an Element.
|
|
|
|
|
|
+ Creates a node of the given type.
|
|
**/
|
|
**/
|
|
- var nodeName(get,set) : String;
|
|
|
|
|
|
+ static public function createProcessingInstruction( data : String ) : Xml {
|
|
|
|
+ var xml = new Xml(ProcessingInstruction);
|
|
|
|
+ xml.nodeValue = data;
|
|
|
|
+ return xml;
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
- Returns the node value. Only works if the Xml node is not an Element or a Document.
|
|
|
|
|
|
+ Creates a node of the given type.
|
|
**/
|
|
**/
|
|
- var nodeValue(get,set) : String;
|
|
|
|
|
|
+ static public function createDocument() : Xml {
|
|
|
|
+ return new Xml(Document);
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
Get the given attribute of an Element node. Returns [null] if not found.
|
|
Get the given attribute of an Element node. Returns [null] if not found.
|
|
Attributes are case-sensitive.
|
|
Attributes are case-sensitive.
|
|
**/
|
|
**/
|
|
- function get( att : String ) : String; // check case insensitivy
|
|
|
|
|
|
+ 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.
|
|
Set the given attribute value for an Element node.
|
|
Attributes are case-sensitive.
|
|
Attributes are case-sensitive.
|
|
**/
|
|
**/
|
|
- function set( att : String, value : String ) : Void;
|
|
|
|
|
|
+ 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.
|
|
Removes an attribute for an Element node.
|
|
Attributes are case-sensitive.
|
|
Attributes are case-sensitive.
|
|
**/
|
|
**/
|
|
- function remove( att : String ) : Void;
|
|
|
|
|
|
+ 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.
|
|
Tells if the Element node has a given attribute.
|
|
Attributes are case-sensitive.
|
|
Attributes are case-sensitive.
|
|
**/
|
|
**/
|
|
- function exists( att : String ) : Bool;
|
|
|
|
|
|
+ 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.
|
|
Returns an [Iterator] on all the attribute names.
|
|
**/
|
|
**/
|
|
- function attributes() : Iterator<String>;
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- Returns the parent object in the Xml hierarchy.
|
|
|
|
- The parent can be [null], an Element or a Document.
|
|
|
|
- **/
|
|
|
|
- var parent(get,null) : Xml;
|
|
|
|
|
|
+ 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.
|
|
Returns an iterator of all child nodes.
|
|
Only works if the current node is an Element or a Document.
|
|
Only works if the current node is an Element or a Document.
|
|
**/
|
|
**/
|
|
- function iterator() : Iterator<Xml>;
|
|
|
|
|
|
+ public inline function iterator() : Iterator<Xml> {
|
|
|
|
+ ensureElementType();
|
|
|
|
+ return children.iterator();
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
Returns an iterator of all child nodes which are Elements.
|
|
Returns an iterator of all child nodes which are Elements.
|
|
Only works if the current node is an Element or a Document.
|
|
Only works if the current node is an Element or a Document.
|
|
**/
|
|
**/
|
|
- function elements() : Iterator<Xml>;
|
|
|
|
|
|
+ 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.
|
|
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.
|
|
Only works if the current node is an Element or a Document.
|
|
**/
|
|
**/
|
|
- function elementsNamed( name : String ) : Iterator<Xml>;
|
|
|
|
|
|
+ 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.
|
|
Returns the first child node.
|
|
**/
|
|
**/
|
|
- function firstChild() : Xml;
|
|
|
|
|
|
+ public inline function firstChild() : Xml {
|
|
|
|
+ ensureElementType();
|
|
|
|
+ return children[0];
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
Returns the first child node which is an Element.
|
|
Returns the first child node which is an Element.
|
|
**/
|
|
**/
|
|
- function firstElement() : Xml;
|
|
|
|
-
|
|
|
|
|
|
+ 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.
|
|
Adds a child node to the Document or Element.
|
|
One node can only be inside one given node which is indicated by the [parent] property.
|
|
One node can only be inside one given node which is indicated by the [parent] property.
|
|
**/
|
|
**/
|
|
- function addChild( x : Xml ) : Void;
|
|
|
|
|
|
+ public function addChild( x : Xml ) : Void {
|
|
|
|
+ ensureElementType();
|
|
|
|
+ if (x.parent == this) {
|
|
|
|
+ return;
|
|
|
|
+ } else if (x.parent != null) {
|
|
|
|
+ x.parent.removeChild(x);
|
|
|
|
+ }
|
|
|
|
+ children.push(x);
|
|
|
|
+ x.parent = this;
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
Removes a child from the Document or Element.
|
|
Removes a child from the Document or Element.
|
|
Returns true if the child was successfuly removed.
|
|
Returns true if the child was successfuly removed.
|
|
**/
|
|
**/
|
|
- function removeChild( x : Xml ) : Bool;
|
|
|
|
|
|
+ public function removeChild( x : Xml ) : Bool {
|
|
|
|
+ ensureElementType();
|
|
|
|
+ return children.remove(x);
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
Inserts a child at the given position among the other childs.
|
|
Inserts a child at the given position among the other childs.
|
|
**/
|
|
**/
|
|
- function insertChild( x : Xml, pos : Int ) : Void;
|
|
|
|
|
|
+ public function insertChild( x : Xml, pos : Int ) : Void {
|
|
|
|
+ ensureElementType();
|
|
|
|
+ children.insert(pos, x);
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
Returns a String representation of the Xml node.
|
|
Returns a String representation of the Xml node.
|
|
**/
|
|
**/
|
|
- function toString() : String;
|
|
|
|
-
|
|
|
|
|
|
+ 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';
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|