diff --git a/Classes/PHPWord/Template.php b/Classes/PHPWord/Template.php index a01a854166..d93cd0c6e3 100755 --- a/Classes/PHPWord/Template.php +++ b/Classes/PHPWord/Template.php @@ -64,7 +64,7 @@ public function __construct($strFilename) if ($this->_tempFileName !== false) { // Copy the source File to the temp File if (!copy($strFilename, $this->_tempFileName)) { - throw new PHPWord_Exception('Could not copy the template from ' . $strFilename . ' to ' . $this->_tempFileName . '.'); + throw new PHPWord_Exception("Could not copy the template from {$strFilename} to {$this->_tempFileName}."); } $this->_objZip = new ZipArchive(); @@ -75,6 +75,36 @@ public function __construct($strFilename) throw new PHPWord_Exception('Could not create temporary file with unique name in the default temporary directory.'); } } + + /** + * Applies XSL style sheet to template's parts + * + * @param DOMDocument &$xslDOMDocument + * @param array $xslOptions = array() + * @param string $xslOptionsURI = '' + */ + public function applyXslStyleSheet(&$xslDOMDocument, $xslOptions = array(), $xslOptionsURI = '') + { + $processor = new \XSLTProcessor(); + + $processor->importStylesheet($xslDOMDocument); + + if ($processor->setParameter($xslOptionsURI, $xslOptions) === false) { + throw new \Exception('Could not set values for the given XSL style sheet parameters.'); + } + + $xmlDOMDocument = new \DOMDocument(); + if ($xmlDOMDocument->loadXML($this->_documentXML) === false) { + throw new \Exception('Could not load XML from the given template.'); + } + + $xmlTransformed = $processor->transformToXml($xmlDOMDocument); + if ($xmlTransformed === false) { + throw new \Exception('Could not transform the given XML document.'); + } + + $this->_documentXML = $xmlTransformed; + } /** * Set a Template value @@ -134,4 +164,4 @@ public function save($strFilename) rename($this->_tempFileName, $strFilename); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/TemplateTest.php b/Tests/PHPWord/TemplateTest.php new file mode 100644 index 0000000000..643fa29ce1 --- /dev/null +++ b/Tests/PHPWord/TemplateTest.php @@ -0,0 +1,71 @@ +load( + \join(\DIRECTORY_SEPARATOR, + array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'passthrough.xsl')) + ); + + /* + * We have to use error control below, because XSLTProcessor::setParameter omits warning on failure. + * This warning fails the test. + */ + @$template->applyXslStyleSheet($xslDOMDocument, array(1 => 'somevalue')); + } + + /** + * @covers ::applyXslStyleSheet + * @expectedException Exception + * @expectedExceptionMessage Could not load XML from the given template. + * @test + */ + final public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplate() + { + $template = new PHPWord_Template( + \join(\DIRECTORY_SEPARATOR, + array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'corrupted_main_document_part.docx')) + ); + + $xslDOMDocument = new \DOMDocument(); + $xslDOMDocument->load( + \join(\DIRECTORY_SEPARATOR, + array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'passthrough.xsl')) + ); + + /* + * We have to use error control below, because DOMDocument::loadXML omits warning on failure. + * This warning fails the test. + */ + @$template->applyXslStyleSheet($xslDOMDocument); + } +} \ No newline at end of file diff --git a/Tests/_files/templates/blank.docx b/Tests/_files/templates/blank.docx new file mode 100644 index 0000000000..071b7f350d Binary files /dev/null and b/Tests/_files/templates/blank.docx differ diff --git a/Tests/_files/templates/corrupted_main_document_part.docx b/Tests/_files/templates/corrupted_main_document_part.docx new file mode 100644 index 0000000000..69712525a4 Binary files /dev/null and b/Tests/_files/templates/corrupted_main_document_part.docx differ diff --git a/Tests/_files/xsl/passthrough.xsl b/Tests/_files/xsl/passthrough.xsl new file mode 100644 index 0000000000..4ab21dd745 --- /dev/null +++ b/Tests/_files/xsl/passthrough.xsl @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file