1 /***
2 * Copyright 2004 Fabrizio Giustina.
3 *
4 * Licensed under the Artistic License; you may not use this file
5 * except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://maven-taglib.sourceforge.net/license.html
9 *
10 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
11 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
12 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 */
14 package net.sf.maventaglib;
15
16 import java.io.BufferedInputStream;
17 import java.io.BufferedOutputStream;
18 import java.io.CharArrayReader;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25
26 import javax.xml.parsers.DocumentBuilder;
27 import javax.xml.parsers.DocumentBuilderFactory;
28 import javax.xml.parsers.FactoryConfigurationError;
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.parsers.SAXParserFactory;
31 import javax.xml.transform.Source;
32 import javax.xml.transform.Transformer;
33 import javax.xml.transform.TransformerException;
34 import javax.xml.transform.TransformerFactory;
35 import javax.xml.transform.sax.SAXSource;
36 import javax.xml.transform.stream.StreamResult;
37 import javax.xml.transform.stream.StreamSource;
38
39 import org.apache.commons.lang.SystemUtils;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42 import org.apache.xalan.processor.TransformerFactoryImpl;
43 import org.xml.sax.EntityResolver;
44 import org.xml.sax.InputSource;
45 import org.xml.sax.SAXException;
46 import org.xml.sax.XMLReader;
47
48
49 /***
50 * Base class for the maven taglib plugin.
51 * @author Fabrizio Giustina
52 * @version $Id: TaglibPlugin.java,v 1.13 2005/08/23 12:29:52 fgiust Exp $
53 */
54 public abstract class TaglibPlugin
55 {
56
57 /***
58 * logger.
59 */
60 protected Log log = LogFactory.getLog(TaglibPlugin.class);
61
62 /***
63 * tld directory path.
64 */
65 protected String srcDir;
66
67 /***
68 * tld directory path.
69 */
70 protected String tldSrc;
71
72 /***
73 * output dir for converted tlds.
74 */
75 protected String outputDir;
76
77 /***
78 * converted tld file name.
79 */
80 protected String tldOut;
81
82 /***
83 * Instantiate a new Plugin and set up transformer factory.
84 */
85 public TaglibPlugin()
86 {
87 if (SystemUtils.JAVA_VERSION_INT >= 150)
88 {
89 System.setProperty(
90 "javax.xml.transform.TransformerFactory",
91 "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
92 }
93 else
94 {
95 System.setProperty(
96 "javax.xml.transform.TransformerFactory",
97 "org.apache.xalan.processor.TransformerFactoryImpl");
98 }
99 }
100
101 /***
102 * @param dir The outputDir to set.
103 */
104 public void setOutputDir(String dir)
105 {
106 this.outputDir = dir;
107 }
108
109 /***
110 * @param file The tldOut to set.
111 */
112 public void setTldOut(String file)
113 {
114 this.tldOut = file;
115 }
116
117 /***
118 * @param file The tldSrc to set.
119 */
120 public void setTldSrc(String file)
121 {
122 this.tldSrc = file;
123 }
124
125 /***
126 * @param path The full path for the tld directory.
127 */
128 public void setSrcDir(String path)
129 {
130 this.srcDir = path;
131 }
132
133 /***
134 * Executes the plugin goal.
135 * @throws Exception any specific exception thrown by a plugin implementation.
136 */
137 public abstract void execute() throws Exception;
138
139 /***
140 * Transform a file using the given xsl stylesheet.
141 * @param inputFile input file
142 * @param stylesheet xslt used for transformation
143 * @param outputFile output file
144 * @throws Exception xml parsing/transforming exceptions
145 */
146 protected void applyXslt(File inputFile, String stylesheet, File outputFile) throws Exception
147 {
148
149 InputStream fis = null;
150 try
151 {
152 fis = new BufferedInputStream(new FileInputStream(inputFile));
153 XMLReader reader = getReader();
154 Source src = new SAXSource(reader, new InputSource(fis));
155 applyXslt(src, stylesheet, outputFile);
156 }
157 catch (TransformerException e)
158 {
159 echo("Unable to complete xslt transformation from "
160 + inputFile.getAbsolutePath()
161 + " to "
162 + outputFile.getAbsolutePath()
163 + " using "
164 + stylesheet
165 + ": "
166 + e.getMessage());
167 }
168 finally
169 {
170 try
171 {
172 if (fis != null)
173 {
174 fis.close();
175 }
176 }
177 catch (IOException ignored)
178 {
179
180 }
181 }
182 }
183
184 /***
185 * Apply an xsl stylesheet to a java.xml.tranform.Source.
186 * @param src Source
187 * @param stylesheet xslt used for transformation
188 * @param outputFile output file
189 * @throws Exception xml parsing/transforming exceptions
190 */
191 protected void applyXslt(Source src, String stylesheet, File outputFile) throws Exception
192 {
193 OutputStream fos = null;
194 outputFile.getParentFile().mkdirs();
195
196 try
197 {
198 fos = new BufferedOutputStream(new FileOutputStream(outputFile));
199
200
201 StreamResult res = new StreamResult(fos);
202
203
204 InputStream xsl = TaglibPlugin.class.getResourceAsStream(stylesheet);
205 if (xsl == null)
206 {
207 throw new Exception("Can't find stylesheet " + stylesheet);
208 }
209 Transformer transformer = getTransformerFactory().newTransformer(new StreamSource(xsl));
210
211 transformer.transform(src, res);
212 }
213 finally
214 {
215 try
216 {
217 if (fos != null)
218 {
219 fos.close();
220 }
221 }
222 catch (IOException ignored)
223 {
224
225 }
226 }
227
228 log.info("applyXslt done");
229 }
230
231 /***
232 * Returns a XMLReader instance.
233 * @return XMLReader instance
234 * @throws SAXException for errors on building the sax parser
235 * @throws ParserConfigurationException if a SAX parser is not configured
236 */
237 protected XMLReader getReader() throws SAXException, ParserConfigurationException
238 {
239 SAXParserFactory factory = SAXParserFactory.newInstance();
240 factory.setNamespaceAware(true);
241 factory.setValidating(false);
242 XMLReader reader = factory.newSAXParser().getXMLReader();
243 reader.setEntityResolver(new EntityResolver()
244 {
245
246 public InputSource resolveEntity(String publicId, String systemId)
247 {
248 return new InputSource(new CharArrayReader(new char[0]));
249 }
250 });
251 return reader;
252 }
253
254 /***
255 * Returns a DocumentBuilder instance.
256 * @return DocumentBuilder instance
257 * @throws FactoryConfigurationError if the parser is not configured
258 * @throws ParserConfigurationException if the parser is not configured
259 */
260 protected DocumentBuilder getDocumentBuilder() throws ParserConfigurationException, FactoryConfigurationError
261 {
262 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
263
264 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
265 factory.setValidating(false);
266 factory.setNamespaceAware(false);
267 factory.setExpandEntityReferences(false);
268 builder.setEntityResolver(new EntityResolver()
269 {
270
271 public InputSource resolveEntity(String publicId, String systemId)
272 {
273 return new InputSource(new CharArrayReader(new char[0]));
274 }
275 });
276
277 return builder;
278 }
279
280 /***
281 * Returns a transformer factory instance.
282 * @return TransformerFactory instance
283 */
284 private TransformerFactory getTransformerFactory()
285 {
286
287 return new TransformerFactoryImpl();
288 }
289
290 /***
291 * echo a message (using System.out).
292 * @param message output text.
293 */
294 protected void echo(String message)
295 {
296 System.out
297 .println(message);
298 }
299
300 }