KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fotreetest > FOTreeTester


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: FOTreeTester.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.fotreetest;
21
22 import java.io.File JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.xml.parsers.SAXParser JavaDoc;
26 import javax.xml.parsers.SAXParserFactory JavaDoc;
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 JavaDoc;
34 import org.xml.sax.XMLReader JavaDoc;
35 import org.xml.sax.helpers.XMLFilterImpl JavaDoc;
36
37 /**
38  * Test driver class for FO tree tests.
39  */

40 public class FOTreeTester {
41     
42     private FopFactory fopFactory = FopFactory.newInstance();
43
44     /**
45      * Main constructor
46      */

47     public FOTreeTester() {
48         fopFactory.addElementMapping(new TestElementMapping());
49     }
50     
51     /**
52      * Runs a test.
53      * @param testFile the test file.
54      * @throws Exception if a test or FOP itself fails
55      */

56     public void runTest(File JavaDoc testFile) throws Exception JavaDoc {
57         ResultCollector collector = ResultCollector.getInstance();
58         collector.reset();
59         
60         SAXParserFactory JavaDoc spf = SAXParserFactory.newInstance();
61         spf.setNamespaceAware(true);
62         spf.setValidating(false);
63         SAXParser JavaDoc parser = spf.newSAXParser();
64         XMLReader JavaDoc reader = parser.getXMLReader();
65                 
66         FOUserAgent ua = fopFactory.newFOUserAgent();
67         ua.setBaseURL(testFile.getParentFile().toURL().toString());
68         ua.setFOEventHandlerOverride(new DummyFOEventHandler(ua));
69
70         //Used to set values in the user agent through processing instructions
71
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 JavaDoc results = collector.getResults();
82         if (results.size() > 0) {
83             for (int i = 0; i < results.size(); i++) {
84                 System.out.println(((Exception JavaDoc)results.get(i)).getMessage());
85             }
86             throw (Exception JavaDoc)results.get(0);
87         }
88     }
89
90     private class PIListener extends XMLFilterImpl JavaDoc {
91         
92         private FOUserAgent userAgent;
93         
94         public PIListener(XMLReader JavaDoc parent, FOUserAgent userAgent) {
95             super(parent);
96             this.userAgent = userAgent;
97         }
98
99         /** @see org.xml.sax.helpers.XMLFilterImpl */
100         public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
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