1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: TransformerHelper.java,v 1.5 2004/02/02 03:50:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.report;
46
47 import java.io.File;
48 import java.io.FileOutputStream;
49 import java.io.IOException;
50 import java.util.HashMap;
51 import java.util.Iterator;
52 import java.util.Map;
53
54 import javax.xml.transform.Templates;
55 import javax.xml.transform.Transformer;
56 import javax.xml.transform.TransformerException;
57 import javax.xml.transform.TransformerFactory;
58 import javax.xml.transform.stream.StreamSource;
59 import javax.xml.transform.stream.StreamResult;
60
61 import org.apache.log4j.Category;
62
63
64 /***
65 * Helper class for performing XML transformations
66 *
67 * @version $Revision: 1.5 $ $Date: 2004/02/02 03:50:24 $
68 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
69 */
70 public final class TransformerHelper {
71
72 /***
73 * The logger
74 */
75 private static final Category log =
76 Category.getInstance(TransformerHelper.class);
77
78 /***
79 * Xdoc file extension
80 */
81 private static final String XDOC_EXT = ".xml";
82
83 /***
84 * HTML file extension
85 */
86 private static final String HTML_EXT = ".html";
87
88 /***
89 * The xdoc to html stylesheet
90 */
91 private static final String XDOC_STYLESHEET = "resources/xdoc2html.xsl";
92
93
94 /***
95 * Prevent construction of utility class
96 */
97 private TransformerHelper() {
98 }
99
100 /***
101 * Transforms a single xdoc file to HTML.
102 *
103 * if the output path is a directory, the output file name will be
104 * the same name as the input path, with a .html extension.
105 *
106 * @param inputPath the path of the file to transform
107 * @param outputPath the file or directory to generate HTML
108 * @param baseDir the base directory path to locate resources
109 * @throws IOException for any I/O error
110 * @throws TransformerException if the stylesheet cannot be loaded or
111 * applied
112 */
113 public static void transformXDoc2HTML(File inputPath, File outputPath,
114 File baseDir)
115 throws IOException, TransformerException {
116 if (outputPath.isDirectory()) {
117 String inputName = inputPath.getName();
118 int length = inputName.length() - HTML_EXT.length() + 1;
119 String outputName = inputName.substring(0, length) + HTML_EXT;
120 outputPath = new File(outputPath, outputName);
121 }
122
123 File stylesheetPath = new File(baseDir, XDOC_STYLESHEET);
124 if (log.isDebugEnabled()) {
125 log.debug("Transforming file " + inputPath
126 + " using stylesheet " + stylesheetPath + " to "
127 + outputPath);
128 }
129
130 Templates stylesheet = getStylesheet(stylesheetPath);
131 transform(inputPath, outputPath, stylesheet, getProperties(baseDir));
132 }
133
134 /***
135 * Transforms all xdoc files in a directory to HTML
136 *
137 * @param inputDir the directory containing the xdocs
138 * @param outputDir the directory to generate HTML
139 * @param baseDir the base directory path to locate resources
140 * @throws IOException for any I/O error
141 * @throws TransformerException if the stylesheet cannot be loaded or
142 * applied
143 */
144 public static void transformAllXDoc2HTML(File inputDir, File outputDir,
145 File baseDir)
146 throws IOException, TransformerException {
147
148 File stylesheetPath = new File(baseDir, XDOC_STYLESHEET);
149 if (log.isDebugEnabled()) {
150 log.debug("Transforming directory " + inputDir
151 + " using stylesheet " + stylesheetPath + " to "
152 + outputDir);
153 }
154
155 String[] files = inputDir.list();
156 if (files == null) {
157 throw new IOException("Failed to list directory: " + inputDir);
158 }
159
160 Templates stylesheet = getStylesheet(stylesheetPath);
161 HashMap properties = getProperties(baseDir);
162
163 for (int i = 0; i < files.length; ++i) {
164 String inputName = files[i];
165 if (inputName.endsWith(XDOC_EXT)) {
166 File input = new File(inputDir, inputName);
167 int length = inputName.length() - HTML_EXT.length() + 1;
168 String outputName = inputName.substring(0, length) + HTML_EXT;
169 File output = new File(outputDir, outputName);
170 transform(input, output, stylesheet, properties);
171 }
172 }
173 }
174
175 /***
176 * Transforms a file
177 *
178 * @param inputPath the path of the file to transform
179 * @param outputPath the path to write the transformed file
180 * @param stylesheetPath the path to the stylesheet to perform the
181 * transformation
182 * @throws IOException for any I/O error
183 * @throws TransformerException if the stylesheet cannot be loaded or
184 * applied
185 */
186 public static void transform(File inputPath, File outputPath,
187 File stylesheetPath)
188 throws IOException, TransformerException {
189
190 Templates stylesheet = getStylesheet(stylesheetPath);
191 transform(inputPath, outputPath, stylesheet, new HashMap());
192 }
193
194 /***
195 * Transforms a file
196 *
197 * @param inputPath the path of the file to transform
198 * @param outputPath the path to write the transformed file
199 * @param stylesheet the stylesheet to perform the transformation
200 * @param properties properties to pass to the transformation
201 * @throws IOException for any I/O error
202 * @throws TransformerException if the stylesheet cannot be loaded or
203 * applied
204 */
205 public static void transform(File inputPath, File outputPath,
206 Templates stylesheet, HashMap properties)
207 throws IOException, TransformerException {
208
209 if (log.isDebugEnabled()) {
210 log.debug("Transforming " + inputPath + " -> " + outputPath);
211 }
212
213 Transformer transformer = stylesheet.newTransformer();
214 Iterator iterator = properties.entrySet().iterator();
215 while (iterator.hasNext()) {
216 Map.Entry entry = (Map.Entry) iterator.next();
217 String name = (String) entry.getKey();
218 Object value = entry.getValue();
219 transformer.setParameter(name, value);
220 }
221
222 transformer.transform(
223 new StreamSource(inputPath),
224 new StreamResult(new FileOutputStream(outputPath)));
225 }
226
227 /***
228 * Loads a stylesheet
229 *
230 * @param path the stylesheet path
231 * @return the stylesheet
232 * @throws TransformerException if the stylesheet cannot be loaded
233 */
234 public static Templates getStylesheet(File path)
235 throws TransformerException {
236 TransformerFactory factory = TransformerFactory.newInstance();
237
238 Templates template = factory.newTemplates(new StreamSource(path));
239 return template;
240 }
241
242 /***
243 * Sets up properties for the xdoc2html stylesheet
244 *
245 * @param baseDir the base directory path to locate resources
246 * @throws IOException for any I/O error
247 * @return a map of the properties
248 */
249 private static HashMap getProperties(File baseDir) throws IOException {
250 File resourcesDir = new File(baseDir, "resources");
251 HashMap properties = new HashMap();
252 properties.put("style-path", resourcesDir.toURL());
253 return properties;
254 }
255
256 }
This page was automatically generated by Maven