1 17 18 19 20 package org.apache.fop.layoutengine; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.lang.reflect.Constructor ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 import javax.xml.transform.Result ; 33 import javax.xml.transform.Source ; 34 import javax.xml.transform.Templates ; 35 import javax.xml.transform.Transformer ; 36 import javax.xml.transform.TransformerConfigurationException ; 37 import javax.xml.transform.TransformerException ; 38 import javax.xml.transform.dom.DOMResult ; 39 import javax.xml.transform.dom.DOMSource ; 40 import javax.xml.transform.sax.SAXResult ; 41 import javax.xml.transform.sax.SAXTransformerFactory ; 42 import javax.xml.transform.sax.TransformerHandler ; 43 import javax.xml.transform.stream.StreamResult ; 44 import javax.xml.transform.stream.StreamSource ; 45 46 import org.apache.fop.apps.FOUserAgent; 47 import org.apache.fop.apps.Fop; 48 import org.apache.fop.apps.FopFactory; 49 import org.apache.fop.apps.FormattingResults; 50 import org.apache.fop.layoutmgr.ElementListObserver; 51 import org.apache.fop.render.xml.XMLRenderer; 52 import org.apache.xpath.XPathAPI; 53 import org.apache.xpath.objects.XObject; 54 import org.w3c.dom.Document ; 55 import org.w3c.dom.Element ; 56 import org.w3c.dom.Node ; 57 import org.w3c.dom.NodeList ; 58 import org.xml.sax.SAXException ; 59 60 64 public class LayoutEngineTester { 65 66 private static final Map CHECK_CLASSES = new java.util.HashMap (); 67 68 private FopFactory fopFactory = FopFactory.newInstance(); 70 private FopFactory fopFactoryWithBase14Kerning = FopFactory.newInstance(); 71 72 private SAXTransformerFactory tfactory 73 = (SAXTransformerFactory )SAXTransformerFactory.newInstance(); 74 75 private Templates testcase2fo; 76 private Templates testcase2checks; 77 78 private File areaTreeBackupDir; 79 80 static { 81 CHECK_CLASSES.put("true", TrueCheck.class); 82 CHECK_CLASSES.put("eval", EvalCheck.class); 83 CHECK_CLASSES.put("element-list", ElementListCheck.class); 84 CHECK_CLASSES.put("result", ResultCheck.class); 85 } 86 87 92 public LayoutEngineTester(File areaTreeBackupDir) { 93 this.areaTreeBackupDir = areaTreeBackupDir; 94 fopFactory.setBase14KerningEnabled(false); 95 fopFactoryWithBase14Kerning.setBase14KerningEnabled(true); 96 } 97 98 private Templates getTestcase2FOStylesheet() throws TransformerConfigurationException { 99 if (testcase2fo == null) { 100 Source src = new StreamSource (new File ("test/layoutengine/testcase2fo.xsl")); 102 testcase2fo = tfactory.newTemplates(src); 103 } 104 return testcase2fo; 105 } 106 107 private Templates getTestcase2ChecksStylesheet() throws TransformerConfigurationException { 108 if (testcase2checks == null) { 109 Source src = new StreamSource (new File ("test/layoutengine/testcase2checks.xsl")); 111 testcase2checks = tfactory.newTemplates(src); 112 } 113 return testcase2checks; 114 } 115 116 124 public void runTest(File testFile) 125 throws TransformerException , SAXException , IOException , ParserConfigurationException { 126 127 DOMResult domres = new DOMResult (); 128 129 ElementListCollector elCollector = new ElementListCollector(); 130 ElementListObserver.addObserver(elCollector); 131 132 Fop fop; 133 134 try { 135 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 136 dbf.setNamespaceAware(true); 137 dbf.setValidating(false); 138 DocumentBuilder builder = dbf.newDocumentBuilder(); 139 Document testDoc = builder.parse(testFile); 140 141 XObject xo = XPathAPI.eval(testDoc, "/testcase/cfg/base14kerning"); 142 String s = xo.str(); 143 boolean base14kerning = ("true".equalsIgnoreCase(s)); 144 FopFactory effFactory = (base14kerning ? fopFactoryWithBase14Kerning : fopFactory); 145 146 Transformer transformer = getTestcase2FOStylesheet().newTransformer(); 148 Source src = new DOMSource (testDoc); 149 150 TransformerHandler athandler = tfactory.newTransformerHandler(); 152 athandler.setResult(domres); 153 154 FOUserAgent ua = effFactory.newFOUserAgent(); 156 ua.setBaseURL(testFile.getParentFile().toURL().toString()); 157 XMLRenderer atrenderer = new XMLRenderer(); 158 atrenderer.setUserAgent(ua); 159 atrenderer.setContentHandler(athandler); 160 ua.setRendererOverride(atrenderer); 161 fop = effFactory.newFop(ua); 162 163 SAXResult fores = new SAXResult (fop.getDefaultHandler()); 164 transformer.transform(src, fores); 165 } finally { 166 ElementListObserver.removeObserver(elCollector); 167 } 168 169 Document doc = (Document )domres.getNode(); 170 if (this.areaTreeBackupDir != null) { 171 saveAreaTreeXML(doc, new File (this.areaTreeBackupDir, testFile.getName() + ".at.xml")); 172 } 173 FormattingResults results = fop.getResults(); 174 LayoutResult result = new LayoutResult(doc, elCollector, results); 175 checkAll(testFile, result); 176 } 177 178 183 protected LayoutEngineCheck createCheck(Element el) { 184 String name = el.getTagName(); 185 Class clazz = (Class )CHECK_CLASSES.get(name); 186 if (clazz != null) { 187 try { 188 Constructor c = clazz.getDeclaredConstructor(new Class [] {Node .class}); 189 LayoutEngineCheck instance = (LayoutEngineCheck)c.newInstance(new Object [] {el}); 190 return instance; 191 } catch (Exception e) { 192 throw new RuntimeException ("Error while instantiating check '" 193 + name + "': " + e.getMessage()); 194 } 195 } else { 196 throw new IllegalArgumentException ("No check class found: " + name); 197 } 198 } 199 200 206 protected void checkAll(File testFile, LayoutResult result) throws TransformerException { 207 Transformer transformer = getTestcase2ChecksStylesheet().newTransformer(); 208 Source src = new StreamSource (testFile); 209 DOMResult res = new DOMResult (); 210 transformer.transform(src, res); 211 212 List checks = new java.util.ArrayList (); 213 Document doc = (Document )res.getNode(); 214 NodeList nodes = doc.getDocumentElement().getChildNodes(); 215 for (int i = 0; i < nodes.getLength(); i++) { 216 Node node = nodes.item(i); 217 if (node instanceof Element ) { 218 checks.add(createCheck((Element )node)); 219 } 220 } 221 222 if (checks.size() == 0) { 223 throw new RuntimeException ("No checks are available!"); 224 } 225 Iterator i = checks.iterator(); 226 while (i.hasNext()) { 227 LayoutEngineCheck check = (LayoutEngineCheck)i.next(); 228 check.check(result); 229 } 230 } 231 232 238 protected void saveAreaTreeXML(Document doc, File target) throws TransformerException { 239 Transformer transformer = tfactory.newTransformer(); 240 Source src = new DOMSource (doc); 241 Result res = new StreamResult (target); 242 transformer.transform(src, res); 243 } 244 } 245 | Popular Tags |