[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZDOMDocument class 4 // 5 // Created on: <16-Nov-2001 12:18:23 bf> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 /*! 30 \class eZDOMDocument ezdomdocument.php 31 \ingroup eZXML 32 \brief eZDOMDocument handles DOM nodes in DOM documents 33 34 The DOM document keeps a tree of DOM nodes and maintains 35 information on the various namespaces in the tree. 36 It also has helper functions for creating nodes and serializing 37 the tree into text. 38 39 Accessing and changing the name of the document is done using name() and setName(). 40 41 Accessing the tree is done using the root() method, use setRoot() to set 42 a new root node. The method appendChild() will do the same as setRoot(). 43 44 For fetching nodes globally the methods elementsByName(), elementsByNameNS() and namespaceByAlias() 45 can be used. They will fetch nodes according to the fetch criteria no matter where they are in the tree. 46 47 Creating new nodes is most easily done with the helper methods 48 createTextNode(), createCDATANode(), createElementNode() and createAttributeNode(). 49 They take care of creating the node with the correct type and does proper initialization. 50 51 Creating typical structures is also possible with the helper methods 52 createElementTextNode(), createElementCDATANode(), createElementNodeNS(), 53 createAttributeNamespaceDefNode() and createAttributeNodeNS(). 54 This will not only create the node itself but also the correct subchild, attribute or namespace. 55 56 After nodes are created they can be registered with the methods registerElement(), registerNamespaceAlias(). 57 This ensures that they are available globally in the DOM document. 58 59 Serializing the tree structure is done using toString(), this also allows specifying 60 the character set of the output. 61 62 Example of using the DOM document to create a node structure. 63 \code 64 $doc = new eZDOMDocument(); 65 $doc->setName( "FishCatalogue" ); 66 67 $root = $doc->createElementNode( "FishCatalogue" ); 68 $doc->setRoot( $root ); 69 70 $freshWater = $doc->createElementNode( "FreshWater" ); 71 $root->appendChild( $freshWater ); 72 73 $saltWater = $doc->createElementNode( "SaltWater" ); 74 $root->appendChild( $saltWater ); 75 76 $guppy = $doc->createElementNode( "Guppy" ); 77 $guppy->appendChild( $doc->createTextNode( "Guppy is a small livebreeder." ) ); 78 79 $freshWater->appendChild( $guppy ); 80 81 $cod = $doc->createElementNode( "Cod" ); 82 $saltWater->appendChild( $cod ); 83 84 $cod->appendChild( $doc->createCDATANode( "A big dull fish <-> !!" ) ); 85 86 print( $doc->toString() ); 87 88 // will print the following 89 <?xml version="1.0"?> 90 <FishCatalogue> 91 <FreshWater> 92 <Guppy> 93 Guppy is a small livebreeder. </Guppy> 94 </FreshWater> 95 <SaltWater> 96 <Cod> 97 <![CDATA[A big dull fish <-> !!]]> </Cod> 98 </SaltWater> 99 </FishCatalogue> 100 101 \endcode 102 */ 103 104 include_once ( "lib/ezxml/classes/ezdomnode.php" ); 105 106 class eZDOMDocument 107 { 108 /*! 109 Initializes the DOM document object with the name \a $name. 110 */ 111 function eZDOMDocument( $name = "", $setParentNode = false ) 112 { 113 $this->Name = $name; 114 $this->setParentNode = $setParentNode; 115 } 116 117 /* 118 Clean up memory. (destructor) 119 */ 120 function cleanup() 121 { 122 if ( $this->Root ) 123 eZDOMNode::cleanup( $this->Root ); 124 } 125 126 /*! 127 Sets the document name to \a $name. 128 */ 129 function setName( $name ) 130 { 131 $this->Name = $name; 132 } 133 134 /*! 135 \return The document root node if it exists, if not \c false is returned. 136 */ 137 function &root() 138 { 139 return $this->Root; 140 } 141 142 /*! 143 \return The document root node if it exists, if not \c false is returned. 144 Extra method for libxml compatibility. 145 */ 146 function &get_root() 147 { 148 return $this->Root; 149 } 150 151 /*! 152 Sets the document root node to \a $node. 153 If the parameter is not an eZDOMNode it will not be set. 154 */ 155 function setRoot( &$node ) 156 { 157 $this->appendChild( $node ); 158 } 159 160 /*! 161 Sets the document root node to \a $node. 162 If the parameter is not an eZDOMNode it will not be set. 163 \sa setRoot() 164 */ 165 function appendChild( &$node ) 166 { 167 if ( get_class( $node ) == "ezdomnode" ) 168 { 169 if ( $this->setParentNode !== false ) 170 $this->updateParentNodeProperty( $node ); 171 $this->Root =& $node; 172 } 173 } 174 175 /*! 176 Updates parentNode of the tree according to setParentNode property of the document. 177 (set false, if setParentNode is false) 178 */ 179 180 function updateParentNodeProperty( &$node ) 181 { 182 if ( $node->parentNode === false ) 183 $node->parentNode = null; 184 185 foreach( array_keys( $node->Children ) as $key ) 186 { 187 $this->updateParentNodeProperty( $node->Children[$key] ); 188 } 189 } 190 191 /*! 192 Finds all element nodes which matches the name \a $name and returns it. 193 \return An array with eZDOMNode elements. 194 */ 195 function &elementsByName( $name ) 196 { 197 if ( !in_array( $name, array_keys( $this->NamedNodes ) ) ) 198 { 199 $emptyArray = array(); 200 return $emptyArray; 201 } 202 return $this->NamedNodes[$name]; 203 } 204 205 /*! 206 Alias for libxml compatibility 207 */ 208 function get_elements_by_tagname( $name ) 209 { 210 return $this->elementsByName( $name ); 211 } 212 213 /*! 214 Finds all element nodes which matches the name \a $name and namespace URI \a $namespaceURI and returns it. 215 \return An array with eZDOMNode elements. 216 */ 217 function &elementsByNameNS( $name, $namespaceURI ) 218 { 219 return $this->NamedNodesNS[$name][$namespaceURI]; 220 } 221 222 /*! 223 Finds all element nodes which matches the namespace alias \a $alias and returns it. 224 \return An array with eZDOMNode elements. 225 */ 226 function namespaceByAlias( $alias ) 227 { 228 if ( isset( $this->Namespaces[$alias] ) ) 229 { 230 return $this->Namespaces[$alias]; 231 } 232 else 233 { 234 return false; 235 } 236 } 237 238 /*! 239 \static 240 Creates a DOM node of type text and returns it. 241 242 Text nodes are used to store text strings, 243 use content() on the node to extract the text. 244 245 \param $text The text string which will be stored in the node 246 247 \code 248 $dom->createTextNode( 'Edge' ); 249 \endcode 250 The resulting XML text will be 251 \code 252 Edge 253 \endcode 254 */ 255 function createTextNode( $text ) 256 { 257 /* We remove all control chars from the text, although they 258 * should have not be there in the first place. This is 259 * iso-8859-1 and UTF-8 safe. Those characters might also never exist 260 * in an XML document in the first place 261 * (http://w3.org/TR/2004/REC-xml-20040204/#NT-Char) so it's safe to 262 * remove them */ 263 $text = preg_replace('/[\x00-\x08\x0b-\x0c\x0e-\x1f]/', '', $text); 264 265 $node = new eZDOMNode(); 266 $node->setName( "#text" ); 267 $node->setContent( $text ); 268 $node->setType( 3 ); 269 270 return $node; 271 } 272 273 /*! 274 \static 275 Creates a DOM node of type CDATA and returns it. 276 277 CDATA nodes are used to store text strings, 278 use content() on the node to extract the text. 279 280 \param $text The text string which will be stored in the node 281 282 \code 283 $dom->createCDATANode( 'http://ez.no' ); 284 \endcode 285 The resulting XML text will be 286 \code 287 <![CDATA[http://ez.no]]> 288 \endcode 289 */ 290 function createCDATANode( $text ) 291 { 292 $node = new eZDOMNode(); 293 $node->setName( "#cdata-section" ); 294 $node->setContent( $text ); 295 $node->setType( 4 ); 296 297 return $node; 298 } 299 300 /*! 301 \deprecated not compatible with W3C DOM standard 302 303 \static 304 Creates DOMNodeElement recursivly from recursive array 305 */ 306 function createElementNodeFromArray( $name, $array ) 307 { 308 $node = new eZDOMNode(); 309 $node->setName( $name ); 310 $node->setType( 1 ); 311 312 foreach ( $array as $arrayKey => $value ) 313 { 314 if ( is_array( $value ) and 315 count( $valueKeys = array_keys( $value ) ) > 0 ) 316 { 317 if ( is_int( $valueKeys[0] ) ) 318 { 319 foreach( $value as $child ) 320 { 321 $node->appendChild( eZDOMDocument::createElementNodeFromArray( $arrayKey, $child ) ); 322 } 323 } 324 else 325 { 326 $node->appendChild( eZDOMDocument::createElementNodeFromArray( $arrayKey, $value ) ); 327 } 328 } 329 else 330 { 331 $node->appendAttribute( eZDomDocument::createAttributeNode( $arrayKey, $value ) ); 332 } 333 } 334 335 return $node; 336 } 337 338 /*! 339 \deprecated not compatible with W3C DOM standard 340 341 \static 342 Creates recursive array from DOMNodeElement 343 */ 344 function createArrayFromDOMNode( $domNode ) 345 { 346 if ( !$domNode ) 347 { 348 return null; 349 } 350 351 $retArray = array(); 352 foreach ( $domNode->children() as $childNode ) 353 { 354 if ( !isset( $retArray[$childNode->name()] ) ) 355 { 356 $retArray[$childNode->name()] = array(); 357 } 358 359 // If the node has children we create an array for this element 360 // and append to it, if not we assign it directly 361 if ( $childNode->hasChildren() ) 362 { 363 $retArray[$childNode->name()][] = eZDOMDocument::createArrayFromDOMNode( $childNode ); 364 } 365 else 366 { 367 $retArray[$childNode->name()] = eZDOMDocument::createArrayFromDOMNode( $childNode ); 368 } 369 } 370 foreach( $domNode->attributes() as $attributeNode ) 371 { 372 $retArray[$attributeNode->name()] = $attributeNode->content(); 373 } 374 375 return $retArray; 376 } 377 378 /*! 379 \deprecated Use createElement and setAttribute instead. 380 381 \static 382 Creates a DOM node of type element and returns it. 383 384 Element nodes are the basic node type in DOM tree, 385 they are used to structure nodes. 386 They can contain child nodes and attribute nodes accessible 387 with children() and attributes(). 388 389 \param $name The name of the element node. 390 \param $attributes An associative array with attribute names and attribute data. 391 This can be used to quickly fill in node attributes. 392 393 \code 394 $dom->createElementNode( 'song', 395 array( 'name' => 'Shine On You Crazy Diamond', 396 'track' => 1 ) ); 397 \endcode 398 The resulting XML text will be 399 \code 400 <song name='Shine On You Crazy Diamond' track='1' /> 401 \endcode 402 */ 403 function createElementNode( $name, $attributes = array() ) 404 { 405 $node = new eZDOMNode(); 406 $node->setName( $name ); 407 $node->setType( 1 ); 408 foreach ( $attributes as $attributeKey => $attributeValue ) 409 { 410 $node->appendAttribute( eZDomDocument::createAttributeNode( $attributeKey, $attributeValue ) ); 411 } 412 413 return $node; 414 } 415 416 /*! 417 Alias for libXML compatibility 418 */ 419 function create_element( $name, $attributes = array() ) 420 { 421 return $this->createElementNode( $name, $attributes ); 422 } 423 424 /*! 425 \deprecated Not compatible with W3C DOM standard 426 427 \static 428 Creates a DOM node of type element and returns it. 429 It will also create a DOM node of type text and add it as child of the element node. 430 431 \param $name The name of the element node. 432 \param $text The text string which will be stored in the text node 433 \param $attributes An associative array with attribute names and attribute data. 434 This can be used to quickly fill in element node attributes. 435 436 \code 437 $dom->createElementTextNode( 'name', 438 'Archer Maclean', 439 array( 'id' => 'archer', 440 'game' => 'ik+' ) ); 441 \endcode 442 The resulting XML text will be 443 \code 444 <name id='archer' game='ik+'>Archer Maclean</name> 445 \endcode 446 447 \sa createTextNode, createElementNode 448 */ 449 function createElementTextNode( $name, $text, $attributes = array() ) 450 { 451 $node = eZDOMDocument::createElementNode( $name, $attributes ); 452 $textNode = eZDOMDocument::createTextNode( $text ); 453 $node->appendChild( $textNode ); 454 455 return $node; 456 } 457 458 /*! 459 \deprecated not compatible with W3C DOM standard 460 461 \static 462 Creates a DOM node of type element and returns it. 463 It will also create a DOM node of type CDATA and add it as child of the element node. 464 465 \param $name The name of the element node. 466 \param $text The text string which will be stored in the CDATA node 467 \param $attributes An associative array with attribute names and attribute data. 468 This can be used to quickly fill in element node attributes. 469 470 \code 471 $dom->createElementCDATANode( 'name', 472 'Peter Molyneux', 473 array( 'type' => 'developer', 474 'game' => 'dungeon keeper' ) ); 475 \endcode 476 The resulting XML text will be 477 \code 478 <name type='developer' game='dungeon keeper'><![CDATA[Peter Molyneux]]></name> 479 \endcode 480 481 \sa createCDATANode, createElementNode 482 */ 483 function createElementCDATANode( $name, $text, $attributes = array() ) 484 { 485 $node = eZDOMDocument::createElementNode( $name, $attributes ); 486 $cdataNode = eZDOMDocument::createCDATANode( $text ); 487 $node->appendChild( $cdataNode ); 488 489 return $node; 490 } 491 492 /*! 493 \deprecated Not compatible with W3C DOM standard. 494 Use createElementNS instead. 495 496 \static 497 Creates a DOM node of type element with a namespace and returns it. 498 499 \param $uri The namespace URI for the element 500 \param $name The name of the element node. 501 502 \code 503 $dom->createElementNodeNS( 'http://ez.no/package', 504 'package' ); 505 \endcode 506 The resulting XML text will be 507 \code 508 <package xmlns="http://ez.no/package" /> 509 \endcode 510 511 \sa createElementNode 512 */ 513 function createElementNodeNS( $uri, $name ) 514 { 515 $node = new eZDOMNode(); 516 $node->setNamespaceURI( $uri ); 517 $node->setName( $name ); 518 $node->setType( 1 ); 519 520 return $node; 521 } 522 523 /*! 524 \deprecated Not compatible with W3C DOM standard. 525 Use createAttribute instead. 526 527 \static 528 Creates a DOM node of type attribute and returns it. 529 530 \param $name The name of the attribute 531 \param $content The content of the attribute 532 \param $prefix Namespace prefix which will be placed before the attribute name 533 534 \code 535 $dom->createAttributeNode( 'name', 536 'Pink Floyd', 537 'music-group' ); 538 \endcode 539 The resulting XML text will be 540 \code 541 music-group:name="Pink Floyd" 542 \endcode 543 */ 544 function createAttributeNode( $name, $content, $prefix = false ) 545 { 546 $node = new eZDOMNode(); 547 $node->setName( $name ); 548 if ( $prefix ) 549 $node->setPrefix( $prefix ); 550 $node->setContent( $content ); 551 $node->setType( 2 ); 552 553 return $node; 554 } 555 556 /*! 557 \deprecated Not compatible with W3C DOM standard. 558 Use createAttributeNS instead. 559 560 \static 561 Creates a DOM node of type attribute which is used for namespace definitions and returns it. 562 563 \param $prefix Namespace prefix which will be placed before the attribute name 564 \param $uri The unique URI for the namespace 565 566 \code 567 $dom->createAttributeNamespaceDefNode( 'music-group', 568 'http://music.org/groups' ); 569 \endcode 570 The resulting XML text will be 571 \code 572 xmlns:music-group="http://music.org/groups" 573 \endcode 574 */ 575 function createAttributeNamespaceDefNode( $prefix, $uri ) 576 { 577 $node = new eZDOMNode(); 578 $node->setName( $prefix ); 579 $node->setPrefix( "xmlns" ); 580 $node->setContent( $uri ); 581 $node->setType( 2 ); 582 583 return $node; 584 } 585 586 /*! 587 \deprecated Not compatible with W3C DOM standard. 588 Use createAttributeNS instead. 589 590 \static 591 Creates a DOM node of type attribute which is used for namespace definitions and returns it. 592 593 \param $uri The unique URI for the namespace 594 \param $name The name of the attribute 595 \param $content The content of the attribute 596 597 \code 598 $dom->createAttributeNodeNS( 'http://music.org/groups', 599 'name', 600 'Pink Floyd' ); 601 \endcode 602 The resulting XML text will be 603 \code 604 name="Pink Floyd" 605 \endcode 606 */ 607 function createAttributeNodeNS( $uri, $name, $content ) 608 { 609 $node = new eZDOMNode(); 610 $node->setName( $name ); 611 // $node->setPrefix( $prefix ); 612 $node->setNamespaceURI( $uri ); 613 $node->setContent( $content ); 614 $node->setType( 2 ); 615 616 return $node; 617 } 618 619 /*! 620 Adds the URL to a XSLT stylesheet to the document. 621 622 \param $url A vaild URL or array of URLs 623 */ 624 function setStylesheet( $url ) 625 { 626 if ( is_array( $url ) ) 627 $this->stylesheet = $url; 628 if ( $url ) 629 $this->stylesheet = array( $url ); 630 } 631 632 /*! 633 Adds the URL to a DTD to the document. 634 635 \param $url A vaild URL 636 \param $alias An alias representing the document, for example 637 \param "-//My Company//DTD XMLEXPORT V 1.0//EN" 638 \param $explict Declare if DTD must be used. 639 */ 640 function setDocTypeDefinition( $url = false, $alias = false, $explict = false ) 641 { 642 if ( $url or $alias ) 643 { 644 $this->dtd['url'] = $url; 645 $this->dtd['alias'] = $alias; 646 $this->dtd['explict'] = $explict; 647 } 648 } 649 650 /*! 651 Returns a XML string representation of the DOM document. 652 653 \param $charset The name of the output charset or \c false to use UTF-8 (default in XML) 654 \param $charsetConversion Controls whether the resulting text is converted to the specified 655 charset or not. 656 657 The \a $charsetConversion parameter can be useful when you know the inserted texts are 658 in the correct charset, turning conversion off can speed things up. 659 660 The XML creation is done by calling the eZDOMNode::toString() function on the root node 661 and let that handle the rest. 662 663 \note The charset conversion is smart enough to only do conversion when required 664 \note Using charset conversion will require the ezi18n library being installed 665 */ 666 function toString( $charset = true, $charsetConversion = true, $convertSpecialChars = true ) 667 { 668 $charsetText = ''; 669 if ( $charset === true ) 670 $charset = 'UTF-8'; 671 if ( $charset !== false ) 672 $charsetText = " encoding=\"$charset\""; 673 $text = "<?xml version=\"1.0\"$charsetText?>\n"; 674 675 if ( get_class( $this->Root ) == "ezdomnode" ) 676 { 677 if ( isset( $this->dtd ) ) 678 { 679 $text .= '<!DOCTYPE ' . $this->Root->name(); 680 if ( $explict ) 681 $text .= ' SYSTEM '; 682 else 683 $text .= ' PUBLIC '; 684 if ( $this->dtd['alias'] ) 685 $text .= '"' . $this->dtd['alias'] . '" "' . $this->dtd['url'] . '"'; 686 else 687 $text .= '"' . $this->dtd['url'] . '"'; 688 $text .= ">\n"; 689 } 690 691 if ( isset( $this->stylesheet ) && count( $this->stylesheet ) ) 692 { 693 foreach ( $this->stylesheet as $stylesheet ) 694 { 695 $text .= '<?xml-stylesheet type="text/xsl" href="' . $stylesheet . '"?>' . "\n"; 696 } 697 } 698 $text .= $this->Root->toString( 0, $charset, $convertSpecialChars ); 699 } 700 701 if ( $charsetConversion ) 702 { 703 include_once ( 'lib/ezi18n/classes/eztextcodec.php' ); 704 $codec =& eZTextCodec::instance( false, $charset, false ); 705 if ( $codec ) 706 { 707 $text = $codec->convertString( $text ); 708 } 709 } 710 711 return $text; 712 } 713 714 /*! 715 Alias for libxml compatibility 716 */ 717 function dump_mem( $charset = true, $conversion = true ) 718 { 719 return $this->toString( $charset, $conversion ); 720 } 721 722 /*! 723 Registers the node element \a $node in the DOM document. 724 This involves extracting the name of the node and add it to the 725 name lookup table which elementsByName() uses, then adding it 726 to the namespace lookup table which elementsByNameNS() uses. 727 728 \note This will not insert the node into the node tree. 729 */ 730 function registerElement( &$node ) 731 { 732 $this->NamedNodes[$node->name()][] =& $node; 733 734 if ( $node->namespaceURI() != "" ) 735 { 736 $this->NamedNodesNS[$node->name()][$node->namespaceURI()][] =& $node; 737 } 738 } 739 740 /*! 741 Register the namespace alias \a $alias to point to the namespace \a $namespace. 742 743 The namespace can then later on be fetched with namespaceByAlias(). 744 */ 745 function registerNamespaceAlias( $alias, $namespace ) 746 { 747 $this->Namespaces[$alias] =& $namespace; 748 } 749 750 751 /* 752 W3C DOM compatibility functions 753 754 */ 755 756 function &createElement( $name ) 757 { 758 $node = new eZDOMNode(); 759 $node->setName( $name ); 760 $node->setType( 1 ); 761 762 $this->registerElement( $node ); 763 764 return $node; 765 } 766 767 // \note W3C DOM function 768 769 function createElementNS( $namespaceURI, $qualifiedName ) 770 { 771 list( $prefix, $name ) = explode( ':', $qualifiedName ); 772 773 $node = new eZDOMNode(); 774 $node->setName( $name ); 775 $node->setPrefix( $prefix ); 776 $node->setNamespaceURI( $namespaceURI ); 777 $node->setType( 1 ); 778 return node; 779 } 780 781 // \note W3C DOM function 782 783 function createAttributeNS( $namespaceURI, $qualifiedName ) 784 { 785 list( $prefix, $name ) = explode( ':', $qualifiedName ); 786 787 $attr = new eZDOMNode(); 788 $attr->setName( $name ); 789 $attr->setPrefix( $prefix ); 790 $attr->setNamespaceURI( $namespaceURI ); 791 $attr->setType( 2 ); 792 return $attr; 793 } 794 795 // \note W3C DOM function 796 797 function createAttribute( $name ) 798 { 799 $attr = new eZDOMNode(); 800 $attr->setName( $name ); 801 $attr->setType( 2 ); 802 return $attr; 803 } 804 805 /// \privatesection 806 807 /// Document name 808 var $Name; 809 810 /// XML version 811 var $Version; 812 813 /// Contains an array of reference to the named nodes 814 var $NamedNodes = array(); 815 816 /// Contains an array of references to the named nodes with namespace 817 var $NamedNodesNS = array(); 818 819 /// Contains an array of the registered namespaces and their aliases 820 var $Namespaces = array(); 821 822 /// Reference to the first child of the DOM document 823 var $Root; 824 825 /// If false eZDOMNode::parentNode will be not set. 826 // This can is used to prevent memory cleanup problems when using mutual references in php. 827 var $setParentNode = false; 828 } 829 830 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |