diff options
author | Jeremias Maerki <jeremias@apache.org> | 2008-07-23 09:34:17 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2008-07-23 09:34:17 +0000 |
commit | 2d83907e2d37f04aa8bebd938b737189d59eb62d (patch) | |
tree | c42d29083d95e098f521591b156a3f1df55bf2b8 | |
parent | 8091bd11210ac68aa3289532643e42b66545a495 (diff) | |
download | xmlgraphics-fop-2d83907e2d37f04aa8bebd938b737189d59eb62d.tar.gz xmlgraphics-fop-2d83907e2d37f04aa8bebd938b737189d59eb62d.zip |
Improved/fixed font setup for painters. Reduced the whole thing to pass in a fully set-up FontInfo object to reduce dependencies on the font package to the necessary.
Fixed problem with some JAXP implementations for "lastProprties" in IFParser.
Support for XMP parsing added in IFParser.
Added round-trip testing for new IF.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign@679047 13f79535-47bb-0310-9956-ffa450edef68
16 files changed, 625 insertions, 268 deletions
@@ -878,25 +878,44 @@ RetroWeaver will be added here --> <test name="org.apache.fop.fotreetest.FOTreeTestSuite" todir="${junit.reports.dir}" outfile="TEST-FO-tree"/> </junit> </target> - <target name="junit-intermediate-format" depends="junit-compile, junit-layout" description="Runs FOP's intermediate format JUnit tests" if="xmlunit.present"> - <echo message="Running intermediate format tests"/> - <junit dir="${basedir}" haltonfailure="${junit.haltonfailure}" fork="${junit.fork}" errorproperty="fop.junit.error" failureproperty="fop.junit.failure"> - <sysproperty key="basedir" value="${basedir}"/> - <sysproperty key="jawa.awt.headless" value="true"/> - <sysproperty key="fop.layoutengine.disabled" value="${layoutengine.disabled}"/> - <sysproperty key="fop.layoutengine.testset" value="standard"/> - <formatter type="brief" usefile="false"/> - <formatter type="plain" usefile="true"/> - <formatter type="xml" usefile="true"/> - <classpath> - <pathelement location="${build.dir}/test-classes"/> - <path refid="libs-build-classpath"/> - <fileset dir="build"> - <include name="fop.jar"/> - </fileset> - </classpath> - <test name="org.apache.fop.intermediate.IntermediateFormatTestSuite" todir="${junit.reports.dir}" outfile="TEST-intermediate-format"/> - </junit> + <macrodef name="junit-run"> + <attribute name="title"/> + <element name="tests"/> + <sequential> + <echo message="Running @{title} tests..."/> + <junit dir="${basedir}" haltonfailure="${junit.haltonfailure}" fork="${junit.fork}" errorproperty="fop.junit.error" failureproperty="fop.junit.failure"> + <sysproperty key="basedir" value="${basedir}"/> + <sysproperty key="jawa.awt.headless" value="true"/> + <sysproperty key="fop.layoutengine.disabled" value="${layoutengine.disabled}"/> + <formatter type="brief" usefile="false"/> + <formatter type="plain" usefile="true"/> + <formatter type="xml" usefile="true"/> + <classpath> + <pathelement location="${build.dir}/test-classes"/> + <path refid="libs-build-tools-classpath"/> + <fileset dir="build"> + <include name="fop.jar"/> + </fileset> + </classpath> + <tests/> + </junit> + </sequential> + </macrodef> + <target name="junit-area-tree-xml-format" depends="junit-compile" description="Runs FOP's area tree XML format JUnit tests" if="xmlunit.present"> + <junit-run title="area tree XML format"> + <tests> + <sysproperty key="fop.layoutengine.testset" value="standard"/> + <test name="org.apache.fop.intermediate.AreaTreeXMLFormatTestSuite" todir="${junit.reports.dir}" outfile="TEST-area-tree-xml-format"/> + </tests> + </junit-run> + </target> + <target name="junit-intermediate-format" depends="junit-compile" description="Runs FOP's intermediate format JUnit tests" if="xmlunit.present"> + <junit-run title="intermediate format"> + <tests> + <sysproperty key="fop.layoutengine.testset" value="standard"/> + <test name="org.apache.fop.intermediate.IntermediateFormatTestSuite" todir="${junit.reports.dir}" outfile="TEST-intermediate-format"/> + </tests> + </junit-run> </target> <target name="junit-text-linebreak" depends="junit-compile" description="Runs FOP's JUnit unicode linebreak tests" if="junit.present"> <echo message="Running tests for Unicode UAX#14 support"/> @@ -916,7 +935,7 @@ RetroWeaver will be added here --> <test name="org.apache.fop.text.linebreak.LineBreakStatusTest" todir="${junit.reports.dir}"/> </junit> </target> - <target name="junit" depends="junit-userconfig, junit-basic, junit-transcoder, junit-text-linebreak, junit-layout, junit-fotree, junit-intermediate-format" description="Runs all of FOP's JUnit tests" if="junit.present"> + <target name="junit" depends="junit-userconfig, junit-basic, junit-transcoder, junit-text-linebreak, junit-layout, junit-fotree, junit-area-tree-xml-format, junit-intermediate-format" description="Runs all of FOP's JUnit tests" if="junit.present"> <fail><condition><or><isset property="fop.junit.error"/><isset property="fop.junit.failure"/><not><isset property="hyphenation.present"/></not></or></condition> NOTE: ************************************************************************** diff --git a/src/java/org/apache/fop/render/PrintRendererConfigurator.java b/src/java/org/apache/fop/render/PrintRendererConfigurator.java index 6849f867f..93c5353ba 100644 --- a/src/java/org/apache/fop/render/PrintRendererConfigurator.java +++ b/src/java/org/apache/fop/render/PrintRendererConfigurator.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -71,7 +71,7 @@ public class PrintRendererConfigurator extends AbstractRendererConfigurator /** * Builds a list of EmbedFontInfo objects for use with the setup() method. - * + * * @param renderer print renderer * @throws FOPException if something's wrong with the config data */ @@ -84,6 +84,19 @@ public class PrintRendererConfigurator extends AbstractRendererConfigurator PrintRenderer printRenderer = (PrintRenderer)renderer; FontResolver fontResolver = printRenderer.getFontResolver(); + + List embedFontInfoList = buildFontList(cfg, fontResolver); + printRenderer.addFontList(embedFontInfoList); + } + + /** + * Builds the font list from configuration. + * @param cfg the configuration object + * @param fontResolver a font resolver + * @return the list of {@code EmbedFontInfo} objects + * @throws FOPException if an error occurs while processing the configuration + */ + protected List buildFontList(Configuration cfg, FontResolver fontResolver) throws FOPException { FopFactory factory = userAgent.getFactory(); FontManager fontManager = factory.getFontManager(); if (fontResolver == null) { @@ -100,12 +113,12 @@ public class PrintRendererConfigurator extends AbstractRendererConfigurator if (fontCache != null && fontCache.hasChanged()) { fontCache.save(); } - printRenderer.addFontList(embedFontInfoList); + return embedFontInfoList; } /** * Builds a list of EmbedFontInfo objects for use with the setup() method. - * + * * @param cfg Configuration object * @param fontResolver the FontResolver to use * @param strict true if an Exception should be thrown if an error is found. @@ -285,7 +298,7 @@ public class PrintRendererConfigurator extends AbstractRendererConfigurator /** * Creates a new FontTriplet given a triple Configuration - * + * * @param tripletCfg a triplet configuration * @param strict use strict validation * @return a font triplet font key @@ -323,7 +336,7 @@ public class PrintRendererConfigurator extends AbstractRendererConfigurator /** * Returns a font info from a font node Configuration definition - * + * * @param fontCfg Configuration object (font node) * @param fontResolver font resolver used to resolve font * @param strict validate configuration strictly diff --git a/src/java/org/apache/fop/render/RendererFactory.java b/src/java/org/apache/fop/render/RendererFactory.java index 3bf4881b9..412f3e2cd 100644 --- a/src/java/org/apache/fop/render/RendererFactory.java +++ b/src/java/org/apache/fop/render/RendererFactory.java @@ -331,6 +331,7 @@ public class RendererFactory { IFPainterConfigurator configurator = maker.getConfigurator(userAgent); if (configurator != null) { configurator.configure(painter); + configurator.setupFontInfo(painter); } return painter; //} diff --git a/src/java/org/apache/fop/render/intermediate/AbstractBinaryWritingIFPainter.java b/src/java/org/apache/fop/render/intermediate/AbstractBinaryWritingIFPainter.java index 860de7946..7f82f3d93 100644 --- a/src/java/org/apache/fop/render/intermediate/AbstractBinaryWritingIFPainter.java +++ b/src/java/org/apache/fop/render/intermediate/AbstractBinaryWritingIFPainter.java @@ -23,7 +23,6 @@ import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.net.URL; -import java.util.List; import javax.xml.transform.Result; import javax.xml.transform.stream.StreamResult; @@ -31,13 +30,11 @@ import javax.xml.transform.stream.StreamResult; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; -import org.apache.fop.fonts.CustomFontCollection; import org.apache.fop.fonts.FontCollection; +import org.apache.fop.fonts.FontEventAdapter; import org.apache.fop.fonts.FontInfo; import org.apache.fop.fonts.FontManager; -import org.apache.fop.fonts.FontResolver; import org.apache.fop.fonts.base14.Base14FontCollection; -import org.apache.fop.render.DefaultFontResolver; /** * Abstract base class for binary-writing IFPainter implementations. @@ -52,12 +49,6 @@ public abstract class AbstractBinaryWritingIFPainter extends AbstractIFPainter { /** Font configuration */ protected FontInfo fontInfo; - /** Font resolver */ - protected FontResolver fontResolver = null; - - /** list of fonts */ - protected List/*<EmbedFontInfo>*/ embedFontInfoList = null; - /** {@inheritDoc} */ public void setResult(Result result) throws IFException { if (result instanceof StreamResult) { @@ -93,43 +84,6 @@ public abstract class AbstractBinaryWritingIFPainter extends AbstractIFPainter { } /** - * Adds a font list to current list of fonts - * @param fontList a font info list - */ - public void addFontList(List/*<EmbedFontInfo>*/ fontList) { - if (embedFontInfoList == null) { - setFontList(fontList); - } else { - embedFontInfoList.addAll(fontList); - } - } - - /** - * @param embedFontInfoList list of available fonts - */ - public void setFontList(List/*<EmbedFontInfo>*/ embedFontInfoList) { - this.embedFontInfoList = embedFontInfoList; - } - - /** - * @return list of available embedded fonts - */ - public List/*<EmbedFontInfo>*/ getFontList() { - return this.embedFontInfoList; - } - - /** - * Returns the {@code FontResolver} used by this painter. - * @return the font resolver - */ - public FontResolver getFontResolver() { - if (this.fontResolver == null) { - this.fontResolver = new DefaultFontResolver(getUserAgent()); - } - return this.fontResolver; - } - - /** * Returns the {@code FontInfo} object. * @return the font info */ @@ -137,23 +91,22 @@ public abstract class AbstractBinaryWritingIFPainter extends AbstractIFPainter { return this.fontInfo; } + /** {@inheritDoc} */ public void setFontInfo(FontInfo fontInfo) { this.fontInfo = fontInfo; } - /** - * Set up the font info - * - * @param inFontInfo font info to set up - */ - public void setupFontInfo(FontInfo inFontInfo) { - setFontInfo(inFontInfo); + /** {@inheritDoc} */ + public void setDefaultFontInfo() { FontManager fontManager = getUserAgent().getFactory().getFontManager(); FontCollection[] fontCollections = new FontCollection[] { - new Base14FontCollection(fontManager.isBase14KerningEnabled()), - new CustomFontCollection(getFontResolver(), getFontList()) + new Base14FontCollection(fontManager.isBase14KerningEnabled()) }; - fontManager.setup(getFontInfo(), fontCollections); + + FontInfo fi = new FontInfo(); + fi.setEventListener(new FontEventAdapter(getUserAgent().getEventBroadcaster())); + fontManager.setup(fi, fontCollections); + setFontInfo(fi); } /** {@inheritDoc} */ diff --git a/src/java/org/apache/fop/render/intermediate/AbstractXMLWritingIFPainter.java b/src/java/org/apache/fop/render/intermediate/AbstractXMLWritingIFPainter.java index bb226dd0c..4336b0bf9 100644 --- a/src/java/org/apache/fop/render/intermediate/AbstractXMLWritingIFPainter.java +++ b/src/java/org/apache/fop/render/intermediate/AbstractXMLWritingIFPainter.java @@ -37,6 +37,8 @@ import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; +import org.apache.fop.fonts.FontInfo; + /** * Abstract base class for XML-writing IFPainter implementations. */ @@ -66,6 +68,16 @@ public abstract class AbstractXMLWritingIFPainter extends AbstractIFPainter { } } + /** {@inheritDoc} */ + public void setFontInfo(FontInfo fontInfo) { + //nop, not used + } + + /** {@inheritDoc} */ + public void setDefaultFontInfo() { + //nop, not used + } + /** * Returns the main namespace used for generated XML content. * @return the main namespace diff --git a/src/java/org/apache/fop/render/intermediate/IFPainter.java b/src/java/org/apache/fop/render/intermediate/IFPainter.java index 44e02fe68..66d7a0750 100644 --- a/src/java/org/apache/fop/render/intermediate/IFPainter.java +++ b/src/java/org/apache/fop/render/intermediate/IFPainter.java @@ -28,6 +28,7 @@ import java.awt.geom.AffineTransform; import javax.xml.transform.Result; import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.fonts.FontInfo; /** * Interface used to paint whole documents layouted by Apache FOP. @@ -90,6 +91,17 @@ public interface IFPainter { void setResult(Result result) throws IFException; /** + * Sets the font set to work with. + * @param fontInfo the font info object + */ + void setFontInfo(FontInfo fontInfo); + + /** + * Sets the default font set (with no custom configuration). + */ + void setDefaultFontInfo(); + + /** * Indicates whether the painter supports to handle the pages in mixed order rather than * ascending order. * @return true if out-of-order handling is supported diff --git a/src/java/org/apache/fop/render/intermediate/IFPainterConfigurator.java b/src/java/org/apache/fop/render/intermediate/IFPainterConfigurator.java index e42f52665..71a3a484a 100644 --- a/src/java/org/apache/fop/render/intermediate/IFPainterConfigurator.java +++ b/src/java/org/apache/fop/render/intermediate/IFPainterConfigurator.java @@ -32,4 +32,11 @@ public interface IFPainterConfigurator { * @throws FOPException if an error occurs while configuring the object */ void configure(IFPainter painter) throws FOPException; + + /** + * Sets up the {@code FontInfo} object for the IFPainter. + * @param painter the painter instance + * @throws FOPException if an error occurs while configuring the object + */ + void setupFontInfo(IFPainter painter) throws FOPException; } diff --git a/src/java/org/apache/fop/render/intermediate/IFParser.java b/src/java/org/apache/fop/render/intermediate/IFParser.java index ba0a6c60b..6f85c5801 100644 --- a/src/java/org/apache/fop/render/intermediate/IFParser.java +++ b/src/java/org/apache/fop/render/intermediate/IFParser.java @@ -41,6 +41,7 @@ import org.w3c.dom.Document; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; import org.xml.sax.helpers.DefaultHandler; import org.apache.commons.logging.Log; @@ -49,7 +50,6 @@ import org.apache.commons.logging.LogFactory; import org.apache.fop.apps.FOUserAgent; import org.apache.fop.fo.ElementMappingRegistry; import org.apache.fop.fo.expr.PropertyException; -import org.apache.fop.fo.extensions.ExtensionAttachment; import org.apache.fop.util.ColorUtil; import org.apache.fop.util.ContentHandlerFactory; import org.apache.fop.util.ContentHandlerFactoryRegistry; @@ -167,7 +167,7 @@ public class IFParser implements IFConstants { delegate.startDocument(); delegate.startElement(uri, localName, qName, attributes); } else { - lastAttributes = attributes; + lastAttributes = new AttributesImpl(attributes); boolean handled = true; if (NAMESPACE.equals(uri)) { ElementHandler elementHandler = (ElementHandler)elementHandlers.get(localName); @@ -456,21 +456,13 @@ public class IFParser implements IFConstants { * Handles objects created by "sub-parsers" that implement the ObjectSource interface. * An example of object handled here are ExtensionAttachments. * @param obj the Object to be handled. + * @throws SAXException if an error occurs while handling the extension object */ - protected void handleExternallyGeneratedObject(Object obj) { - if (obj instanceof ExtensionAttachment) { - ExtensionAttachment attachment = (ExtensionAttachment)obj; - //TODO Implement me - /* - if (this.currentPageViewport == null) { - this.treeModel.handleOffDocumentItem( - new OffDocumentExtensionAttachment(attachment)); - } else { - this.currentPageViewport.addExtensionAttachment(attachment); - } - */ - } else { - log.warn("Don't know how to handle externally generated object: " + obj); + protected void handleExternallyGeneratedObject(Object obj) throws SAXException { + try { + painter.handleExtensionObject(obj); + } catch (IFException ife) { + handleIFException(ife); } } diff --git a/src/java/org/apache/fop/render/intermediate/IFRenderer.java b/src/java/org/apache/fop/render/intermediate/IFRenderer.java index c4243a86a..8df052663 100644 --- a/src/java/org/apache/fop/render/intermediate/IFRenderer.java +++ b/src/java/org/apache/fop/render/intermediate/IFRenderer.java @@ -165,10 +165,7 @@ public class IFRenderer extends AbstractPathOrientedRenderer { this.painter = new IFSerializer(); } this.painter.setUserAgent(getUserAgent()); - if (this.painter instanceof AbstractBinaryWritingIFPainter) { - //TODO THIS IS UGLY. FIX ME!!! - ((AbstractBinaryWritingIFPainter)this.painter).setFontInfo(fontInfo); - } + this.painter.setFontInfo(fontInfo); this.painter.setResult(result); } super.startRenderer(null); diff --git a/src/java/org/apache/fop/render/pdf/PDFPainter.java b/src/java/org/apache/fop/render/pdf/PDFPainter.java index 8d1b80c28..ec83db707 100644 --- a/src/java/org/apache/fop/render/pdf/PDFPainter.java +++ b/src/java/org/apache/fop/render/pdf/PDFPainter.java @@ -30,6 +30,8 @@ import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.xmlgraphics.xmp.Metadata; + import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.MimeConstants; import org.apache.fop.fo.extensions.xmp.XMPMetadata; @@ -423,6 +425,9 @@ public class PDFPainter extends AbstractBinaryWritingIFPainter { } private Typeface getTypeface(String fontName) { + if (fontName == null) { + throw new NullPointerException("fontName must not be null"); + } Typeface tf = (Typeface) fontInfo.getFonts().get(fontName); if (tf instanceof LazyFont) { tf = ((LazyFont)tf).getRealFont(); @@ -528,6 +533,9 @@ public class PDFPainter extends AbstractBinaryWritingIFPainter { public void handleExtensionObject(Object extension) throws IFException { if (extension instanceof XMPMetadata) { pdfUtil.renderXMPMetadata((XMPMetadata)extension); + } else if (extension instanceof Metadata) { + XMPMetadata wrapper = new XMPMetadata(((Metadata)extension)); + pdfUtil.renderXMPMetadata(wrapper); } else { throw new UnsupportedOperationException( "Don't know how to handle extension object: " + extension); diff --git a/src/java/org/apache/fop/render/pdf/PDFRendererConfigurator.java b/src/java/org/apache/fop/render/pdf/PDFRendererConfigurator.java index bee8e1175..2504a4457 100644 --- a/src/java/org/apache/fop/render/pdf/PDFRendererConfigurator.java +++ b/src/java/org/apache/fop/render/pdf/PDFRendererConfigurator.java @@ -27,10 +27,18 @@ import org.apache.avalon.framework.configuration.ConfigurationException; import org.apache.fop.apps.FOPException; import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.fonts.CustomFontCollection; +import org.apache.fop.fonts.FontCollection; +import org.apache.fop.fonts.FontEventAdapter; +import org.apache.fop.fonts.FontInfo; +import org.apache.fop.fonts.FontManager; +import org.apache.fop.fonts.FontResolver; +import org.apache.fop.fonts.base14.Base14FontCollection; import org.apache.fop.pdf.PDFAMode; import org.apache.fop.pdf.PDFEncryptionParams; import org.apache.fop.pdf.PDFFilterList; import org.apache.fop.pdf.PDFXMode; +import org.apache.fop.render.DefaultFontResolver; import org.apache.fop.render.PrintRendererConfigurator; import org.apache.fop.render.Renderer; import org.apache.fop.render.intermediate.IFPainter; @@ -141,17 +149,6 @@ public class PDFRendererConfigurator extends PrintRendererConfigurator } } - public void configure(IFPainter painter) throws FOPException { - Configuration cfg = super.getRendererConfig(painter.getMimeType()); - if (cfg != null) { - PDFPainter pdfPainter = (PDFPainter)painter; - PDFRenderingUtil pdfUtil = pdfPainter.getPDFUtil(); - configure(cfg, pdfUtil); - - //TODO Configure fonts - } - } - /** * Builds a filter map from an Avalon Configuration object. * @param cfg the Configuration object @@ -200,4 +197,37 @@ public class PDFRendererConfigurator extends PrintRendererConfigurator return filterMap; } + // ---=== IFPainter configuration ===--- + + /** {@inheritDoc} */ + public void configure(IFPainter painter) throws FOPException { + Configuration cfg = super.getRendererConfig(painter.getMimeType()); + if (cfg != null) { + PDFPainter pdfPainter = (PDFPainter)painter; + PDFRenderingUtil pdfUtil = pdfPainter.getPDFUtil(); + configure(cfg, pdfUtil); + } + } + + /** {@inheritDoc} */ + public void setupFontInfo(IFPainter painter) throws FOPException { + FontManager fontManager = userAgent.getFactory().getFontManager(); + List fontCollections = new java.util.ArrayList(); + fontCollections.add(new Base14FontCollection(fontManager.isBase14KerningEnabled())); + + Configuration cfg = super.getRendererConfig(painter.getMimeType()); + if (cfg != null) { + FontResolver fontResolver = new DefaultFontResolver(userAgent); + List fontList = buildFontList(cfg, fontResolver); + fontCollections.add(new CustomFontCollection(fontResolver, fontList)); + } + + FontInfo fontInfo = new FontInfo(); + fontInfo.setEventListener(new FontEventAdapter(userAgent.getEventBroadcaster())); + fontManager.setup(fontInfo, + (FontCollection[])fontCollections.toArray( + new FontCollection[fontCollections.size()])); + painter.setFontInfo(fontInfo); + } + } diff --git a/test/java/org/apache/fop/intermediate/AbstractIntermediateTestCase.java b/test/java/org/apache/fop/intermediate/AbstractIntermediateTestCase.java new file mode 100644 index 000000000..ddbf1c078 --- /dev/null +++ b/test/java/org/apache/fop/intermediate/AbstractIntermediateTestCase.java @@ -0,0 +1,228 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.intermediate; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.net.MalformedURLException; + +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Templates; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.sax.SAXTransformerFactory; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + +import org.custommonkey.xmlunit.XMLTestCase; +import org.w3c.dom.Document; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.output.ByteArrayOutputStream; + +import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.FopFactory; +import org.apache.fop.apps.MimeConstants; +import org.apache.fop.util.ConsoleEventListenerForTests; + +/** + * Abstract base class for intermediate format tests. + */ +public abstract class AbstractIntermediateTestCase extends XMLTestCase { + + /** the FOP factory */ + protected static FopFactory fopFactory = FopFactory.newInstance(); + + /** the JAXP transformer factory */ + protected static SAXTransformerFactory tFactory + = (SAXTransformerFactory)SAXTransformerFactory.newInstance(); + + /** the main base directory for tests */ + protected File mainDir = new File("test/layoutengine"); + /** the directory containing the tests */ + protected File testDir = new File(mainDir, "standard-testcases"); + /** the output directory for any files generated by the tests */ + protected File outputDir; + + private static Templates stylesheet = null; + + /** the test file */ + protected File testFile; + /** the intermediate format document as DOM */ + protected Document intermediate; + + /** + * Constructor for the test suite that is used for each test file. + * @param testFile the test file to run + */ + public AbstractIntermediateTestCase(File testFile) { + super(testFile.getName()); + this.testFile = testFile; + } + + /** {@inheritDoc} */ + protected void setUp() throws Exception { + super.setUp(); + setupOutputDirectory(); + intermediate = buildIntermediateDocument( + new StreamSource(testFile), getStylesheet()); + if (outputDir != null) { + saveDOM(intermediate, new File(outputDir, + getName() + ".1" + getIntermediateFileExtension())); + } + } + + /** + * Returns the file extension for the intermediate file format. + * @return the file extension + */ + protected abstract String getIntermediateFileExtension(); + + /** + * Returns the MIME type for which to test or to mimic for the intermediate format. + * @return the MIME type + */ + protected String getTargetMIME() { + return MimeConstants.MIME_PDF; + } + + /** + * Builds an intermediate format document from a source file. + * @param source the source file + * @param templates the (optional) stylesheet + * @return the intermediate format document as a DOM + * @throws Exception if an error occurs while processing the document + */ + protected abstract Document buildIntermediateDocument( + Source source, Templates templates) throws Exception; + + /** + * Returns the stylesheet that transforms layout engine test cases into normal FO files. + * @return the stylesheet + * @throws TransformerConfigurationException if the stylesheet cannot be instantiated + */ + protected Templates getStylesheet() throws TransformerConfigurationException { + if (stylesheet == null) { + File xsltFile = new File(mainDir, "testcase2fo.xsl"); + stylesheet = tFactory.newTemplates(new StreamSource(xsltFile)); + } + return stylesheet; + } + + /** + * Saves a DOM to a file. + * @param doc the DOM Document + * @param tgtFile the target file + * @throws Exception if an error occurs while serializing the DOM + */ + protected void saveDOM(Document doc, File tgtFile) throws Exception { + Transformer transformer = tFactory.newTransformer(); + Source src = new DOMSource(doc); + Result res = new StreamResult(tgtFile); + transformer.transform(src, res); + } + + /** + * Creates a new FOP user agent. + * @return the user agent + */ + protected FOUserAgent createUserAgent() { + FOUserAgent userAgent = fopFactory.newFOUserAgent(); + try { + userAgent.setBaseURL(testDir.toURI().toURL().toExternalForm()); + userAgent.getEventBroadcaster().addEventListener( + new ConsoleEventListenerForTests(testFile.getName())); + } catch (MalformedURLException e) { + //ignore, won't happen + } + return userAgent; + } + + /** + * Sets up the output directory. + */ + protected void setupOutputDirectory() { + String s = System.getProperty("fop.intermediate.outdir"); + if (s != null && s.length() > 0) { + outputDir = new File(s); + outputDir.mkdirs(); + } + } + + /** + * Tests the area tree parser by running the parsed area tree again through the area tree + * renderer. The source and result documents are compared to each other. + * @throws Exception if the test fails + */ + public void testParserToIntermediateFormat() throws Exception { + Source src = new DOMSource(intermediate); + Document doc = parseAndRenderToIntermediateFormat(src); + if (outputDir != null) { + File tgtFile = new File(outputDir, getName() + ".2" + getIntermediateFileExtension()); + saveDOM(doc, tgtFile); + } + + assertXMLEqual(intermediate, doc); + } + + /** + * Parses the intermediate file and renders it back to the intermediate format. + * @param src the source for the intermediate file + * @return a DOM Document with the re-created intermediate file + * @throws Exception if an error occurs while processing the document + */ + protected abstract Document parseAndRenderToIntermediateFormat(Source src) throws Exception; + + /** + * Tests the area tree parser by sending the parsed area tree to the PDF Renderer. Some + * errors might be caught by the PDFRenderer. + * @throws Exception if the test fails + */ + public void testParserToPDF() throws Exception { + OutputStream out; + if (outputDir != null) { + File tgtFile = new File(outputDir, getName() + ".pdf"); + out = new FileOutputStream(tgtFile); + out = new BufferedOutputStream(out); + } else { + out = new ByteArrayOutputStream(); + } + try { + Source src = new DOMSource(intermediate); + parseAndRender(src, out); + } finally { + IOUtils.closeQuietly(out); + } + } + + /** + * Parses and renders an intermediate format document to a final format. + * @param src the source document + * @param out the target output stream + * @throws Exception if an error occurs while rendering the document + */ + protected abstract void parseAndRender(Source src, OutputStream out) + throws Exception; + +} diff --git a/test/java/org/apache/fop/intermediate/AreaTreeParserTestCase.java b/test/java/org/apache/fop/intermediate/AreaTreeParserTestCase.java index 96aa89bc3..724bd80cf 100644 --- a/test/java/org/apache/fop/intermediate/AreaTreeParserTestCase.java +++ b/test/java/org/apache/fop/intermediate/AreaTreeParserTestCase.java @@ -19,30 +19,21 @@ package org.apache.fop.intermediate; -import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileOutputStream; import java.io.OutputStream; -import java.net.MalformedURLException; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Templates; import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.dom.DOMResult; -import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXResult; -import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; -import javax.xml.transform.stream.StreamResult; -import javax.xml.transform.stream.StreamSource; -import org.apache.commons.io.IOUtils; -import org.apache.commons.io.output.ByteArrayOutputStream; +import org.w3c.dom.Document; + import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.Fop; -import org.apache.fop.apps.FopFactory; import org.apache.fop.apps.MimeConstants; import org.apache.fop.area.AreaTreeModel; import org.apache.fop.area.AreaTreeParser; @@ -50,139 +41,31 @@ import org.apache.fop.area.RenderPagesModel; import org.apache.fop.fonts.FontInfo; import org.apache.fop.render.Renderer; import org.apache.fop.render.xml.XMLRenderer; -import org.apache.fop.util.ConsoleEventListenerForTests; - -//XML Unit 1.0: See http://xmlunit.sourceforge.net (BSD-style License) -import org.custommonkey.xmlunit.XMLTestCase; -import org.w3c.dom.Document; /** * Tests the area tree parser. */ -public class AreaTreeParserTestCase extends XMLTestCase { - - // configure fopFactory as desired - private static FopFactory fopFactory = FopFactory.newInstance(); - - private static SAXTransformerFactory tFactory - = (SAXTransformerFactory)SAXTransformerFactory.newInstance(); - private static Templates stylesheet = null; - - private File mainDir = new File("test/layoutengine"); - private File testDir = new File(mainDir, "standard-testcases"); - - private String name; - private File testFile; - - private File outputDir; - private Document intermediate; - - /** @see junit.framework.TestCase#TestCase(String) */ - public AreaTreeParserTestCase(String name) { - super(name); - } - +public class AreaTreeParserTestCase extends AbstractIntermediateTestCase { + /** * Constructor for the test suite that is used for each test file. * @param testFile the test file to run */ public AreaTreeParserTestCase(File testFile) { - super(testFile.getName()); - this.testFile = testFile; - } - - private Templates getStylesheet() throws TransformerConfigurationException { - if (stylesheet == null) { - File xsltFile = new File(mainDir, "testcase2fo.xsl"); - stylesheet = tFactory.newTemplates(new StreamSource(xsltFile)); - } - return stylesheet; - } - - /** @see junit.framework.TestCase#setUp() */ - protected void setUp() throws Exception { - super.setUp(); - String s = System.getProperty("fop.intermediate.outdir"); - if (s != null && s.length() > 0) { - outputDir = new File(s); - outputDir.mkdirs(); - } - File srcFile; - if (testFile != null) { - srcFile = testFile; - } else { - srcFile = new File(testDir, "block_font-style.xml"); - } - this.name = srcFile.getName(); - intermediate = buildAreaTreeXML(new StreamSource(srcFile), getStylesheet()); - if (outputDir != null) { - saveDOM(intermediate, new File(outputDir, name + ".at1.xml")); - } + super(testFile); } - - /** - * Tests the area tree parser by running the parsed area tree again through the area tree - * renderer. The source and result documents are compared to each other. - * @throws Exception if the test fails - */ - public void testParserToAT() throws Exception { - - Source src = new DOMSource(intermediate); - Document doc = parseAndRenderToAreaTree(src); - if (outputDir != null) { - File tgtFile = new File(outputDir, name + ".at2.xml"); - saveDOM(doc, tgtFile); - } - - assertXMLEqual(intermediate, doc); - } - - private void saveDOM(Document doc, File tgtFile) throws Exception { - Transformer transformer = tFactory.newTransformer(); - Source src = new DOMSource(doc); - Result res = new StreamResult(tgtFile); - transformer.transform(src, res); + /** {@inheritDoc} */ + protected String getIntermediateFileExtension() { + return ".at.xml"; } - /** - * Tests the area tree parser by sending the parsed area tree to the PDF Renderer. Some - * errors might be caught by the PDFRenderer. - * @throws Exception if the test fails - */ - public void testParserToPDF() throws Exception { - OutputStream out; - if (outputDir != null) { - File tgtFile = new File(outputDir, name + ".pdf"); - out = new FileOutputStream(tgtFile); - out = new BufferedOutputStream(out); - } else { - out = new ByteArrayOutputStream(); - } - try { - Source src = new DOMSource(intermediate); - parseAndRender(src, out, MimeConstants.MIME_PDF); - } finally { - IOUtils.closeQuietly(out); - } - } - - private FOUserAgent createUserAgent() { - FOUserAgent userAgent = fopFactory.newFOUserAgent(); - try { - userAgent.setBaseURL(testDir.toURL().toExternalForm()); - userAgent.getEventBroadcaster().addEventListener( - new ConsoleEventListenerForTests(testFile.getName())); - } catch (MalformedURLException e) { - //ignore, won't happen - } - return userAgent; - } - - private Document buildAreaTreeXML(Source src, Templates stylesheet) throws Exception { + /** {@inheritDoc} */ + protected Document buildIntermediateDocument(Source src, Templates templates) + throws Exception { Transformer transformer; - if (stylesheet != null) { - transformer = stylesheet.newTransformer(); + if (templates != null) { + transformer = templates.newTransformer(); } else { transformer = tFactory.newTransformer(); } @@ -191,41 +74,43 @@ public class AreaTreeParserTestCase extends XMLTestCase { TransformerHandler handler = tFactory.newTransformerHandler(); DOMResult domResult = new DOMResult(); handler.setResult(domResult); - + FOUserAgent userAgent = createUserAgent(); //Create an instance of the target renderer so the XMLRenderer can use its font setup Renderer targetRenderer = userAgent.getRendererFactory().createRenderer( - userAgent, MimeConstants.MIME_PDF); - + userAgent, getTargetMIME()); + XMLRenderer renderer = new XMLRenderer(); renderer.mimicRenderer(targetRenderer); renderer.setContentHandler(handler); renderer.setUserAgent(userAgent); userAgent.setRendererOverride(renderer); - + Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AREA_TREE, userAgent); Result res = new SAXResult(fop.getDefaultHandler()); transformer.transform(src, res); - + return (Document)domResult.getNode(); } - - private void parseAndRender(Source src, OutputStream out, String mime) throws Exception { + + /** {@inheritDoc} */ + protected void parseAndRender(Source src, OutputStream out) throws Exception { AreaTreeParser parser = new AreaTreeParser(); - + FOUserAgent userAgent = createUserAgent(); FontInfo fontInfo = new FontInfo(); - AreaTreeModel treeModel = new RenderPagesModel(userAgent, - mime, fontInfo, out); + AreaTreeModel treeModel = new RenderPagesModel(userAgent, + getTargetMIME(), fontInfo, out); parser.parse(src, treeModel, userAgent); treeModel.endDocument(); } - - private Document parseAndRenderToAreaTree(Source src) throws Exception { + + /** {@inheritDoc} */ + protected Document parseAndRenderToIntermediateFormat(Source src) throws Exception { AreaTreeParser parser = new AreaTreeParser(); - + //Set up XMLRenderer to render to a DOM TransformerHandler handler = tFactory.newTransformerHandler(); DOMResult domResult = new DOMResult(); @@ -238,12 +123,12 @@ public class AreaTreeParserTestCase extends XMLTestCase { renderer.setUserAgent(userAgent); FontInfo fontInfo = new FontInfo(); - AreaTreeModel treeModel = new RenderPagesModel(userAgent, - MimeConstants.MIME_FOP_AREA_TREE, fontInfo, null); + AreaTreeModel treeModel = new RenderPagesModel(userAgent, + MimeConstants.MIME_FOP_AREA_TREE, fontInfo, null); parser.parse(src, treeModel, userAgent); treeModel.endDocument(); return (Document)domResult.getNode(); } - + } diff --git a/test/java/org/apache/fop/intermediate/AreaTreeXMLFormatTestSuite.java b/test/java/org/apache/fop/intermediate/AreaTreeXMLFormatTestSuite.java new file mode 100644 index 000000000..2c7bd09e4 --- /dev/null +++ b/test/java/org/apache/fop/intermediate/AreaTreeXMLFormatTestSuite.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.intermediate; + +import java.io.File; +import java.io.IOException; +import java.util.Collection; +import java.util.Iterator; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.apache.fop.layoutengine.LayoutEngineTestSuite; + +/** + * JUnit test suite for the area tree XML format + */ +public class AreaTreeXMLFormatTestSuite { + + /** + * @return the test suite with all the tests (one for each XML file) + * @throws IOException in case of an I/O problem + */ + public static Test suite() throws IOException { + TestSuite suite = new TestSuite(); + + Collection files = LayoutEngineTestSuite.getTestFiles(); + + Iterator i = files.iterator(); + while (i.hasNext()) { + File f = (File)i.next(); + addATTestCase(suite, f); + } + + return suite; + } + + private static void addATTestCase(TestSuite suite, + final File f) { + suite.addTest(new AreaTreeParserTestCase(f) { + public void runTest() throws Exception { + try { + testParserToIntermediateFormat(); + testParserToPDF(); + } catch (Exception e) { + org.apache.commons.logging.LogFactory.getLog( + this.getClass()).error("Error on " + f.getName()); + throw e; + } + } + }); + } + +} diff --git a/test/java/org/apache/fop/intermediate/IFParserTestCase.java b/test/java/org/apache/fop/intermediate/IFParserTestCase.java new file mode 100644 index 000000000..92f63a334 --- /dev/null +++ b/test/java/org/apache/fop/intermediate/IFParserTestCase.java @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.intermediate; + +import java.io.File; +import java.io.OutputStream; + +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Templates; +import javax.xml.transform.Transformer; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Document; + +import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.apps.Fop; +import org.apache.fop.render.Renderer; +import org.apache.fop.render.intermediate.IFPainter; +import org.apache.fop.render.intermediate.IFParser; +import org.apache.fop.render.intermediate.IFRenderer; +import org.apache.fop.render.intermediate.IFSerializer; + +/** + * Tests the intermediate format parser. + */ +public class IFParserTestCase extends AbstractIntermediateTestCase { + + /** + * Constructor for the test suite that is used for each test file. + * @param testFile the test file to run + */ + public IFParserTestCase(File testFile) { + super(testFile); + } + + /** {@inheritDoc} */ + protected String getIntermediateFileExtension() { + return ".if.xml"; + } + + /** {@inheritDoc} */ + protected Document buildIntermediateDocument(Source src, Templates templates) + throws Exception { + Transformer transformer; + if (templates != null) { + transformer = templates.newTransformer(); + } else { + transformer = tFactory.newTransformer(); + } + + //Set up XMLRenderer to render to a DOM + DOMResult domResult = new DOMResult(); + + FOUserAgent userAgent = createUserAgent(); + + //Create an instance of the target renderer so the XMLRenderer can use its font setup + Renderer targetRenderer = userAgent.getRendererFactory().createRenderer( + userAgent, getTargetMIME()); + + //Setup renderer + IFRenderer renderer = new IFRenderer(); + renderer.setUserAgent(userAgent); + renderer.mimicRenderer(targetRenderer); + + //Setup painter + IFSerializer serializer = new IFSerializer(); + serializer.setUserAgent(userAgent); + serializer.setResult(domResult); + + renderer.setPainter(serializer); + userAgent.setRendererOverride(renderer); + + Fop fop = fopFactory.newFop(userAgent); + Result res = new SAXResult(fop.getDefaultHandler()); + transformer.transform(src, res); + + return (Document)domResult.getNode(); + } + + /** {@inheritDoc} */ + protected void parseAndRender(Source src, OutputStream out) throws Exception { + IFParser parser = new IFParser(); + + FOUserAgent userAgent = createUserAgent(); + + IFPainter painter = userAgent.getRendererFactory().createPainter( + userAgent, getTargetMIME()); + painter.setResult(new StreamResult(out)); + painter.setDefaultFontInfo(); + parser.parse(src, painter, userAgent); + } + + /** {@inheritDoc} */ + protected Document parseAndRenderToIntermediateFormat(Source src) throws Exception { + IFParser parser = new IFParser(); + + FOUserAgent userAgent = createUserAgent(); + + IFSerializer serializer = new IFSerializer(); + serializer.setUserAgent(userAgent); + DOMResult domResult = new DOMResult(); + serializer.setResult(domResult); + + parser.parse(src, serializer, userAgent); + + return (Document)domResult.getNode(); + } + +} diff --git a/test/java/org/apache/fop/intermediate/IntermediateFormatTestSuite.java b/test/java/org/apache/fop/intermediate/IntermediateFormatTestSuite.java index 68c81da81..f8511cadc 100644 --- a/test/java/org/apache/fop/intermediate/IntermediateFormatTestSuite.java +++ b/test/java/org/apache/fop/intermediate/IntermediateFormatTestSuite.java @@ -46,18 +46,18 @@ public class IntermediateFormatTestSuite { Iterator i = files.iterator(); while (i.hasNext()) { File f = (File)i.next(); - addTestCase(suite, f); + addIFTestCase(suite, f); } - + return suite; } - - private static void addTestCase(TestSuite suite, + + private static void addIFTestCase(TestSuite suite, final File f) { - suite.addTest(new AreaTreeParserTestCase(f) { + suite.addTest(new IFParserTestCase(f) { public void runTest() throws Exception { try { - testParserToAT(); + testParserToIntermediateFormat(); testParserToPDF(); } catch (Exception e) { org.apache.commons.logging.LogFactory.getLog( @@ -67,5 +67,5 @@ public class IntermediateFormatTestSuite { } }); } - + } |