diff options
author | emkael <emkael@tlen.pl> | 2016-02-24 23:18:07 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2016-02-24 23:18:07 +0100 |
commit | 6f7fdef0f500cd4bb540affd3bc1482243f337c1 (patch) | |
tree | 4853eecd0769a903e6130c1896e1d070848150dd /lib/prado/framework/Web/UI/WebControls/TXmlTransform.php | |
parent | 61f2ea48a4e11cb5fb941b3783e19c9e9ef38a45 (diff) |
* Prado 3.3.0
Diffstat (limited to 'lib/prado/framework/Web/UI/WebControls/TXmlTransform.php')
-rw-r--r-- | lib/prado/framework/Web/UI/WebControls/TXmlTransform.php | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/lib/prado/framework/Web/UI/WebControls/TXmlTransform.php b/lib/prado/framework/Web/UI/WebControls/TXmlTransform.php new file mode 100644 index 0000000..e3c206f --- /dev/null +++ b/lib/prado/framework/Web/UI/WebControls/TXmlTransform.php @@ -0,0 +1,193 @@ +<?php +/** + * TXmlTransform class file + * + * @author Knut Urdalen <knut.urdalen@gmail.com> + * @author Qiang Xue <qiang.xue@gmail.com> + * @link https://github.com/pradosoft/prado + * @copyright Copyright © 2005-2015 The PRADO Group + * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT + * @package System.Web.UI.WebControls + */ + +/** + * TXmlTransform class + * + * TXmlTransform uses the PHP's XSL extension to perform + * {@link http://www.w3.org/TR/xslt XSL transformations} using the + * {@link http://xmlsoft.org/XSLT/ libxslt library}. + * + * To associate an XML style sheet with TXmlTransform set the + * {@link setTransformPath TransformPath} property to the namespace or path to the style sheet + * or set the {@link setTransformContent TransformContent} property to the XML style sheet + * data as a string. + * + * To associate the XML data to be transformed set the {@link setDocumentPath DocumentPath} + * property to the namespace or path to the XML document or set the + * {@link setDocumentContent DocumentContent} property to the XML data as a string. + * + * To add additional parameters to the transformation process you can use the {@link getParameters Parameters} + * property. + * + * @author Knut Urdalen <knut.urdalen@gmail.com> + * @author Qiang Xue <qiang.xue@gmail.com> + * @package System.Web.UI.WebControls + * @since 3.1 + */ +class TXmlTransform extends TControl { + + const EXT_XML_FILE = '.xml'; + const EXT_XSL_FILE = '.xsl'; + + /** + * Constructor + * + * Initializes the TXmlTransform object and ensure that the XSL extension is available + * @throws TConfigurationException If XSL extension is not available + */ + public function __construct() { + if(!class_exists('XSLTProcessor', false)) { + throw new TConfigurationException('xmltransform_xslextension_required'); + } + } + + /** + * @return string The path to the XML style sheet. + */ + public function getTransformPath() { + return $this->getViewState('TransformPath', ''); + } + + /** + * @param string The path to the XML style sheet. It must be in namespace format. + */ + public function setTransformPath($value) { + if(!is_file($value)) { + $value = Prado::getPathOfNamespace($value, self::EXT_XSL_FILE); + if($value === null) { + throw new TInvalidDataValueException('xmltransform_transformpath_invalid', $value); + } + } + $this->setViewState('TransformPath', $value, ''); + } + + /** + * @return string XML style sheet as string + */ + public function getTransformContent() { + return $this->getViewState('TransformContent', ''); + } + + /** + * @param string $value XML style sheet as string + */ + public function setTransformContent($value) { + $this->setViewState('TransformContent', $value, ''); + } + + /** + * @return string The path to the XML document. It must be in namespace format. + */ + public function getDocumentPath() { + return $this->getViewState('DocumentPath', ''); + } + + /** + * @param string Namespace or path to XML document + * @throws TInvalidDataValueException + */ + public function setDocumentPath($value) { + if(!is_file($value)) { + $value = Prado::getPathOfNamespace($value, self::EXT_XML_FILE); + if($value === null) { + throw new TInvalidDataValueException('xmltransform_documentpath_invalid', $value); + } + } + $this->setViewState('DocumentPath', $value, ''); + } + + /** + * @return string XML data + */ + public function getDocumentContent() { + return $this->getViewState('DocumentContent', ''); + } + + /** + * @param string $value XML data. If not empty, it takes precedence over {@link setDocumentPath DocumentPath}. + */ + public function setDocumentContent($value) { + $this->setViewState('DocumentContent', $value, ''); + } + + /** + * Returns the list of parameters to be applied to the transform. + * @return TAttributeCollection the list of custom parameters + */ + public function getParameters() { + if($params = $this->getViewState('Parameters',null)) { + return $params; + } else { + $params = new TAttributeCollection(); + $this->setViewState('Parameters', $params, null); + return $params; + } + } + + private function getTransformXmlDocument() { + if(($content = $this->getTransformContent()) !== '') { + $document = new DOMDocument(); + $document->loadXML($content); + return $document; + } else if(($path = $this->getTransformPath()) !== '') { + $document = new DOMDocument(); + $document->load($path); + return $document; + } else { + throw new TConfigurationException('xmltransform_transform_required'); + } + } + + private function getSourceXmlDocument() { + if(($content = $this->getDocumentContent()) !== '') { + $document = new DOMDocument(); + $document->loadXML($content); + return $document; + } else if(($path = $this->getDocumentPath()) !== '') { + $document = new DOMDocument(); + $document->load($path); + return $document; + } else { + return null; + } + } + + /** + * Performs XSL transformation and render the output. + * @param THtmlWriter The writer used for the rendering purpose + */ + public function render($writer) { + if(($document=$this->getSourceXmlDocument()) === null) { + $htmlWriter = Prado::createComponent($this->GetResponse()->getHtmlWriterType(), new TTextWriter()); + parent::render($htmlWriter); + $document = new DOMDocument(); + $document->loadXML($htmlWriter->flush()); + } + $stylesheet = $this->getTransformXmlDocument(); + + // Perform XSL transformation + $xslt = new XSLTProcessor(); + $xslt->importStyleSheet($stylesheet); + + // Check for parameters + $parameters = $this->getParameters(); + foreach($parameters as $name => $value) { + $xslt->setParameter('', $name, $value); + } + $output = $xslt->transformToXML($document); + + // Write output + $writer->write($output); + } +} + |