001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.test.xml;
020:
021: import java.io.File;
022: import java.io.FileOutputStream;
023:
024: import javax.xml.transform.TransformerFactory;
025: import javax.xml.transform.Transformer;
026: import javax.xml.transform.stream.StreamSource;
027: import javax.xml.transform.stream.StreamResult;
028:
029: /*import org.apache.xalan.xslt.XSLTProcessorFactory;
030: import org.apache.xalan.xslt.XSLTInputSource;
031: import org.apache.xalan.xslt.XSLTResultTarget;
032: import org.apache.xalan.xslt.XSLTProcessor;*/
033:
034: import org.apache.batik.test.TestException;
035:
036: /**
037: * This implementation of the <tt>XMLTestReportProcessor.XMLReportConsumer</tt>
038: * interface simply applies an XSL transformation to the input
039: * XML file and stores the result in a configurable directory.
040: *
041: * @author <a href="mailto:vhardy@apache.org">Vincent Hardy</a>
042: * @version $Id: XSLXMLReportConsumer.java 475477 2006-11-15 22:44:28Z cam $
043: */
044: public class XSLXMLReportConsumer implements
045: XMLTestReportProcessor.XMLReportConsumer {
046: /**
047: * Error code used when the output directory cannot be used
048: */
049: public static final String ERROR_OUTPUT_DIRECTORY_UNUSABLE = "xml.XSLXMLReportConsumer.error.output.directory.unusable";
050:
051: /**
052: * Stylesheet URI
053: */
054: private String stylesheet;
055:
056: /**
057: * Output directory, i.e., the directory where the result
058: * of the XSL transformation will be stored.
059: */
060: private String outputDirectory;
061:
062: /**
063: * Output file name
064: */
065: private String outputFileName;
066:
067: /**
068: * Constructor
069: * @param stylesheet URI for the stylesheet to apply to the XML report
070: * @param outputDirectory directory where the result of the XSL transformation
071: * should be written
072: * @param outputFileName name of the output report.
073: */
074: public XSLXMLReportConsumer(String stylesheet,
075: String outputDirectory, String outputFileName) {
076: this .stylesheet = stylesheet;
077: this .outputDirectory = outputDirectory;
078: this .outputFileName = outputFileName;
079: }
080:
081: /**
082: * When a new report has been generated, this consumer
083: * applies the same stylesheet to the input XML document
084: */
085: public void onNewReport(File xmlReport, File reportDirectory)
086: throws Exception {
087:
088: TransformerFactory tFactory = TransformerFactory.newInstance();
089: Transformer transformer = tFactory
090: .newTransformer(new StreamSource(stylesheet));
091:
092: transformer.transform(new StreamSource(xmlReport.toURL()
093: .toString()), new StreamResult(new FileOutputStream(
094: createNewReportOutput(reportDirectory)
095: .getAbsolutePath())));
096: }
097:
098: /**
099: * Returns a new file in the outputDirectory, with
100: * the requested report name.
101: */
102: public File createNewReportOutput(File reportDirectory)
103: throws Exception {
104: File dir = new File(reportDirectory, outputDirectory);
105: checkDirectory(dir);
106: return new File(dir, outputFileName);
107: }
108:
109: /**
110: * Checks that the input File represents a directory that
111: * can be used. If the directory does not exist, this method
112: * will attempt to create it.
113: */
114: public void checkDirectory(File dir) throws TestException {
115: boolean dirOK = false;
116: try {
117: if (!dir.exists()) {
118: dirOK = dir.mkdir();
119: } else if (dir.isDirectory()) {
120: dirOK = true;
121: }
122: } finally {
123: if (!dirOK) {
124: throw new TestException(
125: ERROR_OUTPUT_DIRECTORY_UNUSABLE,
126: new Object[] { dir.getAbsolutePath() }, null);
127:
128: }
129: }
130: }
131:
132: }
|