1 17 18 19 20 package org.apache.fop.render.pdf; 21 22 import java.io.File ; 23 24 import javax.xml.transform.Source ; 25 import javax.xml.transform.Transformer ; 26 import javax.xml.transform.TransformerFactory ; 27 import javax.xml.transform.sax.SAXResult ; 28 import javax.xml.transform.stream.StreamSource ; 29 30 import org.apache.commons.io.FileUtils; 31 import org.apache.commons.io.output.ByteArrayOutputStream; 32 import org.apache.fop.apps.FOUserAgent; 33 import org.apache.fop.apps.Fop; 34 import org.apache.fop.apps.FopFactory; 35 import org.apache.fop.apps.MimeConstants; 36 37 import junit.framework.TestCase; 38 39 42 public class BasePDFTestCase extends TestCase { 43 44 45 protected final FopFactory fopFactory = FopFactory.newInstance(); 46 47 48 protected final TransformerFactory tFactory = TransformerFactory.newInstance(); 49 50 54 protected BasePDFTestCase(String name) { 55 super(name); 56 57 final File uc = getUserConfigFile(); 58 59 try { 60 fopFactory.setUserConfig(uc); 61 } catch (Exception e) { 62 throw new RuntimeException ("fopFactory.setUserConfig (" 63 + uc.getAbsolutePath() + ") failed: " + e.getMessage()); 64 } 65 } 66 67 75 protected byte[] convertFO(File foFile, FOUserAgent ua, boolean dumpPdfFile) 76 throws Exception { 77 ByteArrayOutputStream baout = new ByteArrayOutputStream(); 78 Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, ua, baout); 79 Transformer transformer = tFactory.newTransformer(); 80 Source src = new StreamSource (foFile); 81 SAXResult res = new SAXResult (fop.getDefaultHandler()); 82 transformer.transform(src, res); 83 final byte[] result = baout.toByteArray(); 84 if (dumpPdfFile) { 85 final File outFile = new File (foFile.getParentFile(), foFile.getName() + ".pdf"); 86 FileUtils.writeByteArrayToFile(outFile, result); 87 } 88 return result; 89 } 90 91 92 protected File getUserConfigFile() { 93 return new File ("test/test.xconf"); 94 } 95 } 96 | Popular Tags |