KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > URIResolutionTestCase


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: URIResolutionTestCase.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop;
21
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 import javax.xml.transform.Result JavaDoc;
27 import javax.xml.transform.Source JavaDoc;
28 import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.TransformerException JavaDoc;
30 import javax.xml.transform.URIResolver JavaDoc;
31 import javax.xml.transform.dom.DOMResult JavaDoc;
32 import javax.xml.transform.dom.DOMSource JavaDoc;
33 import javax.xml.transform.sax.SAXResult JavaDoc;
34 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
35 import javax.xml.transform.sax.TransformerHandler JavaDoc;
36 import javax.xml.transform.stream.StreamResult JavaDoc;
37 import javax.xml.transform.stream.StreamSource JavaDoc;
38
39 import org.apache.commons.io.IOUtils;
40 import org.apache.commons.io.output.ByteArrayOutputStream;
41 import org.apache.fop.apps.FOPException;
42 import org.apache.fop.apps.FOUserAgent;
43 import org.apache.fop.apps.Fop;
44 import org.apache.fop.apps.FopFactory;
45 import org.apache.fop.apps.MimeConstants;
46 import org.apache.fop.render.xml.XMLRenderer;
47 import org.apache.xpath.XPathAPI;
48 import org.apache.xpath.objects.XObject;
49 import org.w3c.dom.Document JavaDoc;
50
51 /**
52  * Tests URI resolution facilities.
53  */

54 public class URIResolutionTestCase extends AbstractFOPTestCase {
55
56     // configure fopFactory as desired
57
private FopFactory fopFactory = FopFactory.newInstance();
58     
59     private SAXTransformerFactory JavaDoc tfactory
60             = (SAXTransformerFactory JavaDoc)SAXTransformerFactory.newInstance();
61
62     private File JavaDoc backupDir = new File JavaDoc(getBaseDir(), "build/test-results");
63     
64     /** @see junit.framework.TestCase#TestCase(String) */
65     public URIResolutionTestCase(String JavaDoc name) {
66         super(name);
67     }
68
69     /**
70      * Test custom URI resolution with a hand-written URIResolver.
71      * @throws Exception if anything fails
72      */

73     public void testFO1a() throws Exception JavaDoc {
74         innerTestFO1(false);
75     }
76     
77     /**
78      * Test custom URI resolution with a hand-written URIResolver.
79      * @throws Exception if anything fails
80      */

81     public void testFO1b() throws Exception JavaDoc {
82         innerTestFO1(true);
83     }
84     
85     private void innerTestFO1(boolean withStream) throws Exception JavaDoc {
86         FOUserAgent ua = fopFactory.newFOUserAgent();
87
88         //Reset the image caches to force URI resolution!
89
ua.getFactory().getImageFactory().clearCaches();
90         
91         File JavaDoc foFile = new File JavaDoc(getBaseDir(), "test/xml/uri-resolution1.fo");
92         
93         MyURIResolver resolver = new MyURIResolver(withStream);
94         ua.setURIResolver(resolver);
95         ua.setBaseURL(foFile.getParentFile().toURL().toString());
96
97         Document JavaDoc doc = createAreaTree(foFile, ua);
98         
99         //Check how many times the resolver was consulted
100
assertEquals("Expected resolver to do 1 successful URI resolution",
101                 1, resolver.successCount);
102         assertEquals("Expected resolver to do 0 failed URI resolution",
103                 0, resolver.failureCount);
104         //Additional XPath checking on the area tree
105
assertEquals("viewport for external-graphic is missing",
106                 "true", evalXPath(doc, "boolean(//flow/block[1]/lineArea/viewport)"));
107         assertEquals("46080", evalXPath(doc, "//flow/block[1]/lineArea/viewport/@ipd"));
108         assertEquals("46080", evalXPath(doc, "//flow/block[1]/lineArea/viewport/@bpd"));
109     }
110
111     /**
112      * Test custom URI resolution with a hand-written URIResolver.
113      * @throws Exception if anything fails
114      */

115     public void DISABLEDtestFO2() throws Exception JavaDoc {
116         //TODO This will only work when we can do URI resolution inside Batik!
117
File JavaDoc foFile = new File JavaDoc(getBaseDir(), "test/xml/uri-resolution2.fo");
118         
119         FOUserAgent ua = fopFactory.newFOUserAgent();
120         MyURIResolver resolver = new MyURIResolver(false);
121         ua.setURIResolver(resolver);
122         ua.setBaseURL(foFile.getParentFile().toURL().toString());
123
124         ByteArrayOutputStream baout = new ByteArrayOutputStream();
125
126         Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, ua, baout);
127
128         Transformer JavaDoc transformer = tfactory.newTransformer(); //Identity transf.
129
Source JavaDoc src = new StreamSource JavaDoc(foFile);
130         Result JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
131         transformer.transform(src, res);
132         
133         OutputStream JavaDoc out = new java.io.FileOutputStream JavaDoc(
134                 new File JavaDoc(backupDir, foFile.getName() + ".pdf"));
135         try {
136             baout.writeTo(out);
137         } finally {
138             IOUtils.closeQuietly(out);
139         }
140         
141         //Check how many times the resolver was consulted
142
assertEquals("Expected resolver to do 1 successful URI resolution",
143                 1, resolver.successCount);
144         assertEquals("Expected resolver to do 0 failed URI resolutions",
145                 0, resolver.failureCount);
146         //Test using PDF as the area tree doesn't invoke Batik so we could check
147
//if the resolver is actually passed to Batik by FOP
148
assertTrue("Generated PDF has zero length", baout.size() > 0);
149     }
150
151     private Document JavaDoc createAreaTree(File JavaDoc fo, FOUserAgent ua)
152                 throws TransformerException JavaDoc, FOPException {
153         DOMResult JavaDoc domres = new DOMResult JavaDoc();
154         //Setup Transformer to convert the area tree to a DOM
155
TransformerHandler JavaDoc athandler = tfactory.newTransformerHandler();
156         athandler.setResult(domres);
157
158         XMLRenderer atrenderer = new XMLRenderer();
159         atrenderer.setUserAgent(ua);
160         atrenderer.setContentHandler(athandler);
161         ua.setRendererOverride(atrenderer);
162         
163         Fop fop = fopFactory.newFop(ua);
164
165         Transformer JavaDoc transformer = tfactory.newTransformer(); //Identity transf.
166
Source JavaDoc src = new StreamSource JavaDoc(fo);
167         Result JavaDoc res = new SAXResult JavaDoc(fop.getDefaultHandler());
168         transformer.transform(src, res);
169         
170         Document JavaDoc doc = (Document JavaDoc)domres.getNode();
171         saveAreaTreeXML(doc, new File JavaDoc(backupDir, fo.getName() + ".at.xml"));
172         return doc;
173     }
174     
175     private String JavaDoc evalXPath(Document JavaDoc doc, String JavaDoc xpath) {
176         XObject res;
177         try {
178             res = XPathAPI.eval(doc, xpath);
179         } catch (TransformerException JavaDoc e) {
180             throw new RuntimeException JavaDoc("XPath evaluation failed: " + e.getMessage());
181         }
182         return res.str();
183     }
184
185     /**
186      * Save the area tree XML for later inspection.
187      * @param doc area tree as a DOM document
188      * @param target target file
189      * @throws TransformerException if a problem occurs during serialization
190      */

191     protected void saveAreaTreeXML(Document JavaDoc doc, File JavaDoc target) throws TransformerException JavaDoc {
192         Transformer JavaDoc transformer = tfactory.newTransformer();
193         Source JavaDoc src = new DOMSource JavaDoc(doc);
194         Result JavaDoc res = new StreamResult JavaDoc(target);
195         transformer.transform(src, res);
196     }
197     
198     private class MyURIResolver implements URIResolver JavaDoc {
199
200         private static final String JavaDoc PREFIX = "funky:";
201         
202         private boolean withStream;
203         private int successCount = 0;
204         private int failureCount = 0;
205         
206         public MyURIResolver(boolean withStream) {
207             this.withStream = withStream;
208         }
209         
210         /**
211          * @see javax.xml.transform.URIResolver#resolve(java.lang.String, java.lang.String)
212          */

213         public Source JavaDoc resolve(String JavaDoc href, String JavaDoc base) throws TransformerException JavaDoc {
214             if (href.startsWith(PREFIX)) {
215                 String JavaDoc name = href.substring(PREFIX.length());
216                 if ("myimage123".equals(name)) {
217                     File JavaDoc image = new File JavaDoc(getBaseDir(), "test/resources/images/bgimg300dpi.jpg");
218                     Source JavaDoc src;
219                     if (withStream) {
220                         try {
221                             src = new StreamSource JavaDoc(new java.io.FileInputStream JavaDoc(image));
222                         } catch (FileNotFoundException JavaDoc e) {
223                             throw new TransformerException JavaDoc(e.getMessage(), e);
224                         }
225                     } else {
226                         src = new StreamSource JavaDoc(image);
227                     }
228                     successCount++;
229                     return src;
230                 } else {
231                     failureCount++;
232                     throw new TransformerException JavaDoc("funky image not found");
233                 }
234             } else {
235                 failureCount++;
236                 return null;
237             }
238         }
239         
240     }
241     
242 }
243
Popular Tags