[ Index ]
 

Code source de eZ Publish 3.9.0

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

title

Body

[fermer]

/extension/ezdhtml/lib/ -> advdomnode.php (source)

   1  <?php
   2  
   3  //
   4  // Advanced eZDOMNode and eZDOMDocument classes for compatibility with
   5  // eZ publish versions prior to 3.8
   6  //
   7  // Created on: <18-Nov-2005 10:10:02 ks>
   8  //
   9  // Copyright (C) 1999-2006 eZ systems as. All rights reserved.
  10  //
  11  
  12  
  13  include_once ( "lib/ezxml/classes/ezxml.php" );
  14  
  15  class advDOMDocument extends eZDOMDocument
  16  {
  17  
  18      /*
  19         Clean up memory. (destructor)
  20      */
  21      function cleanup()
  22      {
  23          if ( $this->Root )
  24              advDOMNode::cleanup( $this->Root );
  25      }
  26  
  27      // Class name replaced.
  28  
  29      function setRoot( &$node )
  30      {
  31         // if ( get_class( $node ) == "advdomnode" )
  32         // {
  33              $this->Root =& $node;
  34         // }
  35      }
  36  
  37      // \note W3C DOM function
  38  
  39      function appendChild( &$node )
  40      {
  41          //if ( get_class( $node ) == "advdomnode" )
  42         // {
  43              $this->Root =& $node;
  44         // }
  45      }
  46  
  47      function toString( $charset = true, $charsetConversion = true )
  48      {
  49          $charsetText = '';
  50          if ( $charset === true )
  51              $charset = 'UTF-8';
  52          if ( $charset !== false )
  53              $charsetText = " encoding=\"$charset\"";
  54          $text = "<?xml version=\"1.0\"$charsetText?>\n";
  55  
  56          if ( get_class( $this->Root ) == "advdomnode" )
  57          {
  58              $text .= $this->Root->toString( 0, $charset );
  59          }
  60  
  61          if ( $charsetConversion )
  62          {
  63              include_once ( 'lib/ezi18n/classes/eztextcodec.php' );
  64              $codec =& eZTextCodec::instance( false, $charset, false );
  65              if ( $codec )
  66              {
  67                  $text = $codec->convertString( $text );
  68              }
  69          }
  70  
  71          return $text;
  72      }
  73  
  74      // \note W3C DOM function
  75  
  76      function createTextNode( $text )
  77      {
  78          $text = preg_replace('/[\x00-\x08\x0b-\x0c\x0e-\x1f]/', '', $text);
  79  
  80          $node = new advDOMNode();
  81          $node->setName( "#text" );
  82          $node->setContent( $text );
  83          $node->setType( 3 );
  84  
  85          return $node;
  86      }
  87  
  88      // \note W3C DOM function
  89  
  90      function &createElement( $name )
  91      {
  92          $node = new advDOMNode();
  93          $node->setName( $name );
  94          $node->setType( 1 );
  95  
  96          $this->registerElement( $node );
  97  
  98          return $node;
  99      }
 100  
 101      // \note W3C DOM function
 102  
 103      function createAttributeNS( $namespaceURI, $qualifiedName )
 104      {
 105          list( $prefix, $name ) = explode( ':', $qualifiedName );
 106  
 107          $attr = new advDOMNode();
 108          $attr->setName( $name );
 109          $attr->setPrefix( $prefix );
 110          $attr->setNamespaceURI( $namespaceURI );
 111          $attr->setType( 2 );
 112          return $attr;
 113      }
 114  
 115      // \note W3C DOM function
 116  
 117      function createAttribute( $name )
 118      {
 119          $attr = new advDOMNode();
 120          $attr->setName( $name );
 121          $attr->setType( 2 );
 122          return $attr;
 123      }
 124  }
 125  
 126  class advDOMNode extends eZDOMNode
 127  {
 128      function dn2( $node, $text )
 129      {
 130          if ( !$node )
 131              $node =& $this;
 132          
 133          $parentName = $node->parentNode->nodeName;
 134          unset( $node->parentNode );
 135          $node->parentNode = $parentName;
 136          $children = array();
 137          if ( count( $node->Children ) )
 138          {
 139              foreach( array_keys( $node->Children ) as $key )
 140              {
 141                  $child =& $node->Children[$key];
 142                  if ( $child->nodeName == '#text' )
 143                      $children[$key] = $child->content;
 144                  else
 145                      $children[$key] = $child->nodeName;
 146              }
 147              unset( $node->Children );
 148              $node->Children = $children;
 149          }
 150          eZDebug::writeDebug( $node, $text );
 151      }
 152  
 153  
 154      function advDOMNode()
 155      {
 156          $this->Content =& $this->content;
 157          $this->Type =& $this->type;
 158          $this->nodeName =& $this->Name;
 159      }
 160  
 161      function cloneNode()
 162      {
 163          $tmp = new advDOMNode();
 164          $tmp->Name = $this->Name;
 165          $tmp->Type = $this->Type;
 166          $tmp->Content = $this->Content;
 167          $tmp->Children = null;
 168          $tmp->Attributes = $this->Attributes;
 169          $tmp->NamespaceURI = $this->NamespaceURI;
 170          $tmp->LocalName = $this->LocalName;
 171          $tmp->Prefix = $this->Prefix;
 172  
 173          $tmp->parentNode = null;
 174          return $tmp;
 175      }
 176  
 177      /*!
 178        Subtree destructor. Needed to clean memory properly.
 179      */
 180      function cleanup( &$node )
 181      {
 182          if ( $node->hasChildren() )
 183          {
 184              foreach( array_keys( $node->Children ) as $key )
 185              {
 186                  $child =& $node->Children[$key];
 187                  if ( $child->hasChildren() )
 188                  {
 189                      $child->cleanup( $child );
 190                  }
 191  
 192              }
 193              $node->removeChildren();
 194          }
 195      }
 196  
 197      function hasChildNodes()
 198      {
 199          return count( $this->Children ) > 0;
 200      }
 201  
 202      /*!
 203        Appends the node \a $node as a child of the current node.
 204  
 205        \return The node that was just inserted or \c false if it failed to insert a node.
 206  
 207        \note This will only make sense for element nodes.
 208        \note W3C DOM function
 209      */
 210      function appendChild( &$node )
 211      {
 212  //        if ( get_class( $node ) == "advdomnode" )
 213  //        {
 214              $node->parentNode =& $this;
 215              $this->Children[] =& $node;
 216              //return $node;
 217  //        }
 218          //return false;
 219      }
 220  
 221      /*!
 222        Removes all child nodes from the current node.
 223  
 224        \note This will only make sense for element nodes.
 225      */
 226      function removeChildren()
 227      {
 228          foreach( array_keys( $this->Children ) as $key )
 229          {
 230             unset( $this->Children[$key]->parentNode );
 231             $this->Children[$key]->parentNode = null;
 232          }
 233          $this->Children = array();
 234      }
 235  
 236  
 237      /*!
 238        Removes child by the given child object.
 239  
 240        \note W3C DOM function
 241      */
 242      function removeChild( &$childToRemove )
 243      {
 244          unset( $childToRemove->parentNode );
 245          $childToRemove->parentNode = null;
 246          $child = $childToRemove;
 247  
 248          foreach ( array_keys( $this->Children ) as $key )
 249          {
 250              if ( $this->Children[$key]->parentNode === null )
 251              {
 252                  unset( $this->Children[$key] );
 253                  break;
 254              }
 255          }
 256  
 257          return $child;
 258      }
 259  
 260      /*!
 261        Replaces child by the new one given.
 262  
 263        \note W3C DOM function
 264      */
 265      function replaceChild( &$newChild, &$oldChild )
 266      {
 267          unset( $oldChild->parentNode );
 268          $oldChild->parentNode = null;
 269  
 270          $newChildren = array();
 271  
 272          foreach ( array_keys( $this->Children ) as $key )
 273          {
 274              if ( $this->Children[$key]->parentNode === null )
 275              {
 276                  $newChild->parentNode =& $this;
 277                  $newChildren[$key] =& $newChild;
 278              }
 279              else
 280              {
 281                  $newChildren[$key] =& $this->Children[$key];
 282              }
 283          }
 284          $this->Children =& $newChildren;
 285          return $oldChild;
 286      }
 287  
 288      /*!
 289        Replaces child by the new one given.
 290  
 291        \note W3C DOM function
 292      */
 293      function insertBefore( &$newNode, &$refNode )
 294      {
 295          unset( $refNode->parentNode );
 296          $refNode->parentNode = null;
 297  
 298          $newChildren = array();
 299  
 300          foreach ( array_keys( $this->Children ) as $key )
 301          {
 302              if ( $this->Children[$key]->parentNode === null )
 303              {
 304                  $newChildren[] =& $newNode;
 305                  $newNode->parentNode =& $this;
 306                  $refNode->parentNode =& $this;
 307              }
 308              $newChildren[] =& $this->Children[$key];
 309          }
 310          $this->Children =& $newChildren;
 311          return $newNode;
 312      }
 313  
 314      /*!
 315        \note emulation of W3C DOM property
 316      */
 317  
 318      function &nextSibling()
 319      {
 320          $ret = null;
 321          if ( !$this->parentNode )
 322              return $ret;
 323  
 324          $parent =& $this->parentNode;
 325          $this->flag = true;
 326  
 327          $next = false;
 328          $children =& $parent->Children;
 329  
 330          foreach( array_keys( $children ) as $child_key )
 331          {
 332              if ( $next )
 333              {
 334                  $ret =& $children[$child_key];
 335                  break;
 336              }
 337              elseif ( $children[$child_key]->flag === true )
 338              {
 339                  $next = true;
 340              }
 341          }
 342          $this->flag = false;
 343  
 344          return $ret;
 345      }
 346  
 347     /*!
 348        \note emulation of W3C DOM property
 349      */
 350  
 351      function &previousSibling()
 352      {
 353          $ret = null;
 354          if ( !$this->parentNode )
 355              return $ret;
 356  
 357          $parent =& $this->parentNode;
 358          $this->flag = true;
 359  
 360          $prev = false;
 361          $children =& $parent->Children;
 362          foreach( array_keys( $children ) as $child_key )
 363          {
 364              if ( $prev !== false && $children[$child_key]->flag === true )
 365              {
 366                  $ret =& $children[$prev];
 367                  break;
 368              }
 369              $prev = $child_key;
 370          }
 371          $this->flag = false;
 372          return $ret;
 373      }
 374  
 375      // Class name replaced.
 376  
 377      function appendAttribute( &$node )
 378      {
 379          //if ( get_class( $node ) == "advdomnode" )
 380          //{
 381              $this->Attributes[] =& $node;
 382              return $node;
 383          //}
 384          return false;
 385      }
 386  
 387      // \note W3C DOM function
 388  
 389      function setAttribute( $name, $value )
 390      {
 391          foreach ( $this->Attributes as $attribute )
 392          {
 393              if ( $attribute->name() == $name )
 394              {
 395                  $attribute->setContent( $value );
 396                  return $attribute;
 397              }
 398          }
 399  
 400          $attr = advDOMDocument::createAttribute( $name );
 401          $attr->setContent( $value );
 402          return $this->appendAttribute( $attr );
 403      }
 404  
 405      // \note W3C DOM function
 406  
 407      function setAttributeNS( $namespaceURI, $qualifiedName, $value )
 408      {
 409          foreach ( $this->Attributes as $attribute )
 410          {   
 411              if ( !$attribute->Prefix )
 412                  continue;
 413  
 414              $fullName = $attribute->Prefix . ':' . $attribute->LocalName;
 415              if ( $fullName == $qualifiedName )
 416              {
 417                  $attribute->setContent( $value );
 418                  return $attribute;
 419              }
 420          }
 421          $attr = advDOMDocument::createAttributeNS( $namespaceURI, $qualifiedName );
 422          $attr->setContent( $value );
 423          return $this->appendAttribute( $attr );
 424      }
 425  
 426      // \note W3C DOM function
 427      function getAttribute( $attributeName )
 428      {
 429          $returnValue = '';
 430          foreach ( $this->Attributes as $attribute )
 431          {
 432              if ( $attribute->name() == $attributeName && !$attribute->Prefix )
 433                  $returnValue = $attribute->Content;
 434          }
 435      
 436          return $returnValue;
 437      }
 438  
 439      // \note W3C DOM function
 440      function getAttributeNS( $namespaceURI, $localName )
 441      {
 442          $returnValue = '';
 443          foreach ( $this->Attributes as $attribute )
 444          {
 445              if ( $attribute->LocalName == $localName &&
 446                   $attribute->NamespaceURI == $namespaceURI )
 447                  $returnValue = $attribute->Content;
 448          }
 449      
 450          return $returnValue;
 451      }
 452  
 453      // \note W3C DOM function
 454      function removeAttribute( $name )
 455      {
 456          $removed = false;
 457          foreach( array_keys( $this->Attributes ) as $key )
 458          {
 459              if ( $this->Attributes[$key]->name() == $name && !$this->Attributes[$key]->Prefix )
 460              {
 461                  unset( $this->Attributes[$key] );
 462                  $removed = true;
 463              }
 464          }
 465          return $removed;
 466      }
 467  
 468      // \note W3C DOM function
 469      function removeAttributeNS( $namespaceURI, $localName )
 470      {
 471          $removed = false;
 472          foreach( array_keys( $this->Attributes ) as $key )
 473          {
 474              if ( $this->Attributes[$key]->LocalName == $localName &&
 475                   $this->Attributes[$key]->NamespaceURI == $namespaceURI )
 476              {
 477                  unset( $this->Attributes[$key] );
 478                  $removed = true;
 479              }
 480          }
 481          return $removed;
 482      }
 483  
 484  
 485      /*!
 486        \return The first child of the node or \c false if there are no children.
 487  
 488        \note This will only make sense for element nodes.
 489        \note W3C DOM function
 490      */
 491  
 492      function &firstChild()
 493      {
 494          if ( count( $this->Children ) == 0 )
 495          {
 496              $child = false;
 497              return $child;
 498          }
 499          reset( $this->Children );
 500          $key = key( $this->Children );
 501          $child =& $this->Children[$key];
 502  
 503          return $child;
 504      }
 505  
 506      /*!
 507       \return The last child node or \c false if there are no children.
 508  
 509        \note This will only make sense for element nodes.
 510      */
 511  
 512      function &lastChild()
 513      {
 514          if ( count( $this->Children ) == 0 )
 515          {
 516              $child = false;
 517              return $child;
 518          }
 519          end( $this->Children );
 520          $key = key( $this->Children );
 521          $child =& $this->Children[$key];
 522  
 523          return $child;
 524      }
 525  
 526      /*!
 527        \return The content() of the first child node or \c false if there are no children.
 528  
 529        \note This will only make sense for element nodes.
 530        \sa elementTextContentByName
 531      */
 532      function textContent()
 533      {
 534          return $this->collectTextContent( $this );
 535      }
 536  
 537      function collectTextContent( &$element )
 538      {
 539          $ret = '';
 540          if ( $element->Type == EZ_XML_NODE_TEXT )
 541          {
 542              $ret = $element->content();
 543          }
 544          else
 545          {
 546              if ( count( $element->Children ) > 0 )
 547              {
 548                  foreach( array_keys( $element->Children ) as $key )
 549                  {
 550                      $child =& $element->Children[$key];
 551                      $ret .= $this->collectTextContent( $child );
 552                  }
 553              }
 554          }
 555          return $ret;
 556      }
 557  
 558      /*!
 559        Outputs DOM subtree to the debug output in the easy readable form.
 560        
 561        \param node  subtree root node
 562      */
 563  
 564      function writeDebug( &$node, $text, $showAttributes = false, $showParent = false )
 565      {
 566          if ( !$node )
 567              $node =& $this;
 568  
 569          if ( $node )
 570          {
 571              if ( get_class( $node ) == 'advdomnode' )
 572              {
 573                  $d = eZDOMNode::debugNode( $node, $showAttributes, $showParent );
 574                  eZDebug::writeDebug( $d, $text );
 575              }
 576              else
 577                  eZDebug::writeDebug( '$node has bad class', $text );
 578          }
 579          else 
 580              eZDebug::writeDebug( '$node is null', $text );
 581      }
 582  
 583      function debugNode( &$node, $showAttributes, $showParent )
 584      {
 585          $d = array();
 586          $d['name'] = $node->nodeName;
 587          if ( $node->nodeName == '#text' )
 588              $d['text'] = $node->content;
 589          else if ( $node->Type == 2 )
 590              $d['value'] = $node->value;
 591  
 592          if ( $showParent )
 593             $d['parent'] = $node->parentNode->nodeName;
 594  
 595          if ( count( $node->Children ) )
 596          {
 597              $d['children'] = array();
 598              foreach( array_keys($node->Children) as $child_key )
 599              {
 600                  $d['children'][] = eZDOMNode::debugNode( $node->Children[$child_key], $showAttributes, $showParent );
 601              }
 602          }
 603  
 604          if ( $showAttributes && count( $node->Attributes ) )
 605          {
 606              $d['attributes'] = array();
 607              foreach( array_keys($node->Attributes) as $attr_key )
 608              {
 609                  $d['attributes'][] = eZDOMNode::debugNode( $node->Attributes[$attr_key], $showAttributes, $showParent );
 610              }
 611          }
 612          return $d;
 613      }
 614  
 615      function writeDebugStr( &$node, $text )
 616      {
 617          if ( is_object( $node ) )
 618              eZDebug::writeDebug( $node->toString( 0 ), $text );
 619          else
 620              eZDebug::writeDebug( $node, $text );
 621      }
 622  
 623      var $parentNode = null;
 624  //    var $nextSibling = null;
 625      var $nodeName = null;
 626  
 627      var $flag = false;
 628  }
 629  
 630  ?>


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