Author: kn Date: Wed Feb 6 17:51:32 2008 New Revision: 7316 Log: - Readded test for ezp3 -> ezp4 conversion.
Added: experimental/Document/src/options/converter_base.php (with props) experimental/Document/src/options/converter_ezp3_ezp4.php (with props) experimental/Document/tests/converter_ezp3_ezp4_test.php (with props) Modified: experimental/Document/design/class_diagram.png experimental/Document/src/converters/xslt.php experimental/Document/src/converters/xslt/ezp3_ezp4.php experimental/Document/src/converters/xslt/ezp3_ezp4.xsl experimental/Document/src/document/xml_base.php experimental/Document/src/document_autoload.php experimental/Document/src/interfaces/converter.php experimental/Document/src/interfaces/document.php experimental/Document/tests/suite.php Modified: experimental/Document/design/class_diagram.png ============================================================================== Binary files - no diff available. Modified: experimental/Document/src/converters/xslt.php ============================================================================== --- experimental/Document/src/converters/xslt.php [iso-8859-1] (original) +++ experimental/Document/src/converters/xslt.php [iso-8859-1] Wed Feb 6 17:51:32 2008 @@ -20,6 +20,22 @@ abstract class ezcDocumentXsltConverter extends ezcDocumentConverter { /** + * Path to XSLT transformation file + * + * @var string + */ + protected $xslt; + + /** + * XSLT processor created from the defined XSLT file. + * + * @var XSLTProcessor + */ + protected $xsltProcessor = null; + + protected static $customInlineTags; + + /** * Construct converter * * Construct converter from XSLT file, which is used for the actual @@ -30,6 +46,11 @@ */ public function __construct( $xslt ) { + if ( !ezcBaseFeatures::hasExtensionSupport( 'xsl' ) ) + { + throw new ezcBaseExtensionNotFoundException( 'xsl' ); + } + if ( !file_exists( $xslt ) || !is_readable( $xslt ) ) { throw new ezcBaseFileNotFoundException( $xslt ); @@ -48,7 +69,23 @@ */ public function convert( $doc ) { - // @TODO: Implement + // Create XSLT processor, if not yet initialized + if ( $this->xsltProcessor === null ) + { + $stylesheet = new DOMDocument(); + $stylesheet->load( $this->xslt ); + + $this->xsltProcessor = new XSLTProcessor(); + $this->xsltProcessor->importStyleSheet( $stylesheet ); + } + + // Transform input document + $document = $this->xsltProcessor->transformToDoc( $doc->getDomDocument() ); + + // Create destination document from DOMDocument + $dest = new ezcDocumentEzp4Xml(); + $dest->load( $document ); + return $dest; } } Modified: experimental/Document/src/converters/xslt/ezp3_ezp4.php ============================================================================== --- experimental/Document/src/converters/xslt/ezp3_ezp4.php [iso-8859-1] (original) +++ experimental/Document/src/converters/xslt/ezp3_ezp4.php [iso-8859-1] Wed Feb 6 17:51:32 2008 @@ -19,15 +19,73 @@ */ class ezcDocumentEzp3ToEzp4Converter extends ezcDocumentXsltConverter { + /** - * Construct converter + * Construct new document * - * @return void + * @param ezcFooBarOptions $options */ - public function __construct() + public function __construct( ezcDocumentEzp3ToEzp4ConverterOptions $options = null ) { + $this->options = ( $options === null ? + new ezcDocumentEzp3ToEzp4ConverterOptions() : + $options ); + // Define the conversion file to use. parent::__construct( dirname( __FILE__ ) . '/ezp3_ezp4.xsl' ); + } + + /** + * Convert documents between two formats + * + * Convert documents of the given type to the requested type. + * + * @param ezcDocumentXmlBase $doc + * @return ezcDocumentXmlBase + */ + public function convert( $doc ) + { + // Create XSLT processor, if not yet initialized + if ( $this->xsltProcessor === null ) + { + $stylesheet = new DOMDocument(); + $stylesheet->load( $this->xslt ); + + $this->xsltProcessor = new XSLTProcessor(); + $this->xsltProcessor->importStyleSheet( $stylesheet ); + $this->xsltProcessor->registerPHPFunctions(); + } + + // Update list with custom static tags, to make option available for + // static call from template + self::$customInlineTags = $this->options->customInlineTags; + + // Transform input document + $document = $this->xsltProcessor->transformToDoc( $doc->getDomDocument() ); + + // Create destination document from DOMDocument + $dest = new ezcDocumentEzp4Xml(); + $dest->loadDomDocument( $document ); + return $dest; + } + + /** + * Prepare expression for XSLT + * + * Prepare a or concatenation of name matches as an XPath query for the + * XSLT stylesheet from the configured custom inline tags. + * + * @return string + */ + public static function getCustomInlineTags() + { + $tagList = ''; + foreach ( self::$customInlineTags as $tag ) + { + $tagList .= "@name = '$tag' OR "; + } + + return substr( $tagList, 0, -4 ); } /*** Modified: experimental/Document/src/converters/xslt/ezp3_ezp4.xsl ============================================================================== --- experimental/Document/src/converters/xslt/ezp3_ezp4.xsl [iso-8859-1] (original) +++ experimental/Document/src/converters/xslt/ezp3_ezp4.xsl [iso-8859-1] Wed Feb 6 17:51:32 2008 @@ -77,7 +77,7 @@ <!-- Common template for block tags --> <xsl:template match="embed|table|tr|td|th|ol|ul|li|literal|custom" mode='common'> <xsl:choose> - <xsl:when test="name() = 'custom' and dyn:evaluate(php:function('ezcDocumentEzp3ToEzp4::getInlineCustomTags'))"> + <xsl:when test="name() = 'custom' and dyn:evaluate(php:function('ezcDocumentEzp3ToEzp4Converter::getCustomInlineTags'))"> <xsl:call-template name='inline_common'/> </xsl:when> <xsl:when test="name() = 'embed'"> Modified: experimental/Document/src/document/xml_base.php ============================================================================== --- experimental/Document/src/document/xml_base.php [iso-8859-1] (original) +++ experimental/Document/src/document/xml_base.php [iso-8859-1] Wed Feb 6 17:51:32 2008 @@ -68,6 +68,20 @@ } /** + * Construct directly from DOMDocument + * + * To save execution time this method offers the construction of XML + * documents directly from a DOM document instance. + * + * @param DOMDocument $document + * @return void + */ + public function loadDomDocument( DOMDocument $document ) + { + $this->document = $document; + } + + /** * Return internal document * * Return internally used structure, which is an instance of DOMDocument in Modified: experimental/Document/src/document_autoload.php ============================================================================== --- experimental/Document/src/document_autoload.php [iso-8859-1] (original) +++ experimental/Document/src/document_autoload.php [iso-8859-1] Wed Feb 6 17:51:32 2008 @@ -10,29 +10,31 @@ */ return array( - 'ezcDocumentException' => 'Document/exceptions/exception.php', - 'ezcDocumentErrnousXmlException' => 'Document/exceptions/errnous_xml.php', - 'ezcDocument' => 'Document/interfaces/document.php', - 'ezcDocumentConverter' => 'Document/interfaces/converter.php', - 'ezcDocumentValidation' => 'Document/interfaces/validation.php', - 'ezcDocumentXmlBase' => 'Document/document/xml_base.php', - 'ezcDocumentXmlBaseOptions' => 'Document/options/document_xml_base.php', - 'ezcDocumentXsltConverter' => 'Document/converters/xslt.php', - 'ezcDocumentBaseOptions' => 'Document/options/document_base.php', - 'ezcDocumentDocbook' => 'Document/document/xml/docbook.php', - 'ezcDocumentDocbookOptions' => 'Document/options/document_docbook.php', - 'ezcDocumentEzp3ToEzp4Converter' => 'Document/converters/xslt/ezp3_ezp4.php', - 'ezcDocumentEzp3Xml' => 'Document/document/xml/ezp3.php', - 'ezcDocumentEzp3XmlOptions' => 'Document/options/document_ezp3.php', - 'ezcDocumentEzp4Xml' => 'Document/document/xml/ezp4.php', - 'ezcDocumentEzp4XmlOptions' => 'Document/options/document_ezp4.php', - 'ezcDocumentHtmlConversion' => 'Document/interfaces/conversions/html.php', - 'ezcDocumentManager' => 'Document/document_manager.php', - 'ezcDocumentRelaxNgValidator' => 'Document/validator/realxng.php', - 'ezcDocumentRst' => 'Document/document/rst.php', - 'ezcDocumentRstOptions' => 'Document/options/document_rst.php', - 'ezcDocumentXhtml' => 'Document/document/xml/xhtml.php', - 'ezcDocumentXhtmlOptions' => 'Document/options/document_xhtml.php', - 'ezcDocumentXhtmlToDocbookConverter' => 'Document/converters/xslt/xhtml_docbook.php', + 'ezcDocumentException' => 'Document/exceptions/exception.php', + 'ezcDocumentErrnousXmlException' => 'Document/exceptions/errnous_xml.php', + 'ezcDocument' => 'Document/interfaces/document.php', + 'ezcDocumentBaseOptions' => 'Document/options/document_base.php', + 'ezcDocumentConverter' => 'Document/interfaces/converter.php', + 'ezcDocumentValidation' => 'Document/interfaces/validation.php', + 'ezcDocumentConverterBaseOptions' => 'Document/options/converter_base.php', + 'ezcDocumentXmlBase' => 'Document/document/xml_base.php', + 'ezcDocumentXmlBaseOptions' => 'Document/options/document_xml_base.php', + 'ezcDocumentXsltConverter' => 'Document/converters/xslt.php', + 'ezcDocumentDocbook' => 'Document/document/xml/docbook.php', + 'ezcDocumentDocbookOptions' => 'Document/options/document_docbook.php', + 'ezcDocumentEzp3ToEzp4Converter' => 'Document/converters/xslt/ezp3_ezp4.php', + 'ezcDocumentEzp3ToEzp4ConverterOptions' => 'Document/options/converter_ezp3_ezp4.php', + 'ezcDocumentEzp3Xml' => 'Document/document/xml/ezp3.php', + 'ezcDocumentEzp3XmlOptions' => 'Document/options/document_ezp3.php', + 'ezcDocumentEzp4Xml' => 'Document/document/xml/ezp4.php', + 'ezcDocumentEzp4XmlOptions' => 'Document/options/document_ezp4.php', + 'ezcDocumentHtmlConversion' => 'Document/interfaces/conversions/html.php', + 'ezcDocumentManager' => 'Document/document_manager.php', + 'ezcDocumentRelaxNgValidator' => 'Document/validator/realxng.php', + 'ezcDocumentRst' => 'Document/document/rst.php', + 'ezcDocumentRstOptions' => 'Document/options/document_rst.php', + 'ezcDocumentXhtml' => 'Document/document/xml/xhtml.php', + 'ezcDocumentXhtmlOptions' => 'Document/options/document_xhtml.php', + 'ezcDocumentXhtmlToDocbookConverter' => 'Document/converters/xslt/xhtml_docbook.php', ); ?> Modified: experimental/Document/src/interfaces/converter.php ============================================================================== --- experimental/Document/src/interfaces/converter.php [iso-8859-1] (original) +++ experimental/Document/src/interfaces/converter.php [iso-8859-1] Wed Feb 6 17:51:32 2008 @@ -20,6 +20,25 @@ abstract class ezcDocumentConverter { /** + * XML document base options. + * + * @var ezcDocumentXmlBaseOptions + */ + protected $options; + + /** + * Construct new document + * + * @param ezcFooBarOptions $options + */ + public function __construct( ezcDocumentConverterOptions $options = null ) + { + $this->options = ( $options === null ? + new ezcDocumentConverterOptions() : + $options ); + } + + /** * Convert documents between two formats * * Convert documents of the given type to the requested type. @@ -28,6 +47,73 @@ * @return ezcDocument */ abstract public function convert( $doc ); + + /** + * Returns the value of the property $name. + * + * @throws ezcBasePropertyNotFoundException + * if the property $name does not exist + * @param string $name + * @ignore + */ + public function __get( $name ) + { + switch ( $name ) + { + case 'options': + return $this->options; + } + + throw new ezcBasePropertyNotFoundException( $name ); + } + + /** + * Sets the property $name to $value. + * + * @throws ezcBasePropertyNotFoundException + * if the property $name does not exist + * @throws ezcBaseValueException + * if $value is not accepted for the property $name + * @param string $name + * @param mixed $value + * @ignore + */ + public function __set( $name, $value ) + { + switch ( $name ) + { + case 'options': + if ( !( $value instanceof ezcDocumentConverterOptions ) ) + { + throw new ezcBaseValueException( 'options', $value, 'instanceof ezcDocumentConverterOptions' ); + } + + $this->options = $value; + break; + + default: + throw new ezcBasePropertyNotFoundException( $name ); + } + } + + /** + * Returns true if the property $name is set, otherwise false. + * + * @param string $name + * @return bool + * @ignore + */ + public function __isset( $name ) + { + switch ( $name ) + { + case 'options': + return true; + + default: + return false; + } + } } ?> Modified: experimental/Document/src/interfaces/document.php ============================================================================== --- experimental/Document/src/interfaces/document.php [iso-8859-1] (original) +++ experimental/Document/src/interfaces/document.php [iso-8859-1] Wed Feb 6 17:51:32 2008 @@ -158,9 +158,9 @@ switch ( $name ) { case 'options': - if ( !( $value instanceof ezcDocumentOptions ) ) + if ( !( $value instanceof ezcDocumentBaseOptions ) ) { - throw new ezcBaseValueException( 'options', $value, 'instanceof ezcDocumentOptions' ); + throw new ezcBaseValueException( 'options', $value, 'instanceof ezcDocumentBaseOptions' ); } $this->options = $value; @@ -189,7 +189,6 @@ return false; } } - } ?> Added: experimental/Document/src/options/converter_base.php ============================================================================== --- experimental/Document/src/options/converter_base.php (added) +++ experimental/Document/src/options/converter_base.php [iso-8859-1] Wed Feb 6 17:51:32 2008 @@ -1,0 +1,37 @@ +<?php +/** + * File containing the options class for the ezcDocumentConverter class + * + * @package Document + * @version //autogen// + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Class containing the basic options for the ezcDocumentConverter + * + * @package Document + * @version //autogen// + */ +class ezcDocumentConverterBaseOptions extends ezcBaseOptions +{ + + /** + * Sets the option $name to $value. + * + * @throws ezcBasePropertyNotFoundException + * if the property $name is not defined + * @throws ezcBaseValueException + * if $value is not correct for the property $name + * @param string $name + * @param mixed $value + * @ignore + */ + public function __set( $name, $value ) + { + throw new ezcBasePropertyNotFoundException( $name ); + } +} + +?> Propchange: experimental/Document/src/options/converter_base.php ------------------------------------------------------------------------------ svn:eol-style = native Added: experimental/Document/src/options/converter_ezp3_ezp4.php ============================================================================== --- experimental/Document/src/options/converter_ezp3_ezp4.php (added) +++ experimental/Document/src/options/converter_ezp3_ezp4.php [iso-8859-1] Wed Feb 6 17:51:32 2008 @@ -1,0 +1,68 @@ +<?php +/** + * File containing the options class for the ezcDocumentEzp3Xml class + * + * @package Document + * @version //autogen// + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Class containing the basic options for the ezcDocumentEzp3Xml class + * + * @property array $customInlineTags + * Array with custom inline tags + * + * @package Document + * @version //autogen// + */ +class ezcDocumentEzp3ToEzp4ConverterOptions extends ezcDocumentConverterBaseOptions +{ + /** + * Constructs an object with the specified values. + * + * @throws ezcBasePropertyNotFoundException + * if $options contains a property not defined + * @throws ezcBaseValueException + * if $options contains a property with a value not allowed + * @param array(string=>mixed) $options + */ + public function __construct( array $options = array() ) + { + $this->customInlineTags = array(); + + parent::__construct( $options ); + } + + /** + * Sets the option $name to $value. + * + * @throws ezcBasePropertyNotFoundException + * if the property $name is not defined + * @throws ezcBaseValueException + * if $value is not correct for the property $name + * @param string $name + * @param mixed $value + * @ignore + */ + public function __set( $name, $value ) + { + switch ( $name ) + { + case 'customInlineTags': + if ( !is_array( $value ) ) + { + throw new ezcBaseValueException( $name, $value, 'array' ); + } + + $this->properties[$name] = $value; + break; + + default: + parent::__set( $name, $value ); + } + } +} + +?> Propchange: experimental/Document/src/options/converter_ezp3_ezp4.php ------------------------------------------------------------------------------ svn:eol-style = native Added: experimental/Document/tests/converter_ezp3_ezp4_test.php ============================================================================== --- experimental/Document/tests/converter_ezp3_ezp4_test.php (added) +++ experimental/Document/tests/converter_ezp3_ezp4_test.php [iso-8859-1] Wed Feb 6 17:51:32 2008 @@ -1,0 +1,85 @@ +<?php +/** + * ezcDocumentConverterEzp3TpEzp4Tests + * + * @package Document + * @version //autogen// + * @subpackage Tests + * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Test suite for class. + * + * @package Document + * @subpackage Tests + */ +class ezcDocumentConverterEzp3TpEzp4Tests extends ezcTestCase +{ + public static function suite() + { + return new PHPUnit_Framework_TestSuite( __CLASS__ ); + } + + public static function getTestDocuments() + { + return array( + array( + dirname( __FILE__ ) . '/files/ezp3_sample.xml', + dirname( __FILE__ ) . '/files/ezp4_sample.xml', + ), + ); + } + + public function testExtensionMissingException() + { + if ( ezcBaseFeatures::hasExtensionSupport( 'xsl' ) ) + { + $this->markTestSkipped( 'You need XSLT support disabled for this test.' ); + } + + try + { + $converter = new ezcDocumentEzp3ToEzp4Converter(); + $this->fail( 'Expected ezcBaseExtensionNotFoundException.' ); + } + catch ( ezcBaseExtensionNotFoundException $e ) + { /* Expected */ } + } + + /** + * @TODO: We should test this in more fine steps, then just testing one + * single big document. + * + * @dataProvider getTestDocuments + */ + public function testLoadXmlDocumentFromFile( $from, $to ) + { + if ( !ezcBaseFeatures::hasExtensionSupport( 'xsl' ) ) + { + $this->markTestSkipped( 'You need XSLT support for this test.' ); + } + + $doc = new ezcDocumentEzp3Xml(); + $doc->loadFile( $from ); + + $converter = new ezcDocumentEzp3ToEzp4Converter(); + $converter->options->customInlineTags = array( 'sub' ); + $created = $converter->convert( $doc ); + + $this->assertTrue( + $created instanceof ezcDocumentEzp4Xml + ); + + $dest = new DOMDocument(); + $dest->load( $to ); + + $this->assertEquals( + $dest, + $created->getDomDocument() + ); + } +} + +?> Propchange: experimental/Document/tests/converter_ezp3_ezp4_test.php ------------------------------------------------------------------------------ svn:eol-style = native Modified: experimental/Document/tests/suite.php ============================================================================== --- experimental/Document/tests/suite.php [iso-8859-1] (original) +++ experimental/Document/tests/suite.php [iso-8859-1] Wed Feb 6 17:51:32 2008 @@ -19,6 +19,7 @@ require_once 'document_options_xml_base_test.php'; require_once 'document_xml_base_test.php'; +require_once 'converter_ezp3_ezp4_test.php'; class ezcDocumentSuite extends PHPUnit_Framework_TestSuite @@ -34,10 +35,11 @@ $this->setName( "Document" ); // $this->addTest( ezcDocumentConvertXhtmlTest::suite() ); -// $this->addTest( ezcDocumentConvertEzp3Test::suite() ); $this->addTest( ezcDocumentOptionsXmlBaseTests::suite() ); $this->addTest( ezcDocumentXmlBaseTests::suite() ); + + $this->addTest( ezcDocumentConverterEzp3TpEzp4Tests::suite() ); } } -- svn-components mailing list svn-components@lists.ez.no http://lists.ez.no/mailman/listinfo/svn-components