aboutsummaryrefslogtreecommitdiffstats
path: root/test/java/org/apache/fop/intermediate/AreaTreeParserTestCase.java
blob: 7bc2a9fc5037b42f8a66551f7dd4928a57a56ea6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * 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.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.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;
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;

//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 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);
    }
    
    /**
     * 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"));
        }
    }


    /**
     * 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);
    }

    /**
     * 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());
        } catch (MalformedURLException e) {
            //ignore, won't happen
        }
        return userAgent;
    }

    private Document buildAreaTreeXML(Source src, Templates stylesheet) throws Exception {
        Transformer transformer;
        if (stylesheet != null) {
            transformer = stylesheet.newTransformer();
        } else {
            transformer = tFactory.newTransformer();
        }

        //Set up XMLRenderer to render to a DOM
        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); 
        
        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 {
        AreaTreeParser parser = new AreaTreeParser();
                
        FOUserAgent userAgent = createUserAgent();
        FontInfo fontInfo = new FontInfo();
        AreaTreeModel treeModel = new RenderPagesModel(userAgent, 
                mime, fontInfo, out); 
        parser.parse(src, treeModel, userAgent);
        treeModel.endDocument();
    }
    
    private Document parseAndRenderToAreaTree(Source src) throws Exception {
        AreaTreeParser parser = new AreaTreeParser();
                
        //Set up XMLRenderer to render to a DOM
        TransformerHandler handler = tFactory.newTransformerHandler();
        DOMResult domResult = new DOMResult();
        handler.setResult(domResult);
        XMLRenderer renderer = new XMLRenderer();
        renderer.setContentHandler(handler);

        FOUserAgent userAgent = createUserAgent();
        userAgent.setRendererOverride(renderer);
        renderer.setUserAgent(userAgent);

        FontInfo fontInfo = new FontInfo();
        AreaTreeModel treeModel = new RenderPagesModel(userAgent, 
                MimeConstants.MIME_FOP_AREA_TREE, fontInfo, null); 
        parser.parse(src, treeModel, userAgent);
        treeModel.endDocument();

        return (Document)domResult.getNode();
    }
    
}