[ Index ]
 

Code source de SPIP Agora 1.4

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

title

Body

[fermer]

/Agora1-4/ecrire/include/dxs/ -> PEARTree.php (source)

   1   <?php
   2  /*****************************************************
   3  * This file is part of Agora, web based content management system.
   4  *
   5  * Agora is free software; you can redistribute it and/or modify
   6  * it under the terms of the GNU General Public License as published by
   7  * the Free Software Foundation; version 2 of the License.
   8  *
   9  * Agora is distributed in the hope that it will be useful,
  10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  * GNU General Public License for more details (file "COPYING").
  13  *
  14  * Copyright © Arnaud Martin, Antoine Pitrou et Philippe Rivière.
  15  * List of authors detailed in "copyright_fr.html" file.
  16  * E-mail : agora@sig.premier-ministre.gouv.fr
  17  * Web site : http://www.agora.gouv.fr
  18  *****************************************************/
  19  // $Id$
  20  //
  21  
  22  require_once("XML/Parser.php");
  23  require_once("XML/Tree/Node.php");
  24  
  25  /**
  26  * PEAR::XML_Tree
  27  *
  28  * Purpose
  29  *
  30  *    Allows for the building of XML data structures
  31  *    using a tree representation, without the need
  32  *    for an extension like DOMXML.
  33  *
  34  * Example
  35  *
  36  *    $tree  = new XML_Tree;
  37  *    $root =& $tree->addRoot('root');
  38  *    $foo  =& $root->addChild('foo');
  39  *
  40  *    header('Content-Type: text/xml');
  41  *    $tree->dump();
  42  *
  43  * @author  Bernd Römer <berndr@bonn.edu>
  44  * @package XML
  45  * @version $Version$ - 1.0
  46  */
  47  class XML_Tree extends XML_Parser
  48  {
  49      /**
  50      * File Handle
  51      *
  52      * @var  ressource
  53      */
  54      var $file = NULL;
  55  
  56      /**
  57      * Filename
  58      *
  59      * @var  string
  60      */
  61      var $filename = '';
  62  
  63      /**
  64      * Namespace
  65      *
  66      * @var  array
  67      */
  68      var $namespace = array();
  69  
  70      /**
  71      * Root
  72      *
  73      * @var  object XML_Tree_Node
  74      */
  75      var $root = NULL;
  76  
  77      /**
  78      * XML Version
  79      *
  80      * @var  string
  81      */
  82      var $version = '1.0';
  83  
  84      /**
  85      * Constructor
  86      *
  87      * @param  string  Filename
  88      * @param  string  XML Version
  89      */
  90      function XML_Tree($filename = '', $version = '1.0') {
  91          $this->filename = $filename;
  92          $this->version  = $version;
  93      }
  94  
  95      /**
  96      * Add root node.
  97      *
  98      * @param  string  $name     name of root element
  99      * @return object XML_Tree_Node   reference to root node
 100      *
 101      * @access public
 102      */
 103      function &addRoot($name, $content = '', $attributes = array()) {
 104          $this->root = new XML_Tree_Node($name, $content, $attributes);
 105          return $this->root;
 106      }
 107  
 108      /**
 109      * @deprecated
 110      */
 111      function &add_root($name, $content = '', $attributes = array()) {
 112          return $this->addRoot($name, $content, $attributes);
 113      }
 114  
 115      /**
 116      * inserts a child/tree (child) into tree ($path,$pos) and
 117      * maintains namespace integrity
 118      *
 119      * @param array      $path           path to parent of child to remove
 120      * @param integer    $pos            position of child to be inserted in its parents children-list
 121      * @param mixed      $child          child-node (by XML_Tree,XML_Node or Name)
 122      * @param string     $content        content (text) for new node
 123      * @param array      $attributes     attribute-hash for new node
 124      *
 125      * @return object XML_Tree_Node inserted child (node)
 126      * @access public
 127      */
 128      function &insertChild($path,$pos,$child, $content = '', $attributes = array()) {
 129          // update namespace to maintain namespace integrity
 130          $count=count($path);
 131          foreach($this->namespace as $key => $val) {
 132              if ((array_slice($val,0,$count)==$path) && ($val[$count]>=$pos))
 133                  $this->namespace[$key][$count]++;
 134          }
 135  
 136          $parent=&$this->get_node_by_path($path);
 137          return($parent->insert_child($pos,$child,$content,$attributes));
 138      }
 139  
 140      /**
 141      * @deprecated
 142      */
 143      function &insert_child($path,$pos,$child, $content = '', $attributes = array()) {
 144          return $this->insertChild($path, $child, $content, $attributes);
 145      }
 146  
 147      /*
 148      * removes a child ($path,$pos) from tree ($path,$pos) and
 149      * maintains namespace integrity
 150      *
 151      * @param array      $path   path to parent of child to remove
 152      * @param integer    $pos    position of child in parents children-list
 153      *
 154      * @return object XML_Tree_Node parent whichs child was removed
 155      * @access public
 156      */
 157      function &removeChild($path,$pos) {
 158          // update namespace to maintain namespace integrity
 159          $count=count($path);
 160          foreach($this->namespace as $key => $val) {
 161              if (array_slice($val,0,$count)==$path) {
 162                  if ($val[$count]==$pos) { unset($this->namespace[$key]); break; }
 163                  if ($val[$count]>$pos)
 164                      $this->namespace[$key][$count]--;
 165              }
 166          }
 167  
 168          $parent=&$this->get_node_by_path($path);
 169          return($parent->remove_child($pos));
 170      }
 171  
 172      /**
 173      * @deprecated
 174      */
 175      function &remove_child($path, $pos) {
 176          return $this->removeChild($path, $pos);
 177      }
 178  
 179      /*
 180      * Maps a xml file to a objects tree
 181      *
 182      * @return mixed The objects tree (XML_tree or an Pear error)
 183      * @access public
 184      */
 185      function &getTreeFromFile ()
 186      {
 187          $this->folding = false;
 188          $this->XML_Parser(null, 'event');
 189          $err = $this->setInputFile($this->filename);
 190          if (PEAR::isError($err)) {
 191              return $err;
 192          }
 193          $this->cdata = null;
 194          $err = $this->parse();
 195          if (PEAR::isError($err)) {
 196              return $err;
 197          }
 198          return $this->root;
 199      }
 200  
 201      function getTreeFromString($str)
 202      {
 203          $this->folding = false;
 204          $this->XML_Parser(null, 'event');
 205          $this->cdata = null;
 206          $err = $this->parseString($str);
 207          if (PEAR::isError($err)) {
 208              return $err;
 209          }
 210          return $this->root;
 211      }
 212  
 213      /**
 214      * Handler for the xml-data
 215      *
 216      * @param mixed  $xp         ignored
 217      * @param string $elem       name of the element
 218      * @param array  $attribs    attributes for the generated node
 219      *
 220      * @access private
 221      */
 222      function startHandler($xp, $elem, &$attribs)
 223      {
 224          // root elem
 225          if (!isset($this->i)) {
 226              $this->obj1 =& $this->add_root($elem, null, $attribs);
 227              $this->i = 2;
 228          } else {
 229              // mixed contents
 230              if (!empty($this->cdata)) {
 231                  $parent_id = 'obj' . ($this->i - 1);
 232                  $parent    =& $this->$parent_id;
 233                  $parent->children[] = &new XML_Tree_Node(null, $this->cdata);
 234              }
 235              $obj_id = 'obj' . $this->i++;
 236              $this->$obj_id = &new XML_Tree_Node($elem, null, $attribs);
 237          }
 238          $this->cdata = null;
 239          return null;
 240      }
 241  
 242      /**
 243      * Handler for the xml-data
 244      *
 245      * @param mixed  $xp         ignored
 246      * @param string $elem       name of the element
 247      *
 248      * @access private
 249      */
 250      function endHandler($xp, $elem)
 251      {
 252          $this->i--;
 253          if ($this->i > 1) {
 254              $obj_id = 'obj' . $this->i;
 255              // recover the node created in StartHandler
 256              $node   =& $this->$obj_id;
 257              // mixed contents
 258              if (count($node->children) > 0) {
 259                  if (trim($this->cdata)) {
 260                      $node->children[] = &new XML_Tree_Node(null, $this->cdata);
 261                  }
 262              } else {
 263                  //$node->set_content($this->cdata);\
 264          //XXX MACIEK BOROWKA 27.04.2004
 265          $node->content = $this->cdata;
 266              }
 267              $parent_id = 'obj' . ($this->i - 1);
 268              $parent    =& $this->$parent_id;
 269              // attach the node to its parent node children array
 270              $parent->children[] = $node;
 271          }
 272          $this->cdata = null;
 273          return null;
 274      }
 275  
 276      /*
 277      * The xml character data handler
 278      *
 279      * @param mixed  $xp         ignored
 280      * @param string $data       PCDATA between tags
 281      *
 282      * @access private
 283      */
 284      function cdataHandler($xp, $data)
 285      {
 286          //echo "<p>CDATA: ".$data."<p>";
 287      
 288          //XXX MACIEK BOROWKA 20.04.2004
 289          //if (trim($data) != '') {
 290              $this->cdata .= $data;
 291          //}
 292      }
 293  
 294      /**
 295      * Get a copy of this tree.
 296      *
 297      * @return object XML_Tree
 298      * @access public
 299      */
 300      function clone() {
 301          $clone=new XML_Tree($this->filename,$this->version);
 302          $clone->root=$this->root->clone();
 303  
 304          // clone all other vars
 305          $temp=get_object_vars($this);
 306          foreach($temp as $varname => $value)
 307              if (!in_array($varname,array('filename','version','root')))
 308                  $clone->$varname=$value;
 309  
 310          return($clone);
 311      }
 312  
 313      /**
 314      * Print text representation of XML tree.
 315      *
 316      * @access public
 317      */
 318      function dump() {
 319          echo $this->get();
 320      }
 321  
 322      /**
 323      * Get text representation of XML tree.
 324      *
 325      * @return  string  XML
 326      * @access public
 327      */
 328      function &get() {
 329          $out = '<?xml version="' . $this->version . "\"?>\n";
 330          $out .= $this->root->get();
 331  
 332          return $out;
 333      }
 334  
 335      /**
 336      * Get current namespace.
 337      *
 338      * @param  string  $name namespace
 339      * @return string
 340      *
 341      * @access public
 342      */
 343      function &getName($name) {
 344          return $this->root->get_element($this->namespace[$name]);
 345      }
 346  
 347      /**
 348      * @deprecated
 349      */
 350      function &get_name($name) {
 351          return $this->getName($name);
 352      }
 353  
 354      /**
 355      * Register a namespace.
 356      *
 357      * @param  string  $name namespace
 358      * @param  string  $path path
 359      *
 360      * @access public
 361      */
 362      function registerName($name, $path) {
 363          $this->namespace[$name] = $path;
 364      }
 365  
 366      /**
 367      * @deprecated
 368      */
 369      function register_name($name, $path) {
 370          return $this->registerName($name, $path);
 371      }
 372  }
 373  ?>
 374  
 375  
 376  


Généré le : Sat Feb 24 14:40:03 2007 par Balluche grâce à PHPXref 0.7