[ Index ]
 

Code source de PHP PEAR 1.4.5

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

title

Body

[fermer]

/Config/Container/ -> XML.php (source)

   1  <?php
   2  // +----------------------------------------------------------------------+
   3  // | PHP Version 4                                                        |
   4  // +----------------------------------------------------------------------+
   5  // | Copyright (c) 1997-2003 The PHP Group                                |
   6  // +----------------------------------------------------------------------+
   7  // | This source file is subject to version 2.0 of the PHP license,       |
   8  // | that is bundled with this package in the file LICENSE, and is        |
   9  // | available at through the world-wide-web at                           |
  10  // | http://www.php.net/license/2_02.txt.                                 |
  11  // | If you did not receive a copy of the PHP license and are unable to   |
  12  // | obtain it through the world-wide-web, please send a note to          |
  13  // | license@php.net so we can mail you a copy immediately.               |
  14  // +----------------------------------------------------------------------+
  15  // | Author: Bertrand Mansion <bmansion@mamasam.com>                      |
  16  // +----------------------------------------------------------------------+
  17  //
  18  // $Id: XML.php,v 1.15 2005/12/24 02:24:30 aashley Exp $
  19  
  20  require_once ('XML/Parser.php');
  21  require_once('XML/Util.php');
  22  
  23  /**
  24  * Config parser for XML Files
  25  *
  26  * @author      Bertrand Mansion <bmansion@mamasam.com>
  27  * @package     Config
  28  */
  29  class Config_Container_XML extends XML_Parser
  30  {
  31      /**
  32      * Deep level used for indentation
  33      *
  34      * @var  int
  35      * @access private
  36      */
  37      var $_deep = -1;
  38  
  39      /**
  40      * This class options:
  41      * version (1.0) : XML version
  42      * encoding (ISO-8859-1) : XML content encoding
  43      * name      : like in phparray, name of your config global entity
  44      * indent    : char used for indentation
  45      * linebreak : char used for linebreak
  46      * addDecl   : whether to add the xml declaration at beginning or not
  47      * useAttr   : whether to use the attributes
  48      * isFile    : whether the given content is a file or an XML string
  49      * useCData  : whether to surround data with <![CDATA[...]]>
  50      *
  51      * @var  array
  52      */
  53      var $options = array('version'   => '1.0',
  54                           'encoding'  => 'ISO-8859-1',
  55                           'name'      => '',
  56                           'indent'    => '  ',
  57                           'linebreak' => "\n",
  58                           'addDecl'   => true,
  59                           'useAttr'   => true,
  60                           'isFile'    => true,
  61                           'useCData'  => false);
  62  
  63      /**
  64      * Container objects
  65      *
  66      * @var  array
  67      */
  68      var $containers = array();
  69  
  70      /**
  71      * Constructor
  72      *
  73      * @access public
  74      * @param    string  $options    Options to be used by renderer
  75      *                               version     : (1.0) XML version
  76      *                               encoding    : (ISO-8859-1) XML content encoding
  77      *                               name        : like in phparray, name of your config global entity
  78      *                               indent      : char used for indentation
  79      *                               linebreak   : char used for linebreak
  80      *                               addDecl     : whether to add the xml declaration at beginning or not
  81      *                               useAttr     : whether to use the attributes
  82      *                               isFile      : whether the given content is a file or an XML string
  83      */
  84      function Config_Container_XML($options = array())
  85      {
  86          foreach ($options as $key => $value) {
  87              $this->options[$key] = $value;
  88          }
  89      } // end constructor
  90  
  91      /**
  92      * Parses the data of the given configuration file
  93      *
  94      * @access public
  95      * @param string $datasrc    path to the configuration file
  96      * @param object $obj        reference to a config object
  97      * @return mixed returns a PEAR_ERROR, if error occurs or true if ok
  98      */
  99      function &parseDatasrc($datasrc, &$obj)
 100      {
 101          $err = true;
 102          $this->folding = false;
 103          $this->cdata = null;
 104          $this->XML_Parser($this->options['encoding'], 'event');
 105          $this->containers[0] =& $obj->container;
 106          if (is_string($datasrc)) {
 107              if ($this->options['isFile']) {
 108                  $err = $this->setInputFile($datasrc);
 109                  if (PEAR::isError($err)) {
 110                      return $err;
 111                  }
 112                  $err = $this->parse();
 113              } else {
 114                  $err = $this->parseString($datasrc, true);
 115              }
 116          } else {
 117             $this->setInput($datasrc);
 118             $err = $this->parse();
 119          }
 120          return $err;
 121      } // end func parseDatasrc
 122  
 123      /**
 124      * Handler for the xml-data
 125      *
 126      * @param mixed  $xp         ignored
 127      * @param string $elem       name of the element
 128      * @param array  $attribs    attributes for the generated node
 129      *
 130      * @access private
 131      */
 132      function startHandler($xp, $elem, &$attribs)
 133      {
 134          $container =& new Config_Container('section', $elem, null, $attribs);
 135          $this->containers[] =& $container;
 136          return null;
 137      } // end func startHandler
 138  
 139      /**
 140      * Handler for the xml-data
 141      *
 142      * @param mixed  $xp         ignored
 143      * @param string $elem       name of the element
 144      *
 145      * @access private
 146      */
 147      function endHandler($xp, $elem)
 148      {
 149          $count = count($this->containers);
 150          $container =& $this->containers[$count-1];
 151          $currentSection =& $this->containers[$count-2];
 152          if (count($container->children) == 0) {
 153              $container->setType('directive');
 154              $container->setContent(trim($this->cdata));
 155          }
 156          $currentSection->addItem($container);
 157          array_pop($this->containers);
 158          $this->cdata = null;
 159          return null;
 160      } // end func endHandler
 161  
 162      /*
 163      * The xml character data handler
 164      *
 165      * @param mixed  $xp         ignored
 166      * @param string $data       PCDATA between tags
 167      *
 168      * @access private
 169      */
 170      function cdataHandler($xp, $cdata)
 171      {
 172          $this->cdata .= $cdata;
 173      } //  end func cdataHandler
 174  
 175      /**
 176      * Returns a formatted string of the object
 177      * @param    object  $obj    Container object to be output as string
 178      * @access   public
 179      * @return   string
 180      */
 181      function toString(&$obj)
 182      {
 183          $indent = '';
 184          if (!$obj->isRoot()) {
 185              // no indent for root
 186              $this->_deep++;
 187              $indent = str_repeat($this->options['indent'], $this->_deep);
 188          } else {
 189              // Initialize string with xml declaration
 190              $string = '';
 191              if ($this->options['addDecl']) {
 192                  $string .= XML_Util::getXMLDeclaration($this->options['version'], $this->options['encoding']);
 193                  $string .= $this->options['linebreak'];
 194              }
 195              if (!empty($this->options['name'])) {
 196                  $string .= '<'.$this->options['name'].'>'.$this->options['linebreak'];
 197                  $this->_deep++;
 198                  $indent = str_repeat($this->options['indent'], $this->_deep);
 199              }
 200          }
 201          if (!isset($string)) {
 202              $string = '';
 203          }
 204          switch ($obj->type) {
 205              case 'directive':
 206                  $attributes = ($this->options['useAttr']) ? $obj->attributes : array();
 207                  $string .= $indent.XML_Util::createTag($obj->name, $attributes, $obj->content, null,
 208                              ($this->options['useCData'] ? XML_UTIL_CDATA_SECTION : XML_UTIL_REPLACE_ENTITIES));
 209                  $string .= $this->options['linebreak'];
 210                  break;
 211              case 'comment':
 212                  $string .= $indent.'<!-- '.$obj->content.' -->';
 213                  $string .= $this->options['linebreak'];
 214                  break;
 215              case 'section':
 216                  if (!$obj->isRoot()) {
 217                      $string = $indent.'<'.$obj->name;
 218                      $string .= ($this->options['useAttr']) ? XML_Util::attributesToString($obj->attributes) : '';
 219                  }
 220                  if ($children = count($obj->children)) {
 221                      if (!$obj->isRoot()) {
 222                          $string .= '>'.$this->options['linebreak'];
 223                      }
 224                      for ($i = 0; $i < $children; $i++) {
 225                          $string .= $this->toString($obj->getChild($i));
 226                      }
 227                  }
 228                  if (!$obj->isRoot()) {
 229                      if ($children) {
 230                          $string .= $indent.'</'.$obj->name.'>'.$this->options['linebreak'];
 231                      } else {
 232                          $string .= '/>'.$this->options['linebreak'];
 233                      }
 234                  } else {
 235                      if (!empty($this->options['name'])) {
 236                          $string .= '</'.$this->options['name'].'>'.$this->options['linebreak'];
 237                      }
 238                  }
 239                  break;
 240              default:
 241                  $string = '';
 242          }
 243          if (!$obj->isRoot()) {
 244              $this->_deep--;
 245          }
 246          return $string;
 247      } // end func toString
 248  } // end class Config_Container_XML
 249  ?>


Généré le : Sun Feb 25 14:08:00 2007 par Balluche grâce à PHPXref 0.7