|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of PHPWord - A pure PHP library for reading and writing |
| 4 | + * word processing documents. |
| 5 | + * |
| 6 | + * PHPWord is free software distributed under the terms of the GNU Lesser |
| 7 | + * General Public License version 3 as published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * For the full copyright and license information, please read the LICENSE |
| 10 | + * file that was distributed with this source code. For the full list of |
| 11 | + * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
| 12 | + * |
| 13 | + * @link https://github.com/PHPOffice/PHPWord |
| 14 | + * @copyright 2010-2014 PHPWord contributors |
| 15 | + * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 16 | + */ |
| 17 | + |
| 18 | +namespace PhpOffice\PhpWord\Shared; |
| 19 | + |
| 20 | +use PhpOffice\PhpWord\Exception\Exception; |
| 21 | + |
| 22 | +/** |
| 23 | + * XML Reader wrapper |
| 24 | + * |
| 25 | + * @since 0.10.0 |
| 26 | + */ |
| 27 | +class XMLReader |
| 28 | +{ |
| 29 | + /** |
| 30 | + * DOMDocument object |
| 31 | + * |
| 32 | + * @var \DOMDocument |
| 33 | + */ |
| 34 | + private $dom = null; |
| 35 | + |
| 36 | + /** |
| 37 | + * DOMXpath object |
| 38 | + * |
| 39 | + * @var \DOMXpath |
| 40 | + */ |
| 41 | + private $xpath = null; |
| 42 | + |
| 43 | + /** |
| 44 | + * Get DOMDocument from ZipArchive |
| 45 | + * |
| 46 | + * @param string $zipFile |
| 47 | + * @param string $xmlFile |
| 48 | + * @return \DOMDocument|false |
| 49 | + * @throws \PhpOffice\PhpWord\Exception\Exception |
| 50 | + */ |
| 51 | + public function getDomFromZip($zipFile, $xmlFile) |
| 52 | + { |
| 53 | + if (file_exists($zipFile) === false) { |
| 54 | + throw new Exception('Cannot find archive file.'); |
| 55 | + } |
| 56 | + |
| 57 | + $zip = new ZipArchive(); |
| 58 | + $zip->open($zipFile); |
| 59 | + $content = $zip->getFromName($xmlFile); |
| 60 | + $zip->close(); |
| 61 | + |
| 62 | + if ($content === false) { |
| 63 | + return false; |
| 64 | + } else { |
| 65 | + return $this->getDomFromString($content); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get DOMDocument from content string |
| 71 | + * |
| 72 | + * @param string $content |
| 73 | + * @return \DOMDocument |
| 74 | + */ |
| 75 | + public function getDomFromString($content) |
| 76 | + { |
| 77 | + $this->dom = new \DOMDocument(); |
| 78 | + $this->dom->loadXML($content); |
| 79 | + |
| 80 | + return $this->dom; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get elements |
| 85 | + * |
| 86 | + * @param string $path |
| 87 | + * @param \DOMElement $contextNode |
| 88 | + * @return \DOMNodeList |
| 89 | + */ |
| 90 | + public function getElements($path, \DOMElement $contextNode = null) |
| 91 | + { |
| 92 | + if ($this->dom === null) { |
| 93 | + return array(); |
| 94 | + } |
| 95 | + if ($this->xpath === null) { |
| 96 | + $this->xpath = new \DOMXpath($this->dom); |
| 97 | + // GT Mod - required for reading images |
| 98 | + $this->xpath->registerNamespace('a', 'http://schemas.openxmlformats.org/drawingml/2006/main'); |
| 99 | + $this->xpath->registerNamespace('pic', 'http://schemas.openxmlformats.org/drawingml/2006/picture'); |
| 100 | + } |
| 101 | + |
| 102 | + if (is_null($contextNode)) { |
| 103 | + return $this->xpath->query($path); |
| 104 | + } else { |
| 105 | + return $this->xpath->query($path, $contextNode); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Get element |
| 111 | + * |
| 112 | + * @param string $path |
| 113 | + * @param \DOMElement $contextNode |
| 114 | + * @return \DOMElement|null |
| 115 | + */ |
| 116 | + public function getElement($path, \DOMElement $contextNode = null) |
| 117 | + { |
| 118 | + $elements = $this->getElements($path, $contextNode); |
| 119 | + if ($elements->length > 0) { |
| 120 | + return $elements->item(0); |
| 121 | + } else { |
| 122 | + return null; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Get element attribute |
| 128 | + * |
| 129 | + * @param string $attribute |
| 130 | + * @param \DOMElement $contextNode |
| 131 | + * @param string $path |
| 132 | + * @return string|null |
| 133 | + */ |
| 134 | + public function getAttribute($attribute, \DOMElement $contextNode = null, $path = null) |
| 135 | + { |
| 136 | + $return = null; |
| 137 | + if ($path !== null) { |
| 138 | + $elements = $this->getElements($path, $contextNode); |
| 139 | + if ($elements->length > 0) { |
| 140 | + /** @var \DOMElement $node Type hint */ |
| 141 | + $node = $elements->item(0); |
| 142 | + $return = $node->getAttribute($attribute); |
| 143 | + } |
| 144 | + } else { |
| 145 | + if ($contextNode !== null) { |
| 146 | + $return = $contextNode->getAttribute($attribute); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return ($return == '') ? null : $return; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Get element value |
| 155 | + * |
| 156 | + * @param string $path |
| 157 | + * @param \DOMElement $contextNode |
| 158 | + * @return string|null |
| 159 | + */ |
| 160 | + public function getValue($path, \DOMElement $contextNode = null) |
| 161 | + { |
| 162 | + $elements = $this->getElements($path, $contextNode); |
| 163 | + if ($elements->length > 0) { |
| 164 | + return $elements->item(0)->nodeValue; |
| 165 | + } else { |
| 166 | + return null; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Count elements |
| 172 | + * |
| 173 | + * @param string $path |
| 174 | + * @param \DOMElement $contextNode |
| 175 | + * @return integer |
| 176 | + */ |
| 177 | + public function countElements($path, \DOMElement $contextNode = null) |
| 178 | + { |
| 179 | + $elements = $this->getElements($path, $contextNode); |
| 180 | + |
| 181 | + return $elements->length; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Element exists |
| 186 | + * |
| 187 | + * @param string $path |
| 188 | + * @param \DOMElement $contextNode |
| 189 | + * @return boolean |
| 190 | + */ |
| 191 | + public function elementExists($path, \DOMElement $contextNode = null) |
| 192 | + { |
| 193 | + return $this->getElements($path, $contextNode)->length > 0; |
| 194 | + } |
| 195 | +} |
0 commit comments