Files
@ 7ec91347b83f
Branch filter:
Location: hot67beta/libraries/joomla/utilities/simplexml.php
7ec91347b83f
15.2 KiB
text/x-php
there we go
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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 | <?php
/**
* @version $Id: simplexml.php 10815 2008-08-27 01:15:56Z tcp $
* @package Joomla.Framework
* @subpackage Utilities
* @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();
/**
* SimpleXML implementation.
*
* The XML Parser extension (expat) is required to use JSimpleXML.
*
* The class provides a pure PHP4 implementation of the PHP5
* interface SimpleXML. As with PHP5's SimpleXML it is what it says:
* simple. Nevertheless, it is an easy way to deal with XML data,
* especially for read only access.
*
* Because it's not possible to use the PHP5 ArrayIterator interface
* with PHP4 there are some differences between this implementation
* and that of PHP5:
*
* <ul>
* <li>The access to the root node has to be explicit in
* JSimpleXML, not implicit as with PHP5. Write
* $xml->document->node instead of $xml->node</li>
* <li>You cannot acces CDATA using array syntax. Use the method data() instead</li>
* <li>You cannot access attributes directly with array syntax. use attributes()
* to read them.</li>
* <li>Comments are ignored.</li>
* <li>Last and least, this is not as fast as PHP5 SimpleXML--it is pure PHP4.</li>
* </ul>
*
* Example:
* <code>
* :simple.xml:
* <?xml version="1.0" encoding="utf-8" standalone="yes"?>
* <document>
* <node>
* <child gender="m">Tom Foo</child>
* <child gender="f">Tamara Bar</child>
* <node>
* </document>
*
* ---
*
* // read and write a document
* $xml = new JSimpleXML;
* $xml->loadFile('simple.xml');
* print $xml->document->toString();
*
* // access a given node's CDATA
* print $xml->root->node->child[0]->data(); // Tom Foo
*
* // access attributes
* $attr = $xml->root->node->child[1]->attributes();
* print $attr['gender']; // f
*
* // access children
* foreach( $xml->root->node->children() as $child ) {
* print $child->data();
* }
* </code>
*
* Note: JSimpleXML cannot be used to access sophisticated XML doctypes
* using datatype ANY (e.g. XHTML). With a DOM implementation you can
* handle this.
*
* @package Joomla.Framework
* @subpackage Utilities
* @since 1.5
*/
class JSimpleXML extends JObject
{
/**
* The XML parser
*
* @var resource
*/
var $_parser = null;
/**
* The XML document
*
* @var string
*/
var $_xml = '';
/**
* Document element
*
* @var object
*/
var $document = null;
/**
* Current object depth
*
* @var array
*/
var $_stack = array();
/**
* Constructor.
*
* @access protected
*/
function __construct($options = null)
{
if(! function_exists('xml_parser_create')) {
return false; //TODO throw warning
}
//Create the parser resource and make sure both versions of PHP autodetect the format.
$this->_parser = xml_parser_create('');
// check parser resource
xml_set_object($this->_parser, $this);
xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, 0);
if( is_array($options) )
{
foreach( $options as $option => $value ) {
xml_parser_set_option($this->_parser, $option, $value);
}
}
//Set the handlers
xml_set_element_handler($this->_parser, '_startElement', '_endElement');
xml_set_character_data_handler($this->_parser, '_characterData');
}
/**
* Interprets a string of XML into an object
*
* This function will take the well-formed xml string data and return an object of class
* JSimpleXMLElement with properties containing the data held within the xml document.
* If any errors occur, it returns FALSE.
*
* @param string Well-formed xml string data
* @param string currently ignored
* @return object JSimpleXMLElement
*/
function loadString($string, $classname = null) {
$this->_parse($string);
return true;
}
/**
* Interprets an XML file into an object
*
* This function will convert the well-formed XML document in the file specified by filename
* to an object of class JSimpleXMLElement. If any errors occur during file access or
* interpretation, the function returns FALSE.
*
* @param string Path to xml file containing a well-formed XML document
* @param string currently ignored
* @return boolean True if successful, false if file empty
*/
function loadFile($path, $classname = null)
{
//Check to see of the path exists
if ( !file_exists( $path ) ) {
return false;
}
//Get the XML document loaded into a variable
$xml = trim( file_get_contents($path) );
if ($xml == '')
{
return false;
}
else
{
$this->_parse($xml);
return true;
}
}
/**
* Get a JSimpleXMLElement object from a DOM node.
*
* This function takes a node of a DOM document and makes it into a JSimpleXML node.
* This new object can then be used as a native JSimpleXML element. If any errors occur,
* it returns FALSE.
*
* @param string DOM document
* @param string currently ignored
* @return object JSimpleXMLElement
*/
function importDOM($node, $classname = null) {
return false;
}
/**
* Get the parser
*
* @access public
* @return resource XML parser resource handle
*/
function getParser() {
return $this->_parser;
}
/**
* Set the parser
*
* @access public
* @param resource XML parser resource handle
*/
function setParser($parser) {
$this->_parser = $parser;
}
/**
* Start parsing an XML document
*
* Parses an XML document. The handlers for the configured events are called as many times as necessary.
*
* @param $xml string data to parse
*/
function _parse($data = '')
{
//Error handling
if (!xml_parse($this->_parser, $data)) {
$this->_handleError(
xml_get_error_code($this->_parser),
xml_get_current_line_number($this->_parser),
xml_get_current_column_number($this->_parser)
);
}
//Free the parser
xml_parser_free($this->_parser);
}
/**
* Handles an XML parsing error
*
* @access protected
* @param int $code XML Error Code
* @param int $line Line on which the error happened
* @param int $col Column on which the error happened
*/
function _handleError($code, $line, $col)
{
JError::raiseWarning( 'SOME_ERROR_CODE' , 'XML Parsing Error at '.$line.':'.$col.'. Error '.$code.': '.xml_error_string($code));
}
/**
* Gets the reference to the current direct parent
*
* @return object
*/
function _getStackLocation()
{
$return = '';
foreach($this->_stack as $stack) {
$return .= $stack.'->';
}
return rtrim($return, '->');
}
/**
* Handler function for the start of a tag
*
* @access protected
* @param resource $parser
* @param string $name
* @param array $attrs
*/
function _startElement($parser, $name, $attrs = array())
{
//Check to see if tag is root-level
$count = count($this->_stack);
if ($count == 0)
{
//If so, set the document as the current tag
$classname = get_class( $this ) . 'Element';
$this->document = new $classname($name, $attrs);
//And start out the stack with the document tag
$this->_stack = array('document');
}
//If it isn't root level, use the stack to find the parent
else
{
//Get the name which points to the current direct parent, relative to $this
$parent = $this->_getStackLocation();
//Add the child
eval('$this->'.$parent.'->addChild($name, $attrs, '.$count.');');
//Update the stack
eval('$this->_stack[] = $name.\'[\'.(count($this->'.$parent.'->'.$name.') - 1).\']\';');
}
}
/**
* Handler function for the end of a tag
*
* @access protected
* @param resource $parser
* @param string $name
*/
function _endElement($parser, $name)
{
//Update stack by removing the end value from it as the parent
array_pop($this->_stack);
}
/**
* Handler function for the character data within a tag
*
* @access protected
* @param resource $parser
* @param string $data
*/
function _characterData($parser, $data)
{
//Get the reference to the current parent object
$tag = $this->_getStackLocation();
//Assign data to it
eval('$this->'.$tag.'->_data .= $data;');
}
}
/**
* SimpleXML Element
*
* This object stores all of the direct children of itself in the $children array.
* They are also stored by type as arrays. So, if, for example, this tag had 2 <font>
* tags as children, there would be a class member called $font created as an array.
* $font[0] would be the first font tag, and $font[1] would be the second.
*
* To loop through all of the direct children of this object, the $children member
* should be used.
*
* To loop through all of the direct children of a specific tag for this object, it
* is probably easier to use the arrays of the specific tag names, as explained above.
*
* @package Joomla.Framework
* @subpackage Utilities
* @since 1.5
*/
class JSimpleXMLElement extends JObject
{
/**
* Array with the attributes of this XML element
*
* @var array
*/
var $_attributes = array();
/**
* The name of the element
*
* @var string
*/
var $_name = '';
/**
* The data the element contains
*
* @var string
*/
var $_data = '';
/**
* Array of references to the objects of all direct children of this XML object
*
* @var array
*/
var $_children = array();
/**
* The level of this XML element
*
* @var int
*/
var $_level = 0;
/**
* Constructor, sets up all the default values
*
* @param string $name
* @param array $attrs
* @param int $parents
* @return JSimpleXMLElement
*/
function __construct($name, $attrs = array(), $level = 0)
{
//Make the keys of the attr array lower case, and store the value
$this->_attributes = array_change_key_case($attrs, CASE_LOWER);
//Make the name lower case and store the value
$this->_name = strtolower($name);
//Set the level
$this->_level = $level;
}
/**
* Get the name of the element
*
* @access public
* @return string
*/
function name() {
return $this->_name;
}
/**
* Get the an attribute of the element
*
* @param string $attribute The name of the attribute
*
* @access public
* @return mixed If an attribute is given will return the attribute if it exist.
* If no attribute is given will return the complete attributes array
*/
function attributes($attribute = null)
{
if(!isset($attribute)) {
return $this->_attributes;
}
return isset($this->_attributes[$attribute]) ? $this->_attributes[$attribute] : null;
}
/**
* Get the data of the element
*
* @access public
* @return string
*/
function data() {
return $this->_data;
}
/**
* Set the data of the element
*
* @access public
* @param string $data
* @return string
*/
function setData($data) {
$this->_data = $data;
}
/**
* Get the children of the element
*
* @access public
* @return array
*/
function children() {
return $this->_children;
}
/**
* Get the level of the element
*
* @access public
* @return int
*/
function level() {
return $this->_level;
}
/**
* Adds an attribute to the element
*
* @param string $name
* @param array $attrs
*/
function addAttribute($name, $value)
{
//add the attribute to the element, override if it already exists
$this->_attributes[$name] = $value;
}
/**
* Removes an attribute from the element
*
* @param string $name
*/
function removeAttribute($name)
{
unset($this->_attributes[$name]);
}
/**
* Adds a direct child to the element
*
* @param string $name
* @param array $attrs
* @param int $level
* @return JSimpleXMLElement The added child object
*/
function &addChild($name, $attrs = array(), $level = null)
{
//If there is no array already set for the tag name being added,
//create an empty array for it
if(!isset($this->$name)) {
$this->$name = array();
}
// set the level if not already specified
if ($level == null) {
$level = ($this->_level + 1);
}
//Create the child object itself
$classname = get_class( $this );
$child = new $classname( $name, $attrs, $level );
//Add the reference of it to the end of an array member named for the elements name
$this->{$name}[] =& $child;
//Add the reference to the children array member
$this->_children[] =& $child;
//return the new child
return $child;
}
function removeChild(&$child)
{
$name = $child->name();
for ($i=0,$n=count($this->_children);$i<$n;$i++)
{
if ($this->_children[$i] == $child) {
unset($this->_children[$i]);
}
}
for ($i=0,$n=count($this->{$name});$i<$n;$i++)
{
if ($this->{$name}[$i] == $child) {
unset($this->{$name}[$i]);
}
}
$this->_children = array_values($this->_children);
$this->{$name} = array_values($this->{$name});
unset($child);
}
/**
* Get an element in the document by / separated path
*
* @param string $path The / separated path to the element
* @return object JSimpleXMLElement
*/
function &getElementByPath($path)
{
$tmp =& $this;
$false = false;
$parts = explode('/', trim($path, '/'));
foreach ($parts as $node)
{
$found = false;
foreach ($tmp->_children as $child)
{
if ($child->_name == $node)
{
$tmp =& $child;
$found = true;
break;
}
}
if (!$found) {
break;
}
}
if ($found) {
$ref =& $tmp;
} else {
$ref =& $false;
}
return $ref;
}
/**
* traverses the tree calling the $callback( JSimpleXMLElement
* $this, mixed $args=array() ) function with each JSimpleXMLElement.
*
* @param string $callback function name
* @param array $args
*/
function map($callback, $args=array())
{
$callback($this, $args);
// Map to all children
if ($n = count($this->_children)) {
for($i=0;$i<$n;$i++)
{
$this->_children[$i]->map($callback, $args);
}
}
}
/**
* Return a well-formed XML string based on SimpleXML element
*
* @return string
*/
function toString($whitespace=true)
{
//Start a new line, indent by the number indicated in $this->level, add a <, and add the name of the tag
if ($whitespace) {
$out = "\n".str_repeat("\t", $this->_level).'<'.$this->_name;
} else {
$out = '<'.$this->_name;
}
//For each attribute, add attr="value"
foreach($this->_attributes as $attr => $value) {
$out .= ' '.$attr.'="'.htmlspecialchars($value).'"';
}
//If there are no children and it contains no data, end it off with a />
if (empty($this->_children) && empty($this->_data)) {
$out .= " />";
}
else //Otherwise...
{
//If there are children
if(!empty($this->_children))
{
//Close off the start tag
$out .= '>';
//For each child, call the asXML function (this will ensure that all children are added recursively)
foreach($this->_children as $child)
$out .= $child->toString($whitespace);
//Add the newline and indentation to go along with the close tag
if ($whitespace) {
$out .= "\n".str_repeat("\t", $this->_level);
}
}
//If there is data, close off the start tag and add the data
elseif(!empty($this->_data))
$out .= '>'.htmlspecialchars($this->_data);
//Add the end tag
$out .= '</'.$this->_name.'>';
}
//Return the final output
return $out;
}
}
|