1 17 18 19 20 package org.apache.fop.intermediate; 21 22 import java.io.BufferedOutputStream ; 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.OutputStream ; 26 import java.net.MalformedURLException ; 27 28 import javax.xml.transform.Result ; 29 import javax.xml.transform.Source ; 30 import javax.xml.transform.Templates ; 31 import javax.xml.transform.Transformer ; 32 import javax.xml.transform.TransformerConfigurationException ; 33 import javax.xml.transform.dom.DOMResult ; 34 import javax.xml.transform.dom.DOMSource ; 35 import javax.xml.transform.sax.SAXResult ; 36 import javax.xml.transform.sax.SAXTransformerFactory ; 37 import javax.xml.transform.sax.TransformerHandler ; 38 import javax.xml.transform.stream.StreamResult ; 39 import javax.xml.transform.stream.StreamSource ; 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 import org.custommonkey.xmlunit.XMLTestCase; 56 import org.w3c.dom.Document ; 57 58 61 public class AreaTreeParserTestCase extends XMLTestCase { 62 63 private FopFactory fopFactory = FopFactory.newInstance(); 65 66 private static SAXTransformerFactory tFactory 67 = (SAXTransformerFactory )SAXTransformerFactory.newInstance(); 68 private static Templates stylesheet = null; 69 70 private File mainDir = new File ("test/layoutengine"); 71 private File testDir = new File (mainDir, "standard-testcases"); 72 73 private String name; 74 private File testFile; 75 76 private File outputDir; 77 private Document intermediate; 78 79 80 public AreaTreeParserTestCase(String name) { 81 super(name); 82 } 83 84 88 public AreaTreeParserTestCase(File testFile) { 89 super(testFile.getName()); 90 this.testFile = testFile; 91 } 92 93 private Templates getStylesheet() throws TransformerConfigurationException { 94 if (stylesheet == null) { 95 File xsltFile = new File (mainDir, "testcase2fo.xsl"); 96 stylesheet = tFactory.newTemplates(new StreamSource (xsltFile)); 97 } 98 return stylesheet; 99 } 100 101 102 protected void setUp() throws Exception { 103 super.setUp(); 104 String s = System.getProperty("fop.intermediate.outdir"); 105 if (s != null && s.length() > 0) { 106 outputDir = new File (s); 107 outputDir.mkdirs(); 108 } 109 File srcFile; 110 if (testFile != null) { 111 srcFile = testFile; 112 } else { 113 srcFile = new File (testDir, "block_font-style.xml"); 114 } 115 this.name = srcFile.getName(); 116 intermediate = buildAreaTreeXML(new StreamSource (srcFile), getStylesheet()); 117 if (outputDir != null) { 118 saveDOM(intermediate, new File (outputDir, name + ".at1.xml")); 119 } 120 } 121 122 123 128 public void testParserToAT() throws Exception { 129 130 Source src = new DOMSource (intermediate); 131 Document doc = parseAndRenderToAreaTree(src); 132 if (outputDir != null) { 133 File tgtFile = new File (outputDir, name + ".at2.xml"); 134 saveDOM(doc, tgtFile); 135 } 136 137 assertXMLEqual(intermediate, doc); 138 } 139 140 private void saveDOM(Document doc, File tgtFile) throws Exception { 141 Transformer transformer = tFactory.newTransformer(); 142 Source src = new DOMSource (doc); 143 Result res = new StreamResult (tgtFile); 144 transformer.transform(src, res); 145 } 146 147 152 public void testParserToPDF() throws Exception { 153 OutputStream out; 154 if (outputDir != null) { 155 File tgtFile = new File (outputDir, name + ".pdf"); 156 out = new FileOutputStream (tgtFile); 157 out = new BufferedOutputStream (out); 158 } else { 159 out = new ByteArrayOutputStream(); 160 } 161 try { 162 Source src = new DOMSource (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 e) { 174 } 176 return userAgent; 177 } 178 179 private Document buildAreaTreeXML(Source src, Templates stylesheet) throws Exception { 180 Transformer transformer; 181 if (stylesheet != null) { 182 transformer = stylesheet.newTransformer(); 183 } else { 184 transformer = tFactory.newTransformer(); 185 } 186 187 TransformerHandler handler = tFactory.newTransformerHandler(); 189 DOMResult domResult = new DOMResult (); 190 handler.setResult(domResult); 191 192 FOUserAgent userAgent = createUserAgent(); 193 194 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 res = new SAXResult (fop.getDefaultHandler()); 207 transformer.transform(src, res); 208 209 return (Document )domResult.getNode(); 210 } 211 212 private void parseAndRender(Source src, OutputStream out, String mime) throws Exception { 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 parseAndRenderToAreaTree(Source src) throws Exception { 224 AreaTreeParser parser = new AreaTreeParser(); 225 226 TransformerHandler handler = tFactory.newTransformerHandler(); 228 DOMResult domResult = new DOMResult (); 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 )domResult.getNode(); 244 } 245 246 } 247 | Popular Tags |