1 17 18 19 20 package org.apache.fop.fotreetest; 21 22 import java.io.File ; 23 import java.util.List ; 24 25 import javax.xml.parsers.SAXParser ; 26 import javax.xml.parsers.SAXParserFactory ; 27 28 import org.apache.fop.apps.FOUserAgent; 29 import org.apache.fop.apps.Fop; 30 import org.apache.fop.apps.FopFactory; 31 32 import org.apache.fop.fotreetest.ext.TestElementMapping; 33 import org.xml.sax.SAXException ; 34 import org.xml.sax.XMLReader ; 35 import org.xml.sax.helpers.XMLFilterImpl ; 36 37 40 public class FOTreeTester { 41 42 private FopFactory fopFactory = FopFactory.newInstance(); 43 44 47 public FOTreeTester() { 48 fopFactory.addElementMapping(new TestElementMapping()); 49 } 50 51 56 public void runTest(File testFile) throws Exception { 57 ResultCollector collector = ResultCollector.getInstance(); 58 collector.reset(); 59 60 SAXParserFactory spf = SAXParserFactory.newInstance(); 61 spf.setNamespaceAware(true); 62 spf.setValidating(false); 63 SAXParser parser = spf.newSAXParser(); 64 XMLReader reader = parser.getXMLReader(); 65 66 FOUserAgent ua = fopFactory.newFOUserAgent(); 67 ua.setBaseURL(testFile.getParentFile().toURL().toString()); 68 ua.setFOEventHandlerOverride(new DummyFOEventHandler(ua)); 69 70 reader = new PIListener(reader, ua); 72 73 Fop fop = fopFactory.newFop(ua); 74 75 reader.setContentHandler(fop.getDefaultHandler()); 76 reader.setDTDHandler(fop.getDefaultHandler()); 77 reader.setErrorHandler(fop.getDefaultHandler()); 78 reader.setEntityResolver(fop.getDefaultHandler()); 79 reader.parse(testFile.toURL().toExternalForm()); 80 81 List results = collector.getResults(); 82 if (results.size() > 0) { 83 for (int i = 0; i < results.size(); i++) { 84 System.out.println(((Exception )results.get(i)).getMessage()); 85 } 86 throw (Exception )results.get(0); 87 } 88 } 89 90 private class PIListener extends XMLFilterImpl { 91 92 private FOUserAgent userAgent; 93 94 public PIListener(XMLReader parent, FOUserAgent userAgent) { 95 super(parent); 96 this.userAgent = userAgent; 97 } 98 99 100 public void processingInstruction(String target, String data) throws SAXException { 101 if ("fop-useragent-break-indent-inheritance".equals(target)) { 102 userAgent.getFactory().setBreakIndentInheritanceOnReferenceAreaBoundary( 103 Boolean.valueOf(data).booleanValue()); 104 } 105 super.processingInstruction(target, data); 106 } 107 108 } 109 110 } 111 | Popular Tags |