Config
[ class tree: Config ] [ index: Config ] [ all elements ]

Source for file XML.php

Documentation is available at XML.php

  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.12 2004/08/07 10:11:27 mansion 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.         $this->folding = false;
  102.         $this->cdata = null;
  103.         $this->XML_Parser($this->options['encoding']'event');
  104.         $this->containers[0=$obj->container;
  105.         if (is_string($datasrc)) {
  106.             if ($this->options['isFile']{
  107.                 $err $this->setInputFile($datasrc);
  108.                 if (PEAR::isError($err)) {
  109.                     return $err;
  110.                 }
  111.                 $err $this->parse();
  112.             else {
  113.                 $err $this->parseString($datasrctrue);
  114.             }
  115.         else {
  116.            $this->setInput($datasrc);
  117.            $err $this->parse();
  118.         }
  119.         if (PEAR::isError($err)) {
  120.             return $err;
  121.         }
  122.         return true;
  123.     // end func parseDatasrc
  124.  
  125.     /**
  126.     * Handler for the xml-data
  127.     *
  128.     * @param mixed  $xp         ignored
  129.     * @param string $elem       name of the element
  130.     * @param array  $attribs    attributes for the generated node
  131.     *
  132.     * @access private
  133.     */
  134.     function startHandler($xp$elem&$attribs)
  135.     {
  136.         $container =new Config_Container('section'$elemnull$attribs);
  137.         $this->containers[=$container;
  138.         return null;
  139.     // end func startHandler
  140.  
  141.     /**
  142.     * Handler for the xml-data
  143.     *
  144.     * @param mixed  $xp         ignored
  145.     * @param string $elem       name of the element
  146.     *
  147.     * @access private
  148.     */
  149.     function endHandler($xp$elem)
  150.     {
  151.         $count count($this->containers);
  152.         $container =$this->containers[$count-1];
  153.         $currentSection =$this->containers[$count-2];
  154.         if (count($container->children== 0{
  155.             $container->setType('directive');
  156.             $container->setContent(trim($this->cdata));
  157.         }
  158.         $currentSection->addItem($container);
  159.         array_pop($this->containers);
  160.         $this->cdata = null;
  161.         return null;
  162.     // end func endHandler
  163.  
  164.     /*
  165.     * The xml character data handler
  166.     *
  167.     * @param mixed  $xp         ignored
  168.     * @param string $data       PCDATA between tags
  169.     *
  170.     * @access private
  171.     */
  172.     function cdataHandler($xp$cdata)
  173.     {
  174.         $this->cdata .= $cdata;
  175.     //  end func cdataHandler
  176.  
  177.     /**
  178.     * Returns a formatted string of the object
  179.     * @param    object  $obj    Container object to be output as string
  180.     * @access   public
  181.     * @return   string 
  182.     */
  183.     function toString(&$obj)
  184.     {
  185.         $indent '';
  186.         if (!$obj->isRoot()) {
  187.             // no indent for root
  188.             $this->_deep++;
  189.             $indent str_repeat($this->options['indent']$this->_deep);
  190.         else {
  191.             // Initialize string with xml declaration
  192.             $string '';
  193.             if ($this->options['addDecl']{
  194.                 $string .= XML_Util::getXMLDeclaration($this->options['version']$this->options['encoding']);
  195.                 $string .= $this->options['linebreak'];
  196.             }
  197.             if (!empty($this->options['name'])) {
  198.                 $string .= '<'.$this->options['name'].'>'.$this->options['linebreak'];
  199.                 $this->_deep++;
  200.                 $indent str_repeat($this->options['indent']$this->_deep);
  201.             }
  202.         }
  203.         if (!isset($string)) {
  204.             $string '';
  205.         }
  206.         switch ($obj->type{
  207.             case 'directive':
  208.                 $attributes ($this->options['useAttr']$obj->attributes : array();
  209.                 $string .= $indent.XML_Util::createTag($obj->name$attributes$obj->contentnull,
  210.                             ($this->options['useCData'? XML_UTIL_CDATA_SECTION : XML_UTIL_REPLACE_ENTITIES));
  211.                 $string .= $this->options['linebreak'];
  212.                 break;
  213.             case 'comment':
  214.                 $string .= $indent.'<!-- '.$obj->content.' -->';
  215.                 $string .= $this->options['linebreak'];
  216.                 break;
  217.             case 'section':
  218.                 if (!$obj->isRoot()) {
  219.                     $string $indent.'<'.$obj->name;
  220.                     $string .= ($this->options['useAttr']? XML_Util::attributesToString($obj->attributes'';
  221.                 }
  222.                 if ($children count($obj->children)) {
  223.                     if (!$obj->isRoot()) {
  224.                         $string .= '>'.$this->options['linebreak'];
  225.                     }
  226.                     for ($i = 0; $i $children$i++{
  227.                         $string .= $this->toString($obj->getChild($i));
  228.                     }
  229.                 }
  230.                 if (!$obj->isRoot()) {
  231.                     if ($children{
  232.                         $string .= $indent.'</'.$obj->name.'>'.$this->options['linebreak'];
  233.                     else {
  234.                         $string .= '/>'.$this->options['linebreak'];
  235.                     }
  236.                 else {
  237.                     if (!empty($this->options['name'])) {
  238.                         $string .= '</'.$this->options['name'].'>'.$this->options['linebreak'];
  239.                     }
  240.                 }
  241.                 break;
  242.             default:
  243.                 $string '';
  244.         }
  245.         if (!$obj->isRoot()) {
  246.             $this->_deep--;
  247.         }
  248.         return $string;
  249.     // end func toString
  250. // end class Config_Container_XML
  251. ?>

Documentation generated on Mon, 11 Mar 2019 14:23:50 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.