\PhpOffice\Common\XMLReader -> \PhpOffice\PhpWord\Shared\XMLReader

This commit is contained in:
Libor M 2021-01-01 16:33:27 +01:00
parent 9a26ad9189
commit 0cca050bcd
15 changed files with 371 additions and 26 deletions

View File

@ -17,8 +17,8 @@
namespace PhpOffice\PhpWord\Reader;
use PhpOffice\Common\XMLReader;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Reader for ODText

View File

@ -17,9 +17,9 @@
namespace PhpOffice\PhpWord\Reader\ODText;
use PhpOffice\Common\XMLReader;
use PhpOffice\PhpWord\Element\TrackChange;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Content reader

View File

@ -17,8 +17,8 @@
namespace PhpOffice\PhpWord\Reader\ODText;
use PhpOffice\Common\XMLReader;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Meta reader

View File

@ -17,8 +17,8 @@
namespace PhpOffice\PhpWord\Reader;
use PhpOffice\Common\XMLReader;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
use PhpOffice\PhpWord\Shared\ZipArchive;
/**

View File

@ -17,12 +17,12 @@
namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\Common\XMLReader;
use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType;
use PhpOffice\PhpWord\Element\AbstractContainer;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Element\TrackChange;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Abstract part reader
@ -95,7 +95,7 @@ abstract class AbstractPart
/**
* Read w:p.
*
* @param \PhpOffice\Common\XMLReader $xmlReader
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
* @param \DOMElement $domNode
* @param \PhpOffice\PhpWord\Element\AbstractContainer $parent
* @param string $docPart
@ -202,7 +202,7 @@ abstract class AbstractPart
/**
* Read w:r.
*
* @param \PhpOffice\Common\XMLReader $xmlReader
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
* @param \DOMElement $domNode
* @param \PhpOffice\PhpWord\Element\AbstractContainer $parent
* @param string $docPart
@ -320,7 +320,7 @@ abstract class AbstractPart
/**
* Read w:tbl.
*
* @param \PhpOffice\Common\XMLReader $xmlReader
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
* @param \DOMElement $domNode
* @param mixed $parent
* @param string $docPart
@ -378,7 +378,7 @@ abstract class AbstractPart
/**
* Read w:pPr.
*
* @param \PhpOffice\Common\XMLReader $xmlReader
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
* @param \DOMElement $domNode
* @return array|null
*/
@ -413,7 +413,7 @@ abstract class AbstractPart
/**
* Read w:rPr
*
* @param \PhpOffice\Common\XMLReader $xmlReader
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
* @param \DOMElement $domNode
* @return array|null
*/
@ -459,7 +459,7 @@ abstract class AbstractPart
/**
* Read w:tblPr
*
* @param \PhpOffice\Common\XMLReader $xmlReader
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
* @param \DOMElement $domNode
* @return string|array|null
* @todo Capture w:tblStylePr w:type="firstRow"
@ -509,7 +509,7 @@ abstract class AbstractPart
/**
* Read w:tblpPr
*
* @param \PhpOffice\Common\XMLReader $xmlReader
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
* @param \DOMElement $domNode
* @return array
*/
@ -534,7 +534,7 @@ abstract class AbstractPart
/**
* Read w:tblInd
*
* @param \PhpOffice\Common\XMLReader $xmlReader
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
* @param \DOMElement $domNode
* @return TblWidthComplexType
*/
@ -552,7 +552,7 @@ abstract class AbstractPart
/**
* Read w:tcPr
*
* @param \PhpOffice\Common\XMLReader $xmlReader
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
* @param \DOMElement $domNode
* @return array
*/
@ -620,7 +620,7 @@ abstract class AbstractPart
/**
* Read style definition
*
* @param \PhpOffice\Common\XMLReader $xmlReader
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
* @param \DOMElement $parentNode
* @param array $styleDefs
* @ignoreScrutinizerPatch

View File

@ -17,8 +17,8 @@
namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\Common\XMLReader;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Core properties reader

View File

@ -17,9 +17,9 @@
namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\Common\XMLReader;
use PhpOffice\PhpWord\Metadata\DocInfo;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Custom properties reader

View File

@ -17,9 +17,9 @@
namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\Common\XMLReader;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Document reader
@ -97,7 +97,7 @@ class Document extends AbstractPart
/**
* Read w:sectPr
*
* @param \PhpOffice\Common\XMLReader $xmlReader
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
* @param \DOMElement $domNode
* @ignoreScrutinizerPatch
* @return array
@ -141,7 +141,7 @@ class Document extends AbstractPart
/**
* Read w:p node.
*
* @param \PhpOffice\Common\XMLReader $xmlReader
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
* @param \DOMElement $node
* @param \PhpOffice\PhpWord\Element\Section &$section
*
@ -170,7 +170,7 @@ class Document extends AbstractPart
/**
* Read w:sectPr node.
*
* @param \PhpOffice\Common\XMLReader $xmlReader
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
* @param \DOMElement $node
* @param \PhpOffice\PhpWord\Element\Section &$section
*/

View File

@ -17,8 +17,8 @@
namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\Common\XMLReader;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Footnotes reader

View File

@ -17,8 +17,8 @@
namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\Common\XMLReader;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Numbering reader
@ -89,7 +89,7 @@ class Numbering extends AbstractPart
/**
* Read numbering level definition from w:abstractNum and w:num
*
* @param \PhpOffice\Common\XMLReader $xmlReader
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
* @param \DOMElement $subnode
* @param int $levelId
* @return array

View File

@ -17,9 +17,9 @@
namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\Common\XMLReader;
use PhpOffice\PhpWord\ComplexType\TrackChangesView;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
use PhpOffice\PhpWord\Style\Language;
/**

View File

@ -17,8 +17,8 @@
namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\Common\XMLReader;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
use PhpOffice\PhpWord\Style\Language;
/**

View File

@ -0,0 +1,211 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2010-2018 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Shared;
/**
* XML Reader wrapper
*
* @since 0.2.1
*/
class XMLReader
{
/**
* DOMDocument object
*
* @var \DOMDocument
*/
private $dom = null;
/**
* DOMXpath object
*
* @var \DOMXpath
*/
private $xpath = null;
/**
* Get DOMDocument from ZipArchive
*
* @param string $zipFile
* @param string $xmlFile
* @return \DOMDocument|false
* @throws \Exception
*/
public function getDomFromZip($zipFile, $xmlFile)
{
if (file_exists($zipFile) === false) {
throw new \Exception('Cannot find archive file.');
}
$zip = new \ZipArchive();
$zip->open($zipFile);
$content = $zip->getFromName($xmlFile);
$zip->close();
if ($content === false) {
return false;
}
return $this->getDomFromString($content);
}
/**
* Get DOMDocument from content string
*
* @param string $content
* @return \DOMDocument
*/
public function getDomFromString($content)
{
$originalLibXMLEntityValue = libxml_disable_entity_loader(true);
$this->dom = new \DOMDocument();
$this->dom->loadXML($content);
libxml_disable_entity_loader($originalLibXMLEntityValue);
return $this->dom;
}
/**
* Get elements
*
* @param string $path
* @param \DOMElement $contextNode
* @return \DOMNodeList
*/
public function getElements($path, \DOMElement $contextNode = null)
{
if ($this->dom === null) {
return array();
}
if ($this->xpath === null) {
$this->xpath = new \DOMXpath($this->dom);
}
if (is_null($contextNode)) {
return $this->xpath->query($path);
}
return $this->xpath->query($path, $contextNode);
}
/**
* Registers the namespace with the DOMXPath object
*
* @param string $prefix The prefix
* @param string $namespaceURI The URI of the namespace
* @return bool true on success or false on failure
* @throws \InvalidArgumentException If called before having loaded the DOM document
*/
public function registerNamespace($prefix, $namespaceURI)
{
if ($this->dom === null) {
throw new \InvalidArgumentException('Dom needs to be loaded before registering a namespace');
}
if ($this->xpath === null) {
$this->xpath = new \DOMXpath($this->dom);
}
return $this->xpath->registerNamespace($prefix, $namespaceURI);
}
/**
* Get element
*
* @param string $path
* @param \DOMElement $contextNode
* @return \DOMElement|null
*/
public function getElement($path, \DOMElement $contextNode = null)
{
$elements = $this->getElements($path, $contextNode);
if ($elements->length > 0) {
return $elements->item(0);
}
return null;
}
/**
* Get element attribute
*
* @param string $attribute
* @param \DOMElement $contextNode
* @param string $path
* @return string|null
*/
public function getAttribute($attribute, \DOMElement $contextNode = null, $path = null)
{
$return = null;
if ($path !== null) {
$elements = $this->getElements($path, $contextNode);
if ($elements->length > 0) {
/** @var \DOMElement $node Type hint */
$node = $elements->item(0);
$return = $node->getAttribute($attribute);
}
} else {
if ($contextNode !== null) {
$return = $contextNode->getAttribute($attribute);
}
}
return ($return == '') ? null : $return;
}
/**
* Get element value
*
* @param string $path
* @param \DOMElement $contextNode
* @return string|null
*/
public function getValue($path, \DOMElement $contextNode = null)
{
$elements = $this->getElements($path, $contextNode);
if ($elements->length > 0) {
return $elements->item(0)->nodeValue;
}
return null;
}
/**
* Count elements
*
* @param string $path
* @param \DOMElement $contextNode
* @return integer
*/
public function countElements($path, \DOMElement $contextNode = null)
{
$elements = $this->getElements($path, $contextNode);
return $elements->length;
}
/**
* Element exists
*
* @param string $path
* @param \DOMElement $contextNode
* @return boolean
*/
public function elementExists($path, \DOMElement $contextNode = null)
{
return $this->getElements($path, $contextNode)->length > 0;
}
}

View File

@ -0,0 +1,134 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2010-2018 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Shared;
/**
* Test class for XMLReader
*
* @coversDefaultClass PhpOffice\PhpWord\Shared\XMLReader
*/
class XMLReaderTest extends \PHPUnit\Framework\TestCase
{
/**
* Test reading XML from string
*/
public function testDomFromString()
{
$reader = new XMLReader();
$reader->getDomFromString('<element attr="test"><child attr="subtest">AAA</child></element>');
$this->assertTrue($reader->elementExists('/element/child'));
$this->assertEquals('AAA', $reader->getElement('/element/child')->textContent);
$this->assertEquals('AAA', $reader->getValue('/element/child'));
$this->assertEquals('test', $reader->getAttribute('attr', $reader->getElement('/element')));
$this->assertEquals('subtest', $reader->getAttribute('attr', $reader->getElement('/element'), 'child'));
}
/**
* Test reading XML from zip
*/
public function testDomFromZip()
{
$archiveFile = __DIR__ . '/../_files/xml/reader.zip';
$reader = new XMLReader();
$reader->getDomFromZip($archiveFile, 'test.xml');
$this->assertTrue($reader->elementExists('/element/child'));
$this->assertFalse($reader->getDomFromZip($archiveFile, 'non_existing_xml_file.xml'));
}
/**
* Test that read from non existing archive throws exception
*
* @expectedException Exception
*/
public function testThrowsExceptionOnNonExistingArchive()
{
$archiveFile = __DIR__ . '/../_files/xml/readers.zip';
$reader = new XMLReader();
$reader->getDomFromZip($archiveFile, 'test.xml');
}
/**
* Test elements count
*/
public function testCountElements()
{
$reader = new XMLReader();
$reader->getDomFromString('<element attr="test"><child>AAA</child><child>BBB</child></element>');
$this->assertEquals(2, $reader->countElements('/element/child'));
}
/**
* Test read non existing elements
*/
public function testReturnNullOnNonExistingNode()
{
$reader = new XMLReader();
$this->assertEmpty($reader->getElements('/element/children'));
$reader->getDomFromString('<element><child>AAA</child></element>');
$this->assertNull($reader->getElement('/element/children'));
$this->assertNull($reader->getValue('/element/children'));
}
/**
* Test that xpath fails if custom namespace is not registered
*/
public function testShouldThrowExceptionIfNamespaceIsNotKnown()
{
try {
$reader = new XMLReader();
$reader->getDomFromString('<element><test:child xmlns:test="http://phpword.com/my/custom/namespace">AAA</test:child></element>');
$this->assertTrue($reader->elementExists('/element/test:child'));
$this->assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
$this->fail();
} catch (\Exception $e) {
$this->assertTrue(true);
}
}
/**
* Test reading XML with manually registered namespace
*/
public function testShouldParseXmlWithCustomNamespace()
{
$reader = new XMLReader();
$reader->getDomFromString('<element><test:child xmlns:test="http://phpword.com/my/custom/namespace">AAA</test:child></element>');
$reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
$this->assertTrue($reader->elementExists('/element/test:child'));
$this->assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
}
/**
* Test that xpath fails if custom namespace is not registered
*
* @expectedException InvalidArgumentException
*/
public function testShouldThowExceptionIfTryingToRegisterNamespaceBeforeReadingDoc()
{
$reader = new XMLReader();
$reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
}
}

Binary file not shown.