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
|
/*
* Copyright (C)2005-2012 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 php;
import php.Lib;
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 _nodeName : String;
var _nodeValue : String;
var _attributes : haxe.ds.StringMap<String>;
var _children : Array<Xml>;
var _parent : Xml;
var _fromCustomParser:Bool;
private static var build : Xml;
private static function __start_element_handler(parser : Dynamic, name : String, attribs : ArrayAccess<String>) : Void {
var node = createElement(name);
untyped __php__("foreach($attribs as $k => $v) $node->set($k, $v)");
build.addChild(node);
build = node;
}
private static function __end_element_handler(parser : Dynamic, name : String) : Void {
build = build.parent;
}
private static function __decodeattr(value : String) : String
{
return untyped __call__("str_replace", "'", ''', __call__("htmlspecialchars", value, __php__('ENT_COMPAT'), 'UTF-8'));
}
private static function __decodeent(value : String) : String
{
return untyped __call__("str_replace", "'", ''', __call__("htmlentities", value, __php__('ENT_COMPAT'), 'UTF-8'));
}
private static function __character_data_handler(parser : Dynamic, data : String) : Void {
var d = __decodeent(data);
if ((untyped __call__("strlen", data) == 1 && d != data) || d == data) {
var last = build._children[build._children.length - 1];
if (null != last && last.nodeType == Xml.PCData)
{
last.nodeValue += d;
} else
build.addChild(createPCData(d));
} else {
build.addChild(createCData(data));
}
}
private static function __default_handler(parser : Dynamic, data : String) : Void {
//On some PHP setups (seems to happen when libexpat is used) we may get called for such "entities" although character_data will correctly be called afterward.
if(data == "<![CDATA[")
return;
if(data == "]]>")
return;
if ("<!--" == data.substr(0, 4))
build.addChild(createComment(data.substr(4, data.length-7)));
else
build.addChild(createPCData(data));
}
static var reHeader = ~/\s*(?:<\?(.+?)\?>)?(?:<!DOCTYPE ([^>]+)>)?/mi;
public static function parse( str : String ) : Xml {
build = createDocument();
var xml_parser = untyped __call__("xml_parser_create");
untyped __call__("xml_set_element_handler", xml_parser, __start_element_handler, __end_element_handler);
untyped __call__("xml_set_character_data_handler", xml_parser, __character_data_handler);
untyped __call__("xml_set_default_handler", xml_parser, __default_handler);
untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_CASE_FOLDING"), 0);
untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_SKIP_WHITE"), 0);
reHeader.match(str);
str = "<doc>"+reHeader.matchedRight()+"</doc>";
if(1 != untyped __call__("xml_parse", xml_parser, str, true)) {
throw "Xml parse error ("+untyped __call__("xml_error_string", __call__("xml_get_error_code", xml_parser)) + ") line #" + __call__("xml_get_current_line_number", xml_parser);
}
untyped __call__("xml_parser_free", xml_parser);
build = build._children[0];
build._parent = null;
build._nodeName = null;
build.nodeType = Document;
var doctype = reHeader.matched(2);
if (null != doctype)
build.insertChild(createDocType(doctype), 0);
var ProcessingInstruction = reHeader.matched(1);
if (null != ProcessingInstruction)
build.insertChild(createProcessingInstruction(ProcessingInstruction), 0);
return build;
}
private function new(fromCustomParser:Bool = false) : Void {
_fromCustomParser = fromCustomParser;
}
@:allow(haxe.xml.Parser)
static function createPCDataFromCustomParser( data : String ) : Xml {
var r = new Xml(true);
r.nodeType = Xml.PCData;
r.set_nodeValue( data );
return r;
}
public static function createElement( name : String ) : Xml {
var r = new Xml();
r.nodeType = Xml.Element;
r._children = new Array();
r._attributes = new haxe.ds.StringMap();
r.set_nodeName( name );
return r;
}
public static function createPCData( data : String ) : Xml {
var r = new Xml();
r.nodeType = Xml.PCData;
r.set_nodeValue( data );
return r;
}
public static function createCData( data : String ) : Xml {
var r = new Xml();
r.nodeType = Xml.CData;
r.set_nodeValue( data );
return r;
}
public static function createComment( data : String ) : Xml {
var r = new Xml();
r.nodeType = Xml.Comment;
r.set_nodeValue( data );
return r;
}
public static function createDocType( data : String ) : Xml {
var r = new Xml();
r.nodeType = Xml.DocType;
r.set_nodeValue( data );
return r;
}
public static function createProcessingInstruction( data : String ) : Xml {
var r = new Xml();
r.nodeType = Xml.ProcessingInstruction;
r.set_nodeValue( data );
return r;
}
public static function createDocument() : Xml {
var r = new Xml();
r.nodeType = Xml.Document;
r._children = new Array();
return r;
}
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;
}
private inline function get_parent() : Xml {
return _parent;
}
public function get( att : String ) : String {
if( nodeType != Xml.Element )
throw "bad nodeType";
return _attributes.get( att );
}
// TODO: check correct transform function
@:ifFeature("Xml.parse")
public function set( att : String, value : String ) : Void {
if( nodeType != Xml.Element )
throw "bad nodeType";
_attributes.set( att, __decodeattr(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";
return _children.iterator();
}
public function elements() : Iterator<Xml> {
if( _children == null ) throw "bad nodetype";
return Lambda.filter(_children, function(child) return child.nodeType == Xml.Element).iterator();
}
public function elementsNamed( name : String ) : Iterator<Xml> {
if( _children == null ) throw "bad nodetype";
return Lambda.filter(_children, function(child) return child.nodeType == Xml.Element && child.nodeName == name).iterator();
}
public function firstChild() : Xml {
if( _children == null ) throw "bad nodetype";
if( _children.length == 0 ) return null;
return _children[0];
}
public function firstElement() : Xml {
if( _children == null ) throw "bad nodetype";
for (child in _children)
if (child.nodeType == Xml.Element)
return child;
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 _fromCustomParser ? StringTools.htmlEscape(_nodeValue) : _nodeValue;
var s = "";
if( nodeType == Xml.Element ) {
s += "<";
s += _nodeName;
for( k in _attributes.keys() ){
s += " ";
s += k;
s += "=\""; // \"
s += _attributes.get(k);
s += "\""; // \"
}
if( _children.length == 0 ) {
s += "/>";
return s;
}
s += ">";
} else if( nodeType == Xml.CData )
return "<![CDATA["+_nodeValue+"]]>";
else if( nodeType == Xml.Comment )
return "<!--"+_nodeValue+"-->";
else if( nodeType == Xml.DocType )
return "<!DOCTYPE "+_nodeValue+">";
else if ( nodeType == Xml.ProcessingInstruction )
return "<?"+_nodeValue+"?>";
for( x in iterator() )
s += x.toString();
if( nodeType == Xml.Element ) {
s += "</";
s += _nodeName;
s += ">";
}
return s;
}
static function __init__() : Void untyped {
Xml.Element = "element";
Xml.PCData = "pcdata";
Xml.CData = "cdata";
Xml.Comment = "comment";
Xml.DocType = "doctype";
Xml.ProcessingInstruction = "processingInstruction";
Xml.Document = "document";
}
}
|