1 51 package org.apache.fop.tools; 52 53 import org.apache.fop.apps.*; 54 import org.apache.fop.configuration.*; 55 import org.apache.fop.messaging.MessageHandler; 56 57 import org.apache.avalon.framework.logger.ConsoleLogger; 58 import org.apache.avalon.framework.logger.Logger; 59 60 import java.io.*; 61 import java.util.*; 62 63 import javax.xml.parsers.*; 64 65 import org.w3c.dom.*; 66 import org.xml.sax.XMLReader ; 67 import org.xml.sax.SAXException ; 68 69 82 public class TestConverter { 83 boolean failOnly = false; 84 boolean outputPDF = false; 85 File destdir; 86 File compare = null; 87 String baseDir = "./"; 88 HashMap differ = new HashMap(); 89 private Logger log; 90 91 101 public static void main(String [] args) { 102 if (args == null || args.length == 0) { 103 System.out.println("test suite file name required"); 104 } 105 TestConverter tc = new TestConverter(); 106 107 String testFile = null; 108 for (int count = 0; count < args.length; count++) { 109 if (args[count].equals("-failOnly")) { 110 tc.setFailOnly(true); 111 } else if (args[count].equals("-pdf")) { 112 tc.setOutputPDF(true); 113 } else if (args[count].equals("-b")) { 114 tc.setBaseDir(args[count + 1]); 115 } else { 116 testFile = args[count]; 117 } 118 } 119 if (testFile == null) { 120 System.out.println("test suite file name required"); 121 } 122 123 tc.runTests(testFile, "results", null); 124 } 125 126 public TestConverter() { 127 setupLogging(); 128 } 129 130 private void setupLogging() { 131 log = new ConsoleLogger(ConsoleLogger.LEVEL_ERROR); 132 MessageHandler.setScreenLogger(log); 133 } 134 135 public void setOutputPDF(boolean pdf) { 136 outputPDF = pdf; 137 } 138 139 public void setFailOnly(boolean fail) { 140 failOnly = fail; 141 } 142 143 public void setBaseDir(String str) { 144 baseDir = str; 145 } 146 147 152 public HashMap runTests(String fname, String dest, String compDir) { 153 log.debug("running tests in file:" + fname); 154 try { 155 if (compDir != null) { 156 compare = new File(baseDir + "/" + compDir); 157 } 158 destdir = new File(baseDir + "/" + dest); 159 destdir.mkdirs(); 160 File f = new File(baseDir + "/" + fname); 161 DocumentBuilderFactory factory = 162 DocumentBuilderFactory.newInstance(); 163 DocumentBuilder db = factory.newDocumentBuilder(); 164 Document doc = db.parse(f); 165 166 NodeList suitelist = doc.getChildNodes(); 167 if (suitelist.getLength() == 0) { 168 return differ; 169 } 170 171 Node testsuite = null; 172 testsuite = doc.getDocumentElement(); 173 174 if (testsuite.hasAttributes()) { 175 String profile = 176 testsuite.getAttributes().getNamedItem("profile").getNodeValue(); 177 log.debug("testing test suite:" + profile); 178 } 179 180 NodeList testcases = testsuite.getChildNodes(); 181 for (int count = 0; count < testcases.getLength(); count++) { 182 Node testcase = testcases.item(count); 183 if (testcase.getNodeName().equals("testcases")) { 184 runTestCase(testcase); 185 } 186 } 187 } catch (Exception e) { 188 e.printStackTrace(); 189 } 190 return differ; 191 } 192 193 199 protected void runTestCase(Node tcase) { 200 if (tcase.hasAttributes()) { 201 String profile = 202 tcase.getAttributes().getNamedItem("profile").getNodeValue(); 203 log.debug("testing profile:" + profile); 204 } 205 206 NodeList cases = tcase.getChildNodes(); 207 for (int count = 0; count < cases.getLength(); count++) { 208 Node node = cases.item(count); 209 String nodename = node.getNodeName(); 210 if (nodename.equals("testcases")) { 211 runTestCase(node); 212 } else if (nodename.equals("test")) { 213 runTest(tcase, node); 214 } else if (nodename.equals("result")) {} 215 216 } 217 218 } 219 220 227 protected void runTest(Node testcase, Node test) { 228 String id = test.getAttributes().getNamedItem("id").getNodeValue(); 229 Node result = locateResult(testcase, id); 230 boolean pass = false; 231 if (result != null) { 232 String agreement = 233 result.getAttributes().getNamedItem("agreement").getNodeValue(); 234 pass = agreement.equals("full"); 235 } 236 237 if (pass && failOnly) { 238 return; 239 } 240 241 String xml = test.getAttributes().getNamedItem("xml").getNodeValue(); 242 Node xslNode = test.getAttributes().getNamedItem("xsl"); 243 String xsl = null; 244 if (xslNode != null) { 245 xsl = xslNode.getNodeValue(); 246 } 247 log.debug("converting xml:" + xml + " and xsl:" + 248 xsl + " to area tree"); 249 250 try { 251 File xmlFile = new File(baseDir + "/" + xml); 252 253 try { 254 Configuration.put("baseDir", 255 xmlFile.getParentFile().toURL().toExternalForm()); 256 } catch (Exception e) { 257 log.error("Error setting base directory"); 258 } 259 260 InputHandler inputHandler = null; 261 if (xsl == null) { 262 inputHandler = new FOInputHandler(xmlFile); 263 } else { 264 inputHandler = new XSLTInputHandler(xmlFile, 265 new File(baseDir + "/" 266 + xsl)); 267 } 268 269 XMLReader parser = inputHandler.getParser(); 270 setParserFeatures(parser); 271 272 Logger logger = log.getChildLogger("fop"); 273 Driver driver = new Driver(); 274 driver.setLogger(logger); 275 if (outputPDF) { 276 driver.setRenderer(Driver.RENDER_PDF); 277 } else { 278 driver.setRenderer(Driver.RENDER_XML); 279 } 280 281 HashMap rendererOptions = new HashMap(); 282 rendererOptions.put("fineDetail", new Boolean (false)); 283 rendererOptions.put("consistentOutput", new Boolean (true)); 284 driver.getRenderer().setOptions(rendererOptions); 285 driver.getRenderer().setProducer("Testsuite Converter"); 286 287 String outname = xmlFile.getName(); 288 if (outname.endsWith(".xml")) { 289 outname = outname.substring(0, outname.length() - 4); 290 } 291 driver.setOutputStream(new BufferedOutputStream( 292 new FileOutputStream(new File(destdir, 293 outname + (outputPDF ? ".pdf" : ".at.xml"))))); 294 log.debug("ddir:" + destdir + " on:" + outname + ".pdf"); 295 driver.render(parser, inputHandler.getInputSource()); 296 297 if (compare != null) { 299 File f1 = new File(destdir, outname + ".at.xml"); 300 File f2 = new File(compare, outname + ".at.xml"); 301 if (!compareFiles(f1, f2)) { 302 differ.put(outname + ".at.xml", new Boolean (pass)); 303 } 304 } 305 } catch (Exception e) { 306 e.printStackTrace(); 307 } 308 } 309 310 314 protected boolean compareFiles(File f1, File f2) { 315 if(f1.length() != f2.length()) { 316 return false; 317 } 318 try { 319 InputStream is1 = new BufferedInputStream(new FileInputStream(f1)); 320 InputStream is2 = new BufferedInputStream(new FileInputStream(f2)); 321 while (true) { 322 int ch1 = is1.read(); 323 int ch2 = is2.read(); 324 if (ch1 == ch2) { 325 if (ch1 == -1) { 326 return true; 327 } 328 } else { 329 return false; 330 } 331 } 332 } catch (Exception e) {} 333 334 return false; 335 } 336 337 public void setParserFeatures(XMLReader parser) throws FOPException { 338 try { 339 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", 340 true); 341 } catch (SAXException e) { 342 throw new FOPException("Error in setting up parser feature namespace-prefixes\n" 343 + "You need a parser which supports SAX version 2", e); 344 } 345 } 346 347 protected Node locateResult(Node testcase, String id) { 348 NodeList cases = testcase.getChildNodes(); 349 for (int count = 0; count < cases.getLength(); count++) { 350 Node node = cases.item(count); 351 String nodename = node.getNodeName(); 352 if (nodename.equals("result")) { 353 String resultid = 354 node.getAttributes().getNamedItem("id").getNodeValue(); 355 if (id.equals(resultid)) { 356 return node; 357 } 358 } 359 } 360 return null; 361 } 362 363 } 364 | Popular Tags |