KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > test > xml > XSLXMLReportConsumer


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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 package org.apache.batik.test.xml;
19
20 import java.io.File JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22
23 import javax.xml.transform.TransformerFactory JavaDoc;
24 import javax.xml.transform.Transformer JavaDoc;
25 import javax.xml.transform.stream.StreamSource JavaDoc;
26 import javax.xml.transform.stream.StreamResult JavaDoc;
27
28 /*import org.apache.xalan.xslt.XSLTProcessorFactory;
29 import org.apache.xalan.xslt.XSLTInputSource;
30 import org.apache.xalan.xslt.XSLTResultTarget;
31 import org.apache.xalan.xslt.XSLTProcessor;*/

32
33 import org.apache.batik.test.TestException;
34
35 /**
36  * This implementation of the <tt>XMLTestReportProcessor.XMLReportConsumer</tt>
37  * interface simply applies an XSL transformation to the input
38  * XML file and stores the result in a configurable directory.
39  *
40  * @author <a HREF="mailto:vhardy@apache.org">Vincent Hardy</a>
41  * @version $Id: XSLXMLReportConsumer.java,v 1.7 2004/08/18 07:17:08 vhardy Exp $
42  */

43 public class XSLXMLReportConsumer
44     implements XMLTestReportProcessor.XMLReportConsumer {
45     /**
46      * Error code used when the output directory cannot be used
47      */

48     public static final String JavaDoc ERROR_OUTPUT_DIRECTORY_UNUSABLE
49         = "xml.XSLXMLReportConsumer.error.output.directory.unusable";
50
51     /**
52      * Stylesheet URI
53      */

54     private String JavaDoc stylesheet;
55
56     /**
57      * Output directory, i.e., the directory where the result
58      * of the XSL transformation will be stored.
59      */

60     private String JavaDoc outputDirectory;
61
62     /**
63      * Output file name
64      */

65     private String JavaDoc outputFileName;
66
67     /**
68      * Constructor
69      * @param stylesheet URI for the stylesheet to apply to the XML report
70      * @param outputDirectory directory where the result of the XSL transformation
71      * should be written
72      * @param outputFileName name of the output report.
73      */

74     public XSLXMLReportConsumer(String JavaDoc stylesheet,
75                                 String JavaDoc outputDirectory,
76                                 String JavaDoc outputFileName){
77         this.stylesheet = stylesheet;
78         this.outputDirectory = outputDirectory;
79         this.outputFileName = outputFileName;
80     }
81
82     /**
83      * When a new report has been generated, this consumer
84      * applies the same stylesheet to the input XML document
85      */

86     public void onNewReport(File JavaDoc xmlReport,
87                             File JavaDoc reportDirectory)
88         throws Exception JavaDoc{
89
90         TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
91         Transformer JavaDoc transformer = tFactory.newTransformer(new StreamSource JavaDoc(stylesheet));
92         
93         transformer.transform(new StreamSource JavaDoc(xmlReport.toURL().toString()),
94                               new StreamResult JavaDoc(new FileOutputStream JavaDoc(createNewReportOutput(reportDirectory).getAbsolutePath())));
95     }
96     
97     /**
98      * Returns a new file in the outputDirectory, with
99      * the requested report name.
100      */

101     public File JavaDoc createNewReportOutput(File JavaDoc reportDirectory) throws Exception JavaDoc{
102         File JavaDoc dir = new File JavaDoc(reportDirectory, outputDirectory);
103         checkDirectory(dir);
104         return new File JavaDoc(dir, outputFileName);
105     }
106
107     /**
108      * Checks that the input File represents a directory that
109      * can be used. If the directory does not exist, this method
110      * will attempt to create it.
111      */

112     public void checkDirectory(File JavaDoc dir)
113         throws TestException {
114         boolean dirOK = false;
115         try{
116             if(!dir.exists()){
117                 dirOK = dir.mkdir();
118             }
119             else if(dir.isDirectory()){
120                 dirOK = true;
121             }
122         }finally{
123             if(!dirOK){
124                 throw new TestException(ERROR_OUTPUT_DIRECTORY_UNUSABLE,
125                                         new Object JavaDoc[] {dir.getAbsolutePath()},
126                                         null);
127                 
128             }
129         }
130     }
131
132 }
133
Popular Tags