PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/pear/Config/Container/XML.php

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