/usr/share/php/propel/parser/PropelXMLParser.php is in php-propel-runtime 1.6.9-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 | <?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
/**
* XML parser. Converts data between associative array and XML formats
*
* @author Francois Zaninotto
* @package propel.runtime.parser
*/
class PropelXMLParser extends PropelParser
{
/**
* Converts data from an associative array to XML.
*
* @param array $array Source data to convert
* @param string $rootElementName Name of the root element of the XML document
* @param string $charset Character set of the input data. Defaults to UTF-8.
*
* @return string Converted data, as an XML string
*/
public function fromArray($array, $rootElementName = 'data', $charset = null)
{
$rootNode = $this->getRootNode($rootElementName);
$this->arrayToDOM($array, $rootNode, $charset, false);
return $rootNode->ownerDocument->saveXML();
}
public function listFromArray($array, $rootElementName = 'data', $charset = null)
{
$rootNode = $this->getRootNode($rootElementName);
$this->arrayToDOM($array, $rootNode, $charset, true);
return $rootNode->ownerDocument->saveXML();
}
/**
* Create a DOMDocument and get the root DOMNode using a root element name
*
* @param string $rootElementName The Root Element Name
*
* @return DOMNode The root DOMNode
*/
protected function getRootNode($rootElementName = 'data')
{
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$rootElement = $xml->createElement($rootElementName);
$xml->appendChild($rootElement);
return $rootElement;
}
/**
* Alias for PropelXMLParser::fromArray()
*
* @param array $array Source data to convert
* @param string $rootElementName Name of the root element of the XML document
* @param string $charset Character set of the input data. Defaults to UTF-8.
*
* @return string Converted data, as an XML string
*/
public function toXML($array, $rootElementName = 'data', $charset = null)
{
return $this->fromArray($array, $rootElementName, $charset);
}
/**
* Alias for PropelXMLParser::listFromArray()
*
* @param array $array Source data to convert
* @param string $rootElementName Name of the root element of the XML document
* @param string $charset Character set of the input data. Defaults to UTF-8.
*
* @return string Converted data, as an XML string
*/
public function listToXML($array, $rootElementName = 'data', $charset = null)
{
return $this->listFromArray($array, $rootElementName, $charset);
}
/**
* @param array $array
* @param DOMElement $rootElement
* @param string $charset
* @param boolean $removeNumbersFromKeys
*
* @return DOMElement
*/
protected function arrayToDOM($array, $rootElement, $charset = null, $removeNumbersFromKeys = false)
{
foreach ($array as $key => $value) {
if ($removeNumbersFromKeys) {
$key = preg_replace('/[^a-z]/i', '', $key);
}
$element = $rootElement->ownerDocument->createElement($key);
if (is_array($value)) {
if (!empty($value)) {
$element = $this->arrayToDOM($value, $element, $charset);
}
} elseif (is_string($value)) {
$charset = $charset ? $charset : 'utf-8';
if (function_exists('iconv') && strcasecmp($charset, 'utf-8') !== 0 && strcasecmp($charset, 'utf8') !== 0) {
$value = iconv($charset, 'UTF-8', $value);
}
$value = htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
$child = $element->ownerDocument->createCDATASection($value);
$element->appendChild($child);
} else {
$child = $element->ownerDocument->createTextNode($value);
$element->appendChild($child);
}
$rootElement->appendChild($element);
}
return $rootElement;
}
/**
* Converts data from XML to an associative array.
*
* @param string $data Source data to convert, as an XML string
* @return array Converted data
*/
public function toArray($data)
{
$doc = new DomDocument('1.0', 'UTF-8');
$doc->loadXML($data);
$element = $doc->documentElement;
return $this->convertDOMElementToArray($element);
}
/**
* Alias for PropelXMLParser::toArray()
*
* @param string $data Source data to convert, as an XML string
* @return array Converted data
*/
public function fromXML($data)
{
return $this->toArray($data);
}
/**
* @param DOMNode $data
* @return array
*/
protected function convertDOMElementToArray(DOMNode $data)
{
$array = array();
$elementNames = array();
foreach ($data->childNodes as $element) {
if ($element->nodeType == XML_TEXT_NODE) {
continue;
}
$name = $element->nodeName;
if (isset($elementNames[$name])) {
if (isset($array[$name])) {
// change the first 'book' to 'book0'
$array[$name . $elementNames[$name]] = $array[$name];
unset($array[$name]);
}
$elementNames[$name] += 1;
$index = $name . $elementNames[$name];
} else {
$index = $name;
$elementNames[$name] = 0;
}
if ($element->hasChildNodes() && !$this->hasOnlyTextNodes($element)) {
$array[$index] = $this->convertDOMElementToArray($element);
} elseif ($element->hasChildNodes() && $element->firstChild->nodeType == XML_CDATA_SECTION_NODE) {
$array[$index] = htmlspecialchars_decode($element->firstChild->textContent);
} elseif (!$element->hasChildNodes()) {
$array[$index] = null;
} else {
$array[$index] = $element->textContent;
}
}
return $array;
}
/**
* @param DomNode $node
* @return boolean
*/
protected function hasOnlyTextNodes(DomNode $node)
{
foreach ($node->childNodes as $childNode) {
if ($childNode->nodeType != XML_CDATA_SECTION_NODE && $childNode->nodeType != XML_TEXT_NODE) {
return false;
}
}
return true;
}
}
|