/usr/share/php/phpdocx/lib/log4php/configurators/LoggerConfigurationAdapterXML.php is in php-phpdocx 3.0+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | <?php
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package log4php
*/
/**
* Converts XML configuration files to a PHP array.
*
* @package log4php
* @subpackage configurators
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @version $Revision: 1394956 $
* @since 2.2
*/
class LoggerConfigurationAdapterXML implements LoggerConfigurationAdapter
{
/** Path to the XML schema used for validation. */
const SCHEMA_PATH = '/../xml/log4php.xsd';
private $config = array(
'appenders' => array(),
'loggers' => array(),
'renderers' => array(),
);
public function convert($url) {
$xml = $this->loadXML($url);
$this->parseConfiguration($xml);
// Parse the <root> node
if (isset($xml->root)) {
$this->parseRootLogger($xml->root);
}
// Process <logger> nodes
foreach($xml->logger as $logger) {
$this->parseLogger($logger);
}
// Process <appender> nodes
foreach($xml->appender as $appender) {
$this->parseAppender($appender);
}
// Process <renderer> nodes
foreach($xml->renderer as $rendererNode) {
$this->parseRenderer($rendererNode);
}
// Process <defaultRenderer> node
foreach($xml->defaultRenderer as $rendererNode) {
$this->parseDefaultRenderer($rendererNode);
}
return $this->config;
}
/**
* Loads and validates the XML.
* @param string $url Input XML.
*/
private function loadXML($url) {
if (!file_exists($url)) {
throw new LoggerException("File [$url] does not exist.");
}
libxml_clear_errors();
$oldValue = libxml_use_internal_errors(true);
// Load XML
$xml = @simplexml_load_file($url);
if ($xml === false) {
$errorStr = "";
foreach(libxml_get_errors() as $error) {
$errorStr .= $error->message;
}
throw new LoggerException("Error loading configuration file: " . trim($errorStr));
}
libxml_clear_errors();
libxml_use_internal_errors($oldValue);
return $xml;
}
/**
* Parses the <configuration> node.
*/
private function parseConfiguration(SimpleXMLElement $xml) {
$attributes = $xml->attributes();
if (isset($attributes['threshold'])) {
$this->config['threshold'] = (string) $attributes['threshold'];
}
}
/** Parses an <appender> node. */
private function parseAppender(SimpleXMLElement $node) {
$name = $this->getAttributeValue($node, 'name');
if (empty($name)) {
$this->warn("An <appender> node is missing the required 'name' attribute. Skipping appender definition.");
return;
}
$appender = array();
$appender['class'] = $this->getAttributeValue($node, 'class');
if (isset($node['threshold'])) {
$appender['threshold'] = $this->getAttributeValue($node, 'threshold');
}
if (isset($node->layout)) {
$appender['layout']= $this->parseLayout($node->layout, $name);
}
if (count($node->param) > 0) {
$appender['params'] = $this->parseParameters($node);
}
foreach($node->filter as $filterNode) {
$appender['filters'][] = $this->parseFilter($filterNode);
}
$this->config['appenders'][$name] = $appender;
}
/** Parses a <layout> node. */
private function parseLayout(SimpleXMLElement $node, $appenderName) {
$layout = array();
$layout['class'] = $this->getAttributeValue($node, 'class');
if (count($node->param) > 0) {
$layout['params'] = $this->parseParameters($node);
}
return $layout;
}
/** Parses any <param> child nodes returning them in an array. */
private function parseParameters($paramsNode) {
$params = array();
foreach($paramsNode->param as $paramNode) {
if (empty($paramNode['name'])) {
$this->warn("A <param> node is missing the required 'name' attribute. Skipping parameter.");
continue;
}
$name = $this->getAttributeValue($paramNode, 'name');
$value = $this->getAttributeValue($paramNode, 'value');
$params[$name] = $value;
}
return $params;
}
/** Parses a <root> node. */
private function parseRootLogger(SimpleXMLElement $node) {
$logger = array();
if (isset($node->level)) {
$logger['level'] = $this->getAttributeValue($node->level, 'value');
}
$logger['appenders'] = $this->parseAppenderReferences($node);
$this->config['rootLogger'] = $logger;
}
/** Parses a <logger> node. */
private function parseLogger(SimpleXMLElement $node) {
$logger = array();
$name = $this->getAttributeValue($node, 'name');
if (empty($name)) {
$this->warn("A <logger> node is missing the required 'name' attribute. Skipping logger definition.");
return;
}
if (isset($node->level)) {
$logger['level'] = $this->getAttributeValue($node->level, 'value');
}
if (isset($node['additivity'])) {
$logger['additivity'] = $this->getAttributeValue($node, 'additivity');
}
$logger['appenders'] = $this->parseAppenderReferences($node);
// Check for duplicate loggers
if (isset($this->config['loggers'][$name])) {
$this->warn("Duplicate logger definition [$name]. Overwriting.");
}
$this->config['loggers'][$name] = $logger;
}
/**
* Parses a <logger> node for appender references and returns them in an array.
*
* Previous versions supported appender-ref, as well as appender_ref so both
* are parsed for backward compatibility.
*/
private function parseAppenderReferences(SimpleXMLElement $node) {
$refs = array();
foreach($node->appender_ref as $ref) {
$refs[] = $this->getAttributeValue($ref, 'ref');
}
foreach($node->{'appender-ref'} as $ref) {
$refs[] = $this->getAttributeValue($ref, 'ref');
}
return $refs;
}
/** Parses a <filter> node. */
private function parseFilter($filterNode) {
$filter = array();
$filter['class'] = $this->getAttributeValue($filterNode, 'class');
if (count($filterNode->param) > 0) {
$filter['params'] = $this->parseParameters($filterNode);
}
return $filter;
}
/** Parses a <renderer> node. */
private function parseRenderer(SimpleXMLElement $node) {
$renderedClass = $this->getAttributeValue($node, 'renderedClass');
$renderingClass = $this->getAttributeValue($node, 'renderingClass');
$this->config['renderers'][] = compact('renderedClass', 'renderingClass');
}
/** Parses a <defaultRenderer> node. */
private function parseDefaultRenderer(SimpleXMLElement $node) {
$renderingClass = $this->getAttributeValue($node, 'renderingClass');
// Warn on duplicates
if(isset($this->config['defaultRenderer'])) {
$this->warn("Duplicate <defaultRenderer> node. Overwriting.");
}
$this->config['defaultRenderer'] = $renderingClass;
}
// ******************************************
// ** Helper methods **
// ******************************************
private function getAttributeValue(SimpleXMLElement $node, $name) {
return isset($node[$name]) ? (string) $node[$name] : null;
}
private function warn($message) {
trigger_error("log4php: " . $message, E_USER_WARNING);
}
}
|