1 17 18 19 20 package org.apache.fop; 21 22 import java.io.File ; 23 import java.io.FileNotFoundException ; 24 import java.io.OutputStream ; 25 26 import javax.xml.transform.Result ; 27 import javax.xml.transform.Source ; 28 import javax.xml.transform.Transformer ; 29 import javax.xml.transform.TransformerException ; 30 import javax.xml.transform.URIResolver ; 31 import javax.xml.transform.dom.DOMResult ; 32 import javax.xml.transform.dom.DOMSource ; 33 import javax.xml.transform.sax.SAXResult ; 34 import javax.xml.transform.sax.SAXTransformerFactory ; 35 import javax.xml.transform.sax.TransformerHandler ; 36 import javax.xml.transform.stream.StreamResult ; 37 import javax.xml.transform.stream.StreamSource ; 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 ; 50 51 54 public class URIResolutionTestCase extends AbstractFOPTestCase { 55 56 private FopFactory fopFactory = FopFactory.newInstance(); 58 59 private SAXTransformerFactory tfactory 60 = (SAXTransformerFactory )SAXTransformerFactory.newInstance(); 61 62 private File backupDir = new File (getBaseDir(), "build/test-results"); 63 64 65 public URIResolutionTestCase(String name) { 66 super(name); 67 } 68 69 73 public void testFO1a() throws Exception { 74 innerTestFO1(false); 75 } 76 77 81 public void testFO1b() throws Exception { 82 innerTestFO1(true); 83 } 84 85 private void innerTestFO1(boolean withStream) throws Exception { 86 FOUserAgent ua = fopFactory.newFOUserAgent(); 87 88 ua.getFactory().getImageFactory().clearCaches(); 90 91 File foFile = new File (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 doc = createAreaTree(foFile, ua); 98 99 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 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 115 public void DISABLEDtestFO2() throws Exception { 116 File foFile = new File (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 transformer = tfactory.newTransformer(); Source src = new StreamSource (foFile); 130 Result res = new SAXResult (fop.getDefaultHandler()); 131 transformer.transform(src, res); 132 133 OutputStream out = new java.io.FileOutputStream ( 134 new File (backupDir, foFile.getName() + ".pdf")); 135 try { 136 baout.writeTo(out); 137 } finally { 138 IOUtils.closeQuietly(out); 139 } 140 141 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 assertTrue("Generated PDF has zero length", baout.size() > 0); 149 } 150 151 private Document createAreaTree(File fo, FOUserAgent ua) 152 throws TransformerException , FOPException { 153 DOMResult domres = new DOMResult (); 154 TransformerHandler 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 transformer = tfactory.newTransformer(); Source src = new StreamSource (fo); 167 Result res = new SAXResult (fop.getDefaultHandler()); 168 transformer.transform(src, res); 169 170 Document doc = (Document )domres.getNode(); 171 saveAreaTreeXML(doc, new File (backupDir, fo.getName() + ".at.xml")); 172 return doc; 173 } 174 175 private String evalXPath(Document doc, String xpath) { 176 XObject res; 177 try { 178 res = XPathAPI.eval(doc, xpath); 179 } catch (TransformerException e) { 180 throw new RuntimeException ("XPath evaluation failed: " + e.getMessage()); 181 } 182 return res.str(); 183 } 184 185 191 protected void saveAreaTreeXML(Document doc, File target) throws TransformerException { 192 Transformer transformer = tfactory.newTransformer(); 193 Source src = new DOMSource (doc); 194 Result res = new StreamResult (target); 195 transformer.transform(src, res); 196 } 197 198 private class MyURIResolver implements URIResolver { 199 200 private static final String 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 213 public Source resolve(String href, String base) throws TransformerException { 214 if (href.startsWith(PREFIX)) { 215 String name = href.substring(PREFIX.length()); 216 if ("myimage123".equals(name)) { 217 File image = new File (getBaseDir(), "test/resources/images/bgimg300dpi.jpg"); 218 Source src; 219 if (withStream) { 220 try { 221 src = new StreamSource (new java.io.FileInputStream (image)); 222 } catch (FileNotFoundException e) { 223 throw new TransformerException (e.getMessage(), e); 224 } 225 } else { 226 src = new StreamSource (image); 227 } 228 successCount++; 229 return src; 230 } else { 231 failureCount++; 232 throw new TransformerException ("funky image not found"); 233 } 234 } else { 235 failureCount++; 236 return null; 237 } 238 } 239 240 } 241 242 } 243 | Popular Tags |