[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/ezxml/classes/ -> ezdomnode.php (source)

   1  <?php
   2  //
   3  // Definition of eZDOMNode class
   4  //
   5  // Created on: <16-Nov-2001 12:11:43 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  /*! \file ezdomnode.php
  30    DOM node handling
  31  */
  32  
  33  /*!
  34    \class eZDOMNode ezdomnode.php
  35    \ingroup eZXML
  36    \brief eZDOMNode encapsulates XML DOM nodes
  37  
  38    The following node types are supported:
  39    - Element node, has value \c 1
  40    - Attribute node, has value \c 2
  41    - Text node, has value \c 3
  42    - CDATA node, has value \c 4
  43  
  44    \sa eZXML eZDOMDocument
  45  */
  46  
  47  /*!
  48   Element node, defines a node which contains attributes and children
  49  */
  50  define( "EZ_XML_NODE_ELEMENT", 1 );
  51  /*!
  52   Attribute node, defines a node which contains an attribute name and it's value
  53  */
  54  define( "EZ_XML_NODE_ATTRIBUTE", 2 );
  55  /*!
  56   Text node, defines a node which contains a text string encoded by escaping some characters.
  57  */
  58  define( "EZ_XML_NODE_TEXT", 3 );
  59  /*!
  60   CDATA node, defines a node which contains a text string encoding in a CDATA structure.
  61  */
  62  define( "EZ_XML_NODE_CDATASECTION", 4 );
  63  
  64  class eZDOMNode
  65  {
  66      /*!
  67        Initializes the DOM node.
  68      */
  69      function eZDOMNode()
  70      {
  71          $this->content =& $this->value;
  72          $this->Content =& $this->content;
  73          $this->Name =& $this->tagname;
  74          $this->Type =& $this->type;
  75          $this->nodeName =& $this->Name;
  76      }
  77  
  78      /*!
  79       Makes a copy of the current node and returns a reference to it.
  80      */
  81      function clone()
  82      {
  83          $tmp = new eZDOMNode();
  84          $tmp->Name = $this->Name;
  85          $tmp->Type = $this->Type;
  86          $tmp->Content = $this->Content;
  87          $tmp->Children = $this->Children;
  88          $tmp->Attributes = $this->Attributes;
  89          $tmp->NamespaceURI = $this->NamespaceURI;
  90          $tmp->LocalName = $this->LocalName;
  91          $tmp->Prefix = $this->Prefix;
  92          return $tmp;
  93      }
  94  
  95      /*!
  96        Subtree destructor. Needed to clean memory properly.
  97      */
  98      function cleanup( &$node )
  99      {
 100          if ( $node->hasChildren() )
 101          {
 102              foreach( array_keys( $node->Children ) as $key )
 103              {
 104                  $child =& $node->Children[$key];
 105                  if ( $child->hasChildren() )
 106                  {
 107                      $child->cleanup( $child );
 108                  }
 109  
 110              }
 111              $node->removeChildren();
 112          }
 113      }
 114  
 115      /*!
 116        \return The name of the node.
 117  
 118        For element and attributes nodes this will the name supplied when creating the node,
 119        for text nodes it returns \c #text and CDATA returns \c #cdata-section
 120      */
 121      function name()
 122      {
 123          return $this->Name;
 124      }
 125  
 126      /*!
 127        Sets the current name to \a $name.
 128      */
 129      function setName( $name )
 130      {
 131          $this->Name = $name;
 132          $this->LocalName = $name;
 133      }
 134  
 135      /*!
 136        \return The namespace URI for the node or \c false if no URI
 137      */
 138      function namespaceURI()
 139      {
 140          return $this->NamespaceURI;
 141      }
 142  
 143      /*!
 144        Sets the namespace URI of the node to \a $uri.
 145      */
 146      function setNamespaceURI( $uri )
 147      {
 148          $this->NamespaceURI = $uri;
 149      }
 150  
 151      /*!
 152        Returns the local name of the node if the node uses namespaces. If not false is returned.
 153      */
 154      function localName()
 155      {
 156          return $this->LocalName;
 157      }
 158  
 159      /*!
 160        \return The prefix of the nodes name, this will be the namespace for the node.
 161      */
 162      function prefix()
 163      {
 164          return $this->Prefix;
 165      }
 166  
 167      /*!
 168        Sets the namespace prefix for this node to \a $value.
 169      */
 170      function setPrefix( $value )
 171      {
 172          $this->Prefix = $value;
 173      }
 174  
 175      /*!
 176        \return An integer value which describes the type of node.
 177  
 178        The type is one of:
 179        - 1 - Element node, that is a node which contains attributes and children.
 180        - 2 - Attribute node, this is a node which contains a name and a value.
 181        - 3 - Text node, this is a node which contains a text string
 182        - 4 - CDATA node, this is a node which contains a text string
 183      */
 184      function type()
 185      {
 186          return $this->Type;
 187      }
 188  
 189      /*!
 190        Sets the node type to \a $type.
 191  
 192        Use one of the following defines for the type:
 193        - EZ_XML_NODE_ELEMENT - Element nodes
 194        - EZ_XML_NODE_ATTRIBUTE - Attribute nodes
 195        - EZ_XML_NODE_TEXT - Text nodes
 196        - EZ_XML_NODE_CDATASECTION - CDATA nodes
 197      */
 198      function setType( $type )
 199      {
 200          $this->Type = $type;
 201      }
 202  
 203      /*!
 204        \return The content of the node or \c false if it does not contain any content.
 205  
 206        \note This will only make sense for text and CDATA nodes.
 207      */
 208      function &content()
 209      {
 210          return $this->Content;
 211      }
 212  
 213      /*!
 214        Sets the content of the node to the \a $content.
 215  
 216        \note This will only make sense for text and CDATA nodes.
 217      */
 218      function setContent( $content )
 219      {
 220          $this->Content = $content;
 221      }
 222  
 223      /*!
 224        \return An array with attribute nodes.
 225  
 226        \note This will only make sense for element nodes.
 227      */
 228      function &attributes()
 229      {
 230          return $this->Attributes;
 231      }
 232  
 233      /*!
 234        \return An array with attribute nodes matching the namespace URI \a $namespaceURI.
 235  
 236        \note This will only make sense for element nodes.
 237      */
 238      function &attributesNS( $namespaceURI )
 239      {
 240          $ret = array();
 241          if ( count( $this->Attributes  ) > 0 )
 242          {
 243              foreach ( $this->Attributes as $attribute )
 244              {
 245                  if ( $attribute->namespaceURI() == $namespaceURI )
 246                  {
 247  
 248                      $ret[] = $attribute;
 249                  }
 250              }
 251          }
 252          return $ret;
 253      }
 254  
 255      /*!
 256        \return \c true if the node has any attributes.
 257  
 258        \note This will only make sense for element nodes.
 259      */
 260      function hasAttributes()
 261      {
 262          return count( $this->Attributes ) > 0;
 263      }
 264  
 265      /*!
 266        \return The number of attributes for the node.
 267  
 268        \note This will only make sense for element nodes.
 269      */
 270      function attributeCount()
 271      {
 272          return count( $this->Attributes );
 273      }
 274  
 275      /*!
 276        \return An array with child nodes.
 277  
 278        \note This will only make sense for element nodes.
 279      */
 280      function &children()
 281      {
 282          return $this->Children;
 283      }
 284  
 285      /*!
 286        \return \c true if the node has children.
 287  
 288        \note This will only make sense for element nodes.
 289      */
 290      function hasChildren()
 291      {
 292          return count( $this->Children ) > 0;
 293      }
 294  
 295      /*!
 296        \return The number of children for the node.
 297  
 298        \note This will only make sense for element nodes.
 299      */
 300      function childrenCount()
 301      {
 302          return count( $this->Children );
 303      }
 304  
 305      /*!
 306       \return first child of current dom node.
 307  
 308       \note added for compatibility with DOM XML library
 309      */
 310      function first_child()
 311      {
 312          return isset( $this->Children[0] ) ? $this->Children[0] : null;
 313      }
 314  
 315      /*!
 316       \return node value of current dom node.
 317  
 318       \note added for compatibility with DOM XML library
 319      */
 320      function node_value()
 321      {
 322          return $this->Content;
 323      }
 324  
 325      /*!
 326        Finds the first element named \a $name and returns the children of that node.
 327        If no element node is found it returns \c false.
 328  
 329        \note This will only make sense for element nodes.
 330        \note If multiple elements with that name is found \c false is returned.
 331        \sa elementByName, children
 332      */
 333      function &elementChildrenByName( $name )
 334      {
 335          $element =& $this->elementByName( $name );
 336          if ( !$element )
 337          {
 338              $children = false;
 339              return $children;
 340          }
 341          return $element->children();
 342      }
 343  
 344      /*!
 345        Finds the first element named \a $name and returns the first child of that node.
 346        If no element node is found or there are not children it returns \c false.
 347  
 348        \note This will only make sense for element nodes.
 349        \note If multiple elements with that name is found \c false is returned.
 350        \sa elementByName, firstChild
 351      */
 352      function &elementFirstChildByName( $name )
 353      {
 354          $element =& $this->elementByName( $name );
 355          if ( !$element )
 356          {
 357              $child = false;
 358              return $child;
 359          }
 360          return $element->firstChild();
 361      }
 362  
 363      /*!
 364        \returns The first element that is named \a $name.
 365                 If multiple elements with that name is found \c false is returned.
 366  
 367        \note This will only make sense for element nodes.
 368        \sa elementsByName
 369      */
 370      function &elementByName( $name )
 371      {
 372          $element = false;
 373          foreach ( array_keys( $this->Children ) as $key )
 374          {
 375              $child =& $this->Children[$key];
 376              if ( $child->name() == $name )
 377              {
 378                  if ( $element )
 379                  {
 380                      $retValue = false;
 381                      return $retValue;
 382                  }
 383                  $element =& $child;
 384              }
 385          }
 386          return $element;
 387      }
 388  
 389      /*!
 390       Alias for libxml compatibility
 391      */
 392      function get_elements_by_tagname( $name )
 393      {
 394          $elements = array();
 395          foreach ( array_keys( $this->Children ) as $key )
 396          {
 397              $child =& $this->Children[$key];
 398              if ( $child->name() == $name )
 399                  $elements[] =& $child;
 400          }
 401  
 402          return $elements;
 403      }
 404  
 405      /*!
 406        Finds the first element named \a $name and returns the text content of that node.
 407        If no element node is found or no text content exists it returns \c false.
 408  
 409        \note This will only make sense for element nodes.
 410        \note If multiple elements with that name is found \c false is returned.
 411        \sa elementByName, textContent
 412      */
 413      function elementTextContentByName( $name )
 414      {
 415          $element = $this->elementByName( $name );
 416          if ( !$element )
 417          {
 418              return false;
 419          }
 420  
 421          return $element->textContent();
 422      }
 423  
 424      /*!
 425       \param attribute name
 426       \param attribute value
 427  
 428       \return element by attribute value
 429      */
 430      function &elementByAttributeValue( $attr, $value )
 431      {
 432          foreach ( array_keys( $this->Children ) as $key )
 433          {
 434              $child =& $this->Children[$key];
 435              if ( $child->attributeValue( $attr ) == $value )
 436              {
 437                  return $child;
 438              }
 439          }
 440          $child = false;
 441          return $child;
 442      }
 443  
 444      /*!
 445        \return An array with elements that matches the name \a $name.
 446  
 447        \note This will only make sense for element nodes.
 448        \sa elementByName
 449      */
 450      function &elementsByName( $name )
 451      {
 452          $elements = array();
 453          foreach ( array_keys( $this->Children ) as $key )
 454          {
 455              $child =& $this->Children[$key];
 456              if ( $child->name() == $name )
 457              {
 458                  $elements[] =& $child;
 459              }
 460          }
 461          return $elements;
 462      }
 463  
 464      /*!
 465        \return An array with text contents taken from all child nodes which matches the name \a $name.
 466  
 467        \note This will only make sense for element nodes.
 468        \sa elementsByName, textContent
 469      */
 470      function &elementsTextContentByName( $name )
 471      {
 472          $elements = array();
 473          foreach ( array_keys( $this->Children ) as $key )
 474          {
 475              $child =& $this->Children[$key];
 476              if ( $child->name() == $name )
 477              {
 478                  $elements[] = $child->textContent();
 479              }
 480          }
 481          return $elements;
 482      }
 483  
 484      /*!
 485        \deprecated This function is deprecated.
 486                    Use getAttribute instead.
 487      
 488        \return The value of the attribute named \a $attributeName.
 489        If no value is found \c false is returned.
 490  
 491        \note This will only make sense for element nodes.
 492      */
 493      function attributeValue( $attributeName )
 494      {
 495          $returnValue = false;
 496          foreach ( $this->Attributes as $attribute )
 497          {
 498              if ( $attribute->name() == $attributeName )
 499                  $returnValue = $attribute->content();
 500          }
 501  
 502          return $returnValue;
 503      }
 504  
 505      /*!
 506        Alias for libxml compatibility
 507      */
 508      function get_attribute( $attributeName )
 509      {
 510          return $this->attributeValue( $attributeName );
 511      }
 512  
 513      /*!
 514        Finds the first element named \a $name and returns the value of the attribute named \a $attributeName.
 515        If no element node is found or no attribute with the given name exists it returns \c false.
 516  
 517        \note This will only make sense for element nodes.
 518        \note If multiple elements with that name is found \c false is returned.
 519        \sa elementByName, attributeValue
 520      */
 521      function elementAttributeValueByName( $name, $attributeName )
 522      {
 523          $element = $this->elementByName( $name );
 524          if ( !$element )
 525              return false;
 526          else
 527              return $element->attributeValue( $attributeName );
 528      }
 529  
 530      /*!
 531        Goes trough all attributes of the node and matches the attribute names
 532        with the parameter \a $attributeDefinitions.
 533  
 534        \param $attributeDefinitions An associative array which maps from matching attribute name to lookup name.
 535        \param $defaultValue If other value than \c null it will be set as value for all lookup names that didn't match
 536  
 537        The matching attribute name in the will be matched against the attributes of the node.
 538        When a match is found the attribute value will be fetched and placed in the returned
 539        associative array using lookup name as key.
 540  
 541        A code example will explain this, the variable \a $songNode contains the following xml
 542        \code
 543        <song name="Shine On You Crazy Diamond" track="1" />
 544        \endcode
 545  
 546        The PHP code is.
 547        \code
 548        $def = array( 'name' => 'song_name',
 549                      'track' => 'track_number' );
 550        $values = $songNode->attributeValues( $def );
 551        \encode
 552  
 553        \a $values will now contain.
 554        \code
 555        array( 'song_name' => 'Shine On You Crazy Diamond',
 556               'track_number => '1' )
 557        \endcode
 558  
 559        This method and appendAttributes() work together, the values inserted with appendAttributes()
 560        can be extracted with this method.
 561  
 562        \note This will only make sense for element nodes.
 563        \sa elementAttributeValueByName, appendAttributes
 564      */
 565      function attributeValues( $attributeDefinitions = false, $defaultValue = null )
 566      {
 567          $hash = array();
 568          foreach ( $this->Attributes as $attribute )
 569          {
 570              if ( $attributeDefinitions === false )
 571              {
 572                  $hash[$attribute->name()] = $attribute->content();
 573                  continue;
 574              }
 575  
 576              foreach ( $attributeDefinitions as $attributeName => $keyName )
 577              {
 578                  if ( $attribute->name() == $attributeName )
 579                  {
 580                      $hash[$keyName] = $attribute->content();
 581                      break;
 582                  }
 583              }
 584          }
 585          if ( $defaultValue !== null )
 586          {
 587              foreach ( $attributeDefinitions as $attributeName => $keyName )
 588              {
 589                  if ( !isset( $hash[$keyName] ) )
 590                      $hash[$keyName] = $defaultValue;
 591              }
 592          }
 593  
 594          return $hash;
 595      }
 596  
 597      /*!
 598        \deprecated This function is deprecated.
 599                    Use getAttributeNS instead.
 600      
 601        \return The value of the attribute named \a $attributeName and having namespace \a $namespaceURI.
 602        If no value is found \c false is returned.
 603  
 604        \note This will only make sense for element nodes.
 605      */
 606      function attributeValueNS( $attributeName, $namespaceURI )
 607      {
 608          $returnValue = false;
 609          if ( count( $this->Attributes  ) > 0 )
 610          {
 611              foreach ( $this->Attributes as $attribute )
 612              {
 613                  if ( $attribute->name() == $attributeName &&
 614                       $attribute->namespaceURI() == $namespaceURI )
 615                  {
 616  
 617                      $returnValue = $attribute->content();
 618                  }
 619              }
 620          }
 621  
 622          return $returnValue;
 623      }
 624  
 625      /*!
 626        Appends the node \a $node as a child of the current node.
 627  
 628        \return The node that was just inserted or \c false if it failed to insert a node.
 629  
 630        \note This will only make sense for element nodes.
 631      */
 632      function appendChild( &$node )
 633      {
 634          if ( get_class( $node ) == "ezdomnode" )
 635          {
 636              if ( $this->parentNode !== false )
 637                  $node->parentNode =& $this;
 638  
 639              $this->Children[] =& $node;
 640              return $node;
 641          }
 642          return false;
 643      }
 644  
 645      /*!
 646       Alias for libXML compatibility
 647      */
 648      function append_child( &$node )
 649      {
 650          return $this->appendChild( $node );
 651      }
 652  
 653      /*!
 654        Appends the attribute node \a $node as an attribute of the current node.
 655  
 656        \return The attribute node that was just inserted or \c false if it failed to insert an attribute.
 657  
 658        \note This will only make sense for element nodes.
 659      */
 660      function appendAttribute( &$node )
 661      {
 662          if ( get_class( $node ) == "ezdomnode" )
 663          {
 664              $this->Attributes[] =& $node;
 665              return $node;
 666          }
 667          return false;
 668      }
 669  
 670      function set_attribute( $name, $value )
 671      {
 672          $this->removeNamedAttribute( $name );
 673          return $this->appendAttribute( eZDOMDocument::createAttributeNode( $name, $value ) );
 674      }
 675  
 676      /*!
 677        Appends multiple attributes and attribute values.
 678  
 679        \param $attributeValues An associative array containing the attribute values to insert,
 680                                it maps from lookup name to attribute value.
 681        \param $attributeDefinitions An associative array defining how lookup names maps to attribute names,
 682                                     the array key is the attribute name and the array value the lookup name.
 683        \param $includeEmptyValues If \c true it will set attribute values even though they don't exist in \a $attributeValues
 684  
 685        \code
 686        $definition = array( 'name' => 'song_name',
 687                             'track' => 'track_name' );
 688        $values = array( 'song_name' => 'Shine On You Crazy Diamond',
 689                         'track_number' => '1' );
 690        $node->appendAttributes( $values, $definition );
 691        \encode
 692  
 693        The node will then look like.
 694        \code
 695        <song name="Shine On You Crazy Diamond" track="1" />
 696        \endcode
 697  
 698        This method and attributeValues() work together, the returned result of attributeValues()
 699        can be inserted with this method.
 700  
 701        \note This will only make sense for element nodes.
 702        \sa attributeValues
 703      */
 704      function appendAttributes( $attributeValues,
 705                                 $attributeDefinitions,
 706                                 $includeEmptyValues = false )
 707      {
 708          foreach ( $attributeDefinitions as $attributeXMLName => $attributeKey )
 709          {
 710              if ( $includeEmptyValues or
 711                   ( isset( $attributeValues[$attributeKey] ) and
 712                     $attributeValues[$attributeKey] !== false ) )
 713              {
 714                  $value = false;
 715                  if ( isset( $attributeValues[$attributeKey] ) and
 716                       $attributeValues[$attributeKey] !== false )
 717                      $value = $attributeValues[$attributeKey];
 718                  $this->Attributes[] = eZDOMDocument::createAttributeNode( $attributeXMLName, $value );
 719              }
 720          }
 721      }
 722  
 723      /*!
 724        Removes the attribute node named \a $name.
 725        \return The removed attribute node or \c false if no such node exists.
 726  
 727        \note This will only make sense for element nodes.
 728      */
 729      function removeNamedAttribute( $name )
 730      {
 731          $removed = false;
 732          foreach( array_keys( $this->Attributes ) as $key )
 733          {
 734              if ( $this->Attributes[$key]->name() == $name )
 735              {
 736                  unset( $this->Attributes[$key] );
 737                  $removed = true;
 738              }
 739          }
 740          return $removed;
 741      }
 742  
 743      /*!
 744       Alias for libxml compatibility
 745      */
 746      function remove_attribute( $name )
 747      {
 748          return $this->removeNamedAttribute( $name );
 749      }
 750  
 751      /*!
 752        Removes all attribute from the node.
 753  
 754        \note This will only make sense for element nodes.
 755      */
 756      function removeAttributes()
 757      {
 758          $this->Attributes = array();
 759      }
 760  
 761      /*!
 762        Removes all child nodes that matches the name \a $name.
 763        \return \c true if it removed any nodes, otherwise \c false.
 764  
 765        \note This will only make sense for element nodes.
 766      */
 767      function removeNamedChildren( $name )
 768      {
 769          $removed = false;
 770          foreach( array_keys( $this->Children ) as $key )
 771          {
 772              if ( $this->Children[$key]->name() == $name )
 773              {
 774                  if ( $this->parentNode !== false )
 775                  {
 776                      unset( $this->Children[$key]->parentNode );
 777                      $this->Children[$key]->parentNode = null;
 778                  }
 779                  unset( $this->Children[$key] );
 780                  $removed = true;
 781              }
 782          }
 783          return $removed;
 784      }
 785  
 786      /*!
 787        Removes all child nodes from the current node.
 788  
 789        \note This will only make sense for element nodes.
 790      */
 791      function removeChildren()
 792      {
 793          if ( $this->parentNode !== false )
 794          {
 795              foreach( array_keys( $this->Children ) as $key )
 796              {
 797                 unset( $this->Children[$key]->parentNode );
 798                 $this->Children[$key]->parentNode = null;
 799              }
 800          }
 801  
 802          $this->Children = array();
 803      }
 804  
 805      /*!
 806        Removes the last child node of the current node.
 807  
 808        \note This will only make sense for element nodes.
 809      */
 810      function removeLastChild()
 811      {
 812          end( $this->Children );
 813          $key = key( $this->Children );
 814          if ( $this->parentNode !== false )
 815          {
 816              unset( $this->Children[$key]->parentNode );
 817              $this->Children[$key]->parentNode = null;
 818          }
 819  
 820          unset( $this->Children[$key] );
 821      }
 822  
 823      /*!
 824        Removes child by the given child object.
 825        
 826        \note W3C DOM function
 827      */
 828      function removeChild( &$childToRemove )
 829      {
 830          if ( $childToRemove->parentNode !== false )
 831          {
 832              unset( $childToRemove->parentNode );
 833              $childToRemove->parentNode = null;
 834          }
 835          $childToRemove->flag = true;
 836  
 837          foreach ( array_keys( $this->Children ) as $key )
 838          {
 839              if ( $this->Children[$key]->flag === true )
 840              {
 841                  unset( $this->Children[$key] );
 842                  break;
 843              }
 844          }
 845          $childToRemove->flag = false;
 846      }
 847  
 848      /*!
 849        \return The content() of the first child node or \c false if there are no children.
 850  
 851        \note This will only make sense for element nodes.
 852        \sa elementTextContentByName
 853      */
 854      function textContent()
 855      {
 856          return $this->collectTextContent( $this );
 857      }
 858  
 859      function collectTextContent( &$element )
 860      {
 861          $ret = '';
 862          if ( $element->Type == EZ_XML_NODE_TEXT )
 863          {
 864              $ret = $element->content();
 865          }
 866          else
 867          {
 868              if ( count( $element->Children ) > 0 )
 869              {
 870                  foreach( array_keys( $element->Children ) as $key )
 871                  {
 872                      $child =& $element->Children[$key];
 873                      $ret .= $this->collectTextContent( $child );
 874                  }
 875              }
 876          }
 877          return $ret;
 878      }
 879  
 880      /*!
 881        \return A string that represents the current node.
 882        The string will be created according to the node type which are:
 883        - Element node, places the name in <>, expands all attributes and calls toString() on all children.
 884        - Text node, returns the content() by escaping the characters & < > ' and ".
 885        - CDATA node, returns the text wrapped in <![CDATA[ and ]]
 886  
 887        \param $level The current tab level, starts at 0 and is increased by 1 for each recursion
 888        \param $charset Which charset the text will be encoded in, currently not used
 889  
 890        Example strings.
 891        \code
 892        '<song name="Shine On You Crazy Diamond" track="1" />'
 893        'This &amp; that &quot;wrapped&quot; in &lt;div&gt; tags'
 894        '<![CDATA[This & that "wrapped" in <div> tags'
 895        \endcode
 896  
 897        \note This will only make sense for element nodes.
 898      */
 899      function toString( $level, $charset = false, $convertSpecialChars = true )
 900      {
 901          $spacer = str_repeat( " ", $level*2 );
 902          $ret = "";
 903          switch ( $this->Name )
 904          {
 905              case "#text" :
 906              {
 907                  $tagContent = $this->Content;
 908                  // convert special chars
 909                  if ( $convertSpecialChars )
 910                  {
 911                      $tagContent = str_replace( "&", "&amp;", $tagContent );
 912                      $tagContent = str_replace( ">", "&gt;", $tagContent );
 913                      $tagContent = str_replace( "<", "&lt;", $tagContent );
 914                      $tagContent = str_replace( "'", "&apos;", $tagContent );
 915                      $tagContent = str_replace( '"', "&quot;", $tagContent );
 916                  }
 917  
 918                  $ret = $tagContent;
 919              }break;
 920  
 921              case "#cdata-section" :
 922              {
 923                  $ret = "<![CDATA[";
 924                  $ret .= $this->Content;
 925                  $ret .= "]]>";
 926              }break;
 927  
 928              default :
 929              {
 930                  $isOneLiner = false;
 931                  // check if it's a oneliner
 932                  if ( count( $this->Children ) == 0 and ( $this->Content == "" ) )
 933                      $isOneLiner = true;
 934  
 935                  $attrStr = "";
 936  
 937                  // check for namespace definition
 938                  if ( $this->namespaceURI() != "" )
 939                  {
 940                      $attrPrefix = "";
 941                      if ( $this->Prefix != "" )
 942                          $attrPrefix = ":" . $this->prefix();
 943                      $attrStr = " xmlns" . $attrPrefix . "=\"" . $this->namespaceURI() . "\"";
 944                  }
 945  
 946                  $prefix = "";
 947                  if ( $this->Prefix != false )
 948                      $prefix = $this->Prefix. ":";
 949  
 950                  // generate attributes string
 951                  if ( count( $this->Attributes ) > 0 )
 952                  {
 953                      $i = 0;
 954                      foreach ( $this->Attributes as $attr )
 955                      {
 956                          $attrPrefix = "";
 957                          if ( $attr->prefix() != false )
 958                              $attrPrefix = $attr->prefix(). ":";
 959  
 960                          if ( $i > 0 )
 961                              $attrStr .= "\n" . $spacer . str_repeat( " ", strlen( $prefix . $this->Name ) + 1 + 1  );
 962                          else
 963                              $attrStr .= ' ';
 964  
 965                          $attrContent = $attr->content();
 966                          $attrContent = str_replace( "&", "&amp;", $attrContent );
 967                          $attrContent = str_replace( ">", "&gt;", $attrContent );
 968                          $attrContent = str_replace( "<", "&lt;", $attrContent );
 969                          $attrContent = str_replace( "'", "&apos;", $attrContent );
 970                          $attrContent = str_replace( '"', "&quot;", $attrContent );
 971  
 972                          $attrStr .=  $attrPrefix . $attr->name() . "=\"" . $attrContent . "\"";
 973                          ++$i;
 974                      }
 975                  }
 976  
 977                  if ( $isOneLiner )
 978                      $oneLinerEnd = " /";
 979                  else
 980                      $oneLinerEnd = "";
 981  
 982                  $ret = '';
 983  
 984                  if ( $this->Name =='link' )  //don't insert enything before <link> tag
 985                  {
 986                      $ret .= "<" . $prefix . $this->Name . $attrStr . $oneLinerEnd . ">";
 987                  }
 988                  else //make alignment
 989                  {
 990                      if ( $level > 0 )
 991                          $ret .= "\n";
 992                      $ret .= "$spacer<" . $prefix . $this->Name . $attrStr . $oneLinerEnd . ">";
 993                  }
 994  
 995                  $lastChildType = false;
 996                  if ( count( $this->Children ) > 0 )
 997                  {
 998                      foreach ( $this->Children as $child )
 999                      {
1000                          $ret .= $child->toString( $level + 1, $charset, $convertSpecialChars );
1001                          $lastChildType = $child->type();
1002                      }
1003                  }
1004  
1005                  if ( !$isOneLiner )
1006                  {
1007                      if ( $lastChildType == 1 )
1008                          $ret .= "\n$spacer";
1009                      $ret .= "</" . $prefix . $this->Name . ">";
1010                  }
1011  //                    $ret .= "$spacer</" . $prefix . $this->Name . ">\n";
1012  
1013              }break;
1014          }
1015          return $ret;
1016      }
1017  
1018      /*!
1019       Alias for libxml compatibility
1020      */
1021      function dump_mem( $format, $charset = false )
1022      {
1023          return $this->toString( 0, $charset);
1024      }
1025  
1026  
1027      /*
1028          W3C DOM compatibility functions
1029      */
1030      // \note W3C DOM function
1031  
1032      function setAttribute( $name, $value )
1033      {
1034          foreach ( $this->Attributes as $attribute )
1035          {
1036              if ( $attribute->name() == $name )
1037              {
1038                  $attribute->setContent( $value );
1039                  return $attribute;
1040              }
1041          }
1042  
1043          $attr = eZDOMDocument::createAttribute( $name );
1044          $attr->setContent( $value );
1045          return $this->appendAttribute( $attr );
1046      }
1047  
1048      // \note W3C DOM function
1049  
1050      function setAttributeNS( $namespaceURI, $qualifiedName, $value )
1051      {
1052          foreach ( $this->Attributes as $attribute )
1053          {
1054              if ( !$attribute->Prefix )
1055                  continue;
1056  
1057              $fullName = $attribute->Prefix . ':' . $attribute->LocalName;
1058              if ( $fullName == $qualifiedName )
1059              {
1060                  $attribute->setContent( $value );
1061                  return $attribute;
1062              }
1063          }
1064          $attr = eZDOMDocument::createAttributeNS( $namespaceURI, $qualifiedName );
1065          $attr->setContent( $value );
1066          return $this->appendAttribute( $attr );
1067      }
1068  
1069      // \note W3C DOM function
1070      function getAttribute( $attributeName )
1071      {
1072          $returnValue = '';
1073          foreach ( $this->Attributes as $attribute )
1074          {
1075              if ( $attribute->name() == $attributeName && !$attribute->Prefix )
1076                  $returnValue = $attribute->Content;
1077          }
1078  
1079          return $returnValue;
1080      }
1081  
1082      // \note W3C DOM function
1083      function getAttributeNS( $namespaceURI, $localName )
1084      {
1085          $returnValue = '';
1086          foreach ( $this->Attributes as $attribute )
1087          {
1088              if ( $attribute->LocalName == $localName &&
1089                   $attribute->NamespaceURI == $namespaceURI )
1090                  $returnValue = $attribute->Content;
1091          }
1092  
1093          return $returnValue;
1094      }
1095  
1096      // \note W3C DOM function
1097      function removeAttribute( $name )
1098      {
1099          $removed = false;
1100          foreach( array_keys( $this->Attributes ) as $key )
1101          {
1102              if ( $this->Attributes[$key]->name() == $name && !$this->Attributes[$key]->Prefix )
1103              {
1104                  unset( $this->Attributes[$key] );
1105                  $removed = true;
1106              }
1107          }
1108          return $removed;
1109      }
1110  
1111      // \note W3C DOM function
1112      function removeAttributeNS( $namespaceURI, $localName )
1113      {
1114          $removed = false;
1115          foreach( array_keys( $this->Attributes ) as $key )
1116          {
1117              if ( $this->Attributes[$key]->LocalName == $localName &&
1118                   $this->Attributes[$key]->NamespaceURI == $namespaceURI )
1119              {
1120                  unset( $this->Attributes[$key] );
1121                  $removed = true;
1122              }
1123          }
1124          return $removed;
1125      }
1126  
1127      /*
1128        \note W3C DOM function
1129      */
1130      function hasChildNodes()
1131      {
1132          return count( $this->Children ) > 0;
1133      }
1134  
1135      /*!
1136        \return The first child of the node or \c false if there are no children.
1137  
1138        \note This will only make sense for element nodes.
1139        \note W3C DOM function
1140      */
1141  
1142      function &firstChild()
1143      {
1144          if ( count( $this->Children ) == 0 )
1145          {
1146              $child = false;
1147              return $child;
1148          }
1149          reset( $this->Children );
1150          $key = key( $this->Children );
1151          $child =& $this->Children[$key];
1152  
1153          return $child;
1154      }
1155  
1156      /*!
1157       \return The last child node or \c false if there are no children.
1158  
1159        \note This will only make sense for element nodes.
1160      */
1161  
1162      function &lastChild()
1163      {
1164          if ( count( $this->Children ) == 0 )
1165          {
1166              $child = false;
1167              return $child;
1168          }
1169          end( $this->Children );
1170          $key = key( $this->Children );
1171          $child =& $this->Children[$key];
1172  
1173          return $child;
1174      }
1175  
1176      /*!
1177        Replaces child by the new one given.
1178  
1179        \note W3C DOM function
1180      */
1181      function replaceChild( &$newChild, &$oldChild )
1182      {
1183          if ( $this->parentNode !== false )
1184          {
1185              unset( $oldChild->parentNode );
1186              $oldChild->parentNode = null;
1187          }
1188          $oldChild->flag = true;
1189  
1190          $newChildren = array();
1191  
1192          foreach( array_keys( $this->Children ) as $key )
1193          {
1194              if ( $this->Children[$key]->flag === true )
1195              {
1196                  if ( $this->parentNode !== false )
1197                      $newChild->parentNode =& $this;
1198  
1199                  $newChildren[$key] =& $newChild;
1200              }
1201              else
1202              {
1203                  $newChildren[$key] =& $this->Children[$key];
1204              }
1205          }
1206          $this->Children =& $newChildren;
1207          $oldChild->flag = false;
1208  
1209          return $oldChild;
1210      }
1211  
1212      /*!
1213        Replaces child by the new one given.
1214  
1215        \note W3C DOM function
1216      */
1217      function insertBefore( &$newNode, &$refNode )
1218      {
1219          $refNode->flag = true;
1220  
1221          $newChildren = array();
1222  
1223          foreach ( array_keys( $this->Children ) as $key )
1224          {
1225              if ( $this->Children[$key]->flag === true )
1226              {
1227                  $newChildren[] =& $newNode;
1228                  if ( $this->parentNode !== false )
1229                  {
1230                      $newNode->parentNode =& $this;
1231                  }
1232              }
1233              $newChildren[] =& $this->Children[$key];
1234          }
1235          $this->Children =& $newChildren;
1236          $refNode->flag = false;
1237          return $newNode;
1238      }
1239  
1240      /*!
1241        \note emulation of W3C DOM property
1242      */
1243  
1244      function &nextSibling()
1245      {
1246          $ret = null;
1247          if ( !$this->parentNode )
1248              return $ret;
1249  
1250          $parent =& $this->parentNode;
1251          $this->flag = true;
1252  
1253          $next = false;
1254          $children =& $parent->Children;
1255  
1256          foreach( array_keys( $children ) as $child_key )
1257          {
1258              if ( $next )
1259              {
1260                  $ret =& $children[$child_key];
1261                  break;
1262              }
1263              elseif ( $children[$child_key]->flag === true )
1264              {
1265                  $next = true;
1266              }
1267          }
1268          $this->flag = false;
1269  
1270          return $ret;
1271      }
1272  
1273     /*!
1274        \note emulation of W3C DOM property
1275      */
1276  
1277      function &previousSibling()
1278      {
1279          $ret = null;
1280          if ( !$this->parentNode )
1281              return $ret;
1282  
1283          $parent =& $this->parentNode;
1284          $this->flag = true;
1285  
1286          $prev = false;
1287          $children =& $parent->Children;
1288          foreach( array_keys( $children ) as $child_key )
1289          {
1290              if ( $prev !== false && $children[$child_key]->flag === true )
1291              {
1292                  $ret =& $children[$prev];
1293                  break;
1294              }
1295              $prev = $child_key;
1296          }
1297          $this->flag = false;
1298          return $ret;
1299      }
1300  
1301      /*!
1302        Outputs DOM subtree to the debug output in the easy readable form.
1303        
1304        \param node  subtree root node
1305      */
1306  
1307      function writeDebug( &$node, $text, $showAttributes = false, $showParent = false )
1308      {
1309          if ( !$node )
1310              $node =& $this;
1311  
1312          if ( $node )
1313          {
1314              if ( get_class( $node ) == 'ezdomnode' )
1315              {
1316                  $d = eZDOMNode::debugNode( $node, $showAttributes, $showParent );
1317                  eZDebug::writeDebug( $d, $text );
1318              }
1319              else
1320                  eZDebug::writeDebug( $node, $text );
1321          }
1322          else
1323          {
1324              eZDebug::writeDebug( array( $node ), $text );
1325          }
1326      }
1327  
1328      function debugNode( &$node, $showAttributes, $showParent )
1329      {
1330          $d = array();
1331          $d['name'] = $node->nodeName;
1332          if ( $node->nodeName == '#text' )
1333              $d['text'] = $node->content;
1334          else if ( $node->Type == 2 )
1335              $d['value'] = $node->value;
1336  
1337          if ( $showParent )
1338             $d['parent'] = $node->parentNode->nodeName;
1339  
1340          if ( count( $node->Children ) )
1341          {
1342              $d['children'] = array();
1343              foreach( array_keys($node->Children) as $child_key )
1344              {
1345                  $d['children'][] = eZDOMNode::debugNode( $node->Children[$child_key], $showAttributes, $showParent );
1346              }
1347          }
1348  
1349          if ( $showAttributes && count( $node->Attributes ) )
1350          {
1351              $d['attributes'] = array();
1352              foreach( array_keys($node->Attributes) as $attr_key )
1353              {
1354                  $d['attributes'][] = eZDOMNode::debugNode( $node->Attributes[$attr_key], $showAttributes, $showParent );
1355              }
1356          }
1357          return $d;
1358      }
1359  
1360      /*!
1361        Outputs XML from DOM as a string.
1362        
1363        \param node  subtree root node
1364      */
1365      function writeDebugStr( &$node, $text )
1366      {
1367          if ( is_object( $node ) )
1368              eZDebug::writeDebug( $node->toString( 0 ), $text );
1369          else
1370              eZDebug::writeDebug( $node, $text );
1371      }
1372  
1373      /// \privatesection
1374  
1375      /// Name of the node
1376      var $Name = false;
1377  
1378      /// tagname, added for DOM XML compatibility.
1379      var $tagname = null;
1380      
1381      /// DOM W3C compatibility
1382      var $nodeName = null;
1383  
1384      /// Type of the DOM node. ElementNode=1, AttributeNode=2, TextNode=3, CDATASectionNode=4
1385      var $type;
1386      var $Type = EZ_XML_NODE_ELEMENT;
1387  
1388      /// Content of the node
1389      var $content = "";
1390      var $Content = "";
1391      var $value = '';
1392  
1393      /// Subnodes
1394      var $Children = array();
1395  
1396      /// Attributes
1397      var $Attributes = array();
1398  
1399      /// Contains the namespace URI. E.g. xmlns="http://ez.no/article/", http://ez.no/article/ would be the namespace URI
1400      var $NamespaceURI = false;
1401  
1402      /// The local part of a name. E.g: book:title, title is the local part
1403      var $LocalName = false;
1404  
1405      /// contains the namespace prefix. E.g: book:title, book is the prefix
1406      var $Prefix = false;
1407  
1408      /// Reference to the parent node.
1409      ///  Available only if Document has been created with parameter $setParentNode = true
1410      ///  or parsed with eZXML::domTree function with $params["SetParentNode"] = true
1411      var $parentNode = false;
1412  
1413      // temporary flag to mark node
1414      var $flag = false;
1415  }
1416  
1417  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7