123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- /*
- * Copyright (c) 2005, The haXe Project Contributors
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- */
- enum XmlType {
- Element;
- PCData;
- CData;
- Comment;
- DocType;
- Prolog;
- Document;
- }
- @:core_api 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 Prolog(default,null) : XmlType;
- public static var Document(default,null) : XmlType;
- public var nodeType(default,null) : XmlType;
- public var nodeName(getNodeName,setNodeName) : String;
- public var nodeValue(getNodeValue,setNodeValue) : String;
- public var parent(getParent,null) : Xml;
- var _nodeName : String;
- var _nodeValue : String;
- var _attributes : Hash<String>;
- var _children : Array<Xml>;
- var _parent : Xml;
- public static function parse( str : String ) : Xml {
- return haxe.xml.Parser.parse(str);
- }
- private function new() : Void {
- }
- public static function createElement( name : String ) : Xml {
- var r = new Xml();
- r.nodeType = Xml.Element;
- r._children = new Array();
- r._attributes = new Hash();
- r.setNodeName( name );
- return r;
- }
- public static function createPCData( data : String ) : Xml {
- var r = new Xml();
- r.nodeType = Xml.PCData;
- r.setNodeValue( data );
- return r;
- }
- public static function createCData( data : String ) : Xml {
- var r = new Xml();
- r.nodeType = Xml.CData;
- r.setNodeValue( data );
- return r;
- }
- public static function createComment( data : String ) : Xml {
- var r = new Xml();
- r.nodeType = Xml.Comment;
- r.setNodeValue( data );
- return r;
- }
- public static function createDocType( data : String ) : Xml {
- var r = new Xml();
- r.nodeType = Xml.DocType;
- r.setNodeValue( data );
- return r;
- }
- public static function createProlog( data : String ) : Xml {
- var r = new Xml();
- r.nodeType = Xml.Prolog;
- r.setNodeValue( data );
- return r;
- }
- public static function createDocument() : Xml {
- var r = new Xml();
- r.nodeType = Xml.Document;
- r._children = new Array();
- return r;
- }
- private function getNodeName() : String {
- if( nodeType != Xml.Element )
- throw "bad nodeType";
- return _nodeName;
- }
- private function setNodeName( n : String ) : String {
- if( nodeType != Xml.Element )
- throw "bad nodeType";
- return _nodeName = n;
- }
- private function getNodeValue() : String {
- if( nodeType == Xml.Element || nodeType == Xml.Document )
- throw "bad nodeType";
- return _nodeValue;
- }
- private function setNodeValue( v : String ) : String {
- if( nodeType == Xml.Element || nodeType == Xml.Document )
- throw "bad nodeType";
- return _nodeValue = v;
- }
- private function getParent() : Xml {
- return _parent;
- }
- public function get( att : String ) : String {
- if( nodeType != Xml.Element )
- throw "bad nodeType";
- return _attributes.get( att );
- }
- public function set( att : String, value : String ) : Void {
- if( nodeType != Xml.Element )
- throw "bad nodeType";
- _attributes.set( att, value );
- }
- public function remove( att : String ) : Void{
- if( nodeType != Xml.Element )
- throw "bad nodeType";
- _attributes.remove( att );
- }
- public function exists( att : String ) : Bool {
- if( nodeType != Xml.Element )
- throw "bad nodeType";
- return _attributes.exists( att );
- }
- public function attributes() : Iterator<String> {
- if( nodeType != Xml.Element )
- throw "bad nodeType";
- return _attributes.keys();
- }
- public function iterator() : Iterator<Xml> {
- if ( _children == null ) throw "bad nodetype";
- var cur = 0;
- var x = this._children;
- return {
- hasNext : function(){
- return cur < x.length;
- },
- next : function(){
- return x[cur++];
- }
- }
- }
- public function elements() : Iterator<Xml> {
- if ( _children == null ) throw "bad nodetype";
- var cur = 0;
- var x = _children;
- return untyped {
- hasNext : function() {
- var k = cur;
- var l = x.length;
- while( k < l ) {
- if( x[k].nodeType == Xml.Element )
- break;
- k += 1;
- }
- cur = k;
- return k < l;
- },
- next : function() {
- var k = cur;
- var l = x.length;
- while( k < l ) {
- var n = x[k];
- k += 1;
- if( n.nodeType == Xml.Element ) {
- cur = k;
- return n;
- }
- }
- return null;
- }
- }
- }
- public function elementsNamed( name : String ) : Iterator<Xml> {
- if ( _children == null ) throw "bad nodetype";
- var x = _children;
- var cur = 0;
- return untyped {
- cur: 0,
- x: this._children,
- hasNext : function() {
- var k = cur;
- var l = x.length;
- while( k < l ) {
- var n = x[k];
- if( n.nodeType == Xml.Element && n._nodeName == name )
- break;
- k++;
- }
- cur = k;
- return k < l;
- },
- next : function() {
- var k = cur;
- var l = x.length;
- while( k < l ) {
- var n = x[k];
- k++;
- if( n.nodeType == Xml.Element && n._nodeName == name ) {
- cur = k;
- return n;
- }
- }
- return null;
- }
- }
- }
- public function firstChild() : Xml {
- if( _children == null ) throw "bad nodetype";
- return _children[0];
- }
- public function firstElement() : Xml {
- if( _children == null ) throw "bad nodetype";
- var cur = 0;
- var l = _children.length;
- while( cur < l ) {
- var n = _children[cur];
- if( n.nodeType == Xml.Element )
- return n;
- cur++;
- }
- 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 );
- }
- 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 );
- }
- public function toString() : String {
- if( nodeType == Xml.PCData )
- return _nodeValue;
- if( nodeType == Xml.CData )
- return "<![CDATA["+_nodeValue+"]]>";
- if( nodeType == Xml.Comment )
- return "<!--"+_nodeValue+"-->";
- if( nodeType == Xml.DocType )
- return "<!DOCTYPE "+_nodeValue+">";
- if( nodeType == Xml.Prolog )
- return "<?"+_nodeValue+"?>";
- var s = new StringBuf();
- if( nodeType == Xml.Element ) {
- s.add("<");
- s.add(_nodeName);
- for( k in _attributes.keys() ){
- s.add(" ");
- s.add(k);
- s.add("=\"");
- s.add(_attributes.get(k));
- s.add("\"");
- }
- if( _children.length == 0 ) {
- s.add("/>");
- return s.toString();
- }
- s.add(">");
- }
- for( x in iterator() )
- s.add(x.toString());
- if( nodeType == Xml.Element ) {
- s.add("</");
- s.add(_nodeName);
- s.add(">");
- }
- return s.toString();
- }
- static function __init__() : Void untyped {
- Xml.Element = XmlType.Element;
- Xml.PCData = XmlType.PCData;
- Xml.CData = XmlType.CData;
- Xml.Comment = XmlType.Comment;
- Xml.DocType = XmlType.DocType;
- Xml.Prolog = XmlType.Prolog;
- Xml.Document = XmlType.Document;
- }
- }
|