KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > intermediate > AreaTreeParserTestCase


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id$ */
19
20 package org.apache.fop.intermediate;
21
22 import java.io.BufferedOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27
28 import javax.xml.transform.Result JavaDoc;
29 import javax.xml.transform.Source JavaDoc;
30 import javax.xml.transform.Templates JavaDoc;
31 import javax.xml.transform.Transformer JavaDoc;
32 import javax.xml.transform.TransformerConfigurationException JavaDoc;
33 import javax.xml.transform.dom.DOMResult JavaDoc;
34 import javax.xml.transform.dom.DOMSource JavaDoc;
35 import javax.xml.transform.sax.SAXResult JavaDoc;
36 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
37 import javax.xml.transform.sax.TransformerHandler JavaDoc;
38 import javax.xml.transform.stream.StreamResult JavaDoc;
39 import javax.xml.transform.stream.StreamSource JavaDoc;
40
41 import org.apache.commons.io.IOUtils;
42 import org.apache.commons.io.output.ByteArrayOutputStream;
43 import org.apache.fop.apps.FOUserAgent;
44 import org.apache.fop.apps.Fop;
45 import org.apache.fop.apps.FopFactory;
46 import org.apache.fop.apps.MimeConstants;
47 import org.apache.fop.area.AreaTreeModel;
48 import org.apache.fop.area.AreaTreeParser;
49 import org.apache.fop.area.RenderPagesModel;
50 import org.apache.fop.fonts.FontInfo;
51 import org.apache.fop.render.Renderer;
52 import org.apache.fop.render.xml.XMLRenderer;
53
54 //XML Unit 1.0: See http://xmlunit.sourceforge.net (BSD-style License)
55
import org.custommonkey.xmlunit.XMLTestCase;
56 import org.w3c.dom.Document JavaDoc;
57
58 /**
59  * Tests the area tree parser.
60  */

61 public class AreaTreeParserTestCase extends XMLTestCase {
62
63     // configure fopFactory as desired
64
private FopFactory fopFactory = FopFactory.newInstance();
65     
66     private static SAXTransformerFactory JavaDoc tFactory
67             = (SAXTransformerFactory JavaDoc)SAXTransformerFactory.newInstance();
68     private static Templates JavaDoc stylesheet = null;
69     
70     private File JavaDoc mainDir = new File JavaDoc("test/layoutengine");
71     private File JavaDoc testDir = new File JavaDoc(mainDir, "standard-testcases");
72     
73     private String JavaDoc name;
74     private File JavaDoc testFile;
75
76     private File JavaDoc outputDir;
77     private Document JavaDoc intermediate;
78     
79     /** @see junit.framework.TestCase#TestCase(String) */
80     public AreaTreeParserTestCase(String JavaDoc name) {
81         super(name);
82     }
83     
84     /**
85      * Constructor for the test suite that is used for each test file.
86      * @param testFile the test file to run
87      */

88     public AreaTreeParserTestCase(File JavaDoc testFile) {
89         super(testFile.getName());
90         this.testFile = testFile;
91     }
92  
93     private Templates JavaDoc getStylesheet() throws TransformerConfigurationException JavaDoc {
94         if (stylesheet == null) {
95             File JavaDoc xsltFile = new File JavaDoc(mainDir, "testcase2fo.xsl");
96             stylesheet = tFactory.newTemplates(new StreamSource JavaDoc(xsltFile));
97         }
98         return stylesheet;
99     }
100     
101     /** @see junit.framework.TestCase#setUp() */
102     protected void setUp() throws Exception JavaDoc {
103         super.setUp();
104         String JavaDoc s = System.getProperty("fop.intermediate.outdir");
105         if (s != null && s.length() > 0) {
106             outputDir = new File JavaDoc(s);
107             outputDir.mkdirs();
108         }
109         File JavaDoc srcFile;
110         if (testFile != null) {
111             srcFile = testFile;
112         } else {
113             srcFile = new File JavaDoc(testDir, "block_font-style.xml");
114         }
115         this.name = srcFile.getName();
116         intermediate = buildAreaTreeXML(new StreamSource JavaDoc(srcFile), getStylesheet());
117         if (outputDir != null) {
118             saveDOM(intermediate, new File JavaDoc(outputDir, name + ".at1.xml"));
119         }
120     }
121
122
123     /**
124      * Tests the area tree parser by running the parsed area tree again through the area tree
125      * renderer. The source and result documents are compared to each other.
126      * @throws Exception if the test fails
127      */

128     public void testParserToAT() throws Exception JavaDoc {
129                 
130         Source JavaDoc src = new DOMSource JavaDoc(intermediate);
131         Document JavaDoc doc = parseAndRenderToAreaTree(src);
132         if (outputDir != null) {
133             File JavaDoc tgtFile = new File JavaDoc(outputDir, name + ".at2.xml");
134             saveDOM(doc, tgtFile);
135         }
136         
137         assertXMLEqual(intermediate, doc);
138     }
139     
140     private void saveDOM(Document JavaDoc doc, File JavaDoc tgtFile) throws Exception JavaDoc {
141         Transformer JavaDoc transformer = tFactory.newTransformer();
142         Source JavaDoc src = new DOMSource JavaDoc(doc);
143         Result JavaDoc res = new StreamResult JavaDoc(tgtFile);
144         transformer.transform(src, res);
145     }
146
147     /**
148      * Tests the area tree parser by sending the parsed area tree to the PDF Renderer. Some
149      * errors might be caught by the PDFRenderer.
150      * @throws Exception if the test fails
151      */

152     public void testParserToPDF() throws Exception JavaDoc {
153         OutputStream JavaDoc out;
154         if (outputDir != null) {
155             File JavaDoc tgtFile = new File JavaDoc(outputDir, name + ".pdf");
156             out = new FileOutputStream JavaDoc(tgtFile);
157             out = new BufferedOutputStream JavaDoc(out);
158         } else {
159             out = new ByteArrayOutputStream();
160         }
161         try {
162             Source JavaDoc src = new DOMSource JavaDoc(intermediate);
163             parseAndRender(src, out, MimeConstants.MIME_PDF);
164         } finally {
165             IOUtils.closeQuietly(out);
166         }
167     }
168     
169     private FOUserAgent createUserAgent() {
170         FOUserAgent userAgent = fopFactory.newFOUserAgent();
171         try {
172             userAgent.setBaseURL(testDir.toURL().toExternalForm());
173         } catch (MalformedURLException JavaDoc e) {
174             //ignore, won't happen
175
}
176         return userAgent;
177     }
178
179     private Document JavaDoc buildAreaTreeXML(Source JavaDoc src, Templates JavaDoc stylesheet) throws Exception JavaDoc {
180         Transformer JavaDoc transformer;
181         if (stylesheet != null) {
182             transformer = stylesheet.newTransformer();
183         } else {
184             transformer = tFactory.newTransformer();
185         }
186
187         //Set up XMLRenderer to render to a DOM
188
TransformerHandler JavaDoc handler = tFactory.newTransformerHandler();
189         DOMResult JavaDoc domResult = new DOMResult JavaDoc();
190         handler.setResult(domResult);
191         
192         FOUserAgent userAgent = createUserAgent();
193
194         //Create an instance of the target renderer so the XMLRenderer can use its font setup
195
Renderer targetRenderer = userAgent.getRendererFactory().createRenderer(
196                 userAgent, MimeConstants.MIME_PDF);
197         
198         XMLRenderer renderer = new XMLRenderer();
199         renderer.mimicRenderer(targetRenderer);
200         renderer.setContentHandler(handler);
201         renderer.setUserAgent(userAgent);
202
203         userAgent.setRendererOverride(renderer);
204         
205         Fop fop = fopFactory.newFop(MimeConstants.MIME_FOP_AREA_TREE, userAgent);
206         Result JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
207         transformer.transform(src, res);
208         
209         return (Document JavaDoc)domResult.getNode();
210     }
211     
212     private void parseAndRender(Source JavaDoc src, OutputStream JavaDoc out, String JavaDoc mime) throws Exception JavaDoc {
213         AreaTreeParser parser = new AreaTreeParser();
214                 
215         FOUserAgent userAgent = createUserAgent();
216         FontInfo fontInfo = new FontInfo();
217         AreaTreeModel treeModel = new RenderPagesModel(userAgent,
218                 mime, fontInfo, out);
219         parser.parse(src, treeModel, userAgent);
220         treeModel.endDocument();
221     }
222     
223     private Document JavaDoc parseAndRenderToAreaTree(Source JavaDoc src) throws Exception JavaDoc {
224         AreaTreeParser parser = new AreaTreeParser();
225                 
226         //Set up XMLRenderer to render to a DOM
227
TransformerHandler JavaDoc handler = tFactory.newTransformerHandler();
228         DOMResult JavaDoc domResult = new DOMResult JavaDoc();
229         handler.setResult(domResult);
230         XMLRenderer renderer = new XMLRenderer();
231         renderer.setContentHandler(handler);
232
233         FOUserAgent userAgent = createUserAgent();
234         userAgent.setRendererOverride(renderer);
235         renderer.setUserAgent(userAgent);
236
237         FontInfo fontInfo = new FontInfo();
238         AreaTreeModel treeModel = new RenderPagesModel(userAgent,
239                 MimeConstants.MIME_FOP_AREA_TREE, fontInfo, null);
240         parser.parse(src, treeModel, userAgent);
241         treeModel.endDocument();
242
243         return (Document JavaDoc)domResult.getNode();
244     }
245     
246 }
247
Popular Tags