KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > ant > StyleTransformer


1 /*
2  * @(#)StyleTransformer.java
3  *
4  * Copyright (C) 2003-2004 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.codecoverage.v2.ant;
28
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.text.DateFormat JavaDoc;
34 import java.util.Date JavaDoc;
35
36 import javax.xml.parsers.DocumentBuilder JavaDoc;
37 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
38 import javax.xml.transform.Result JavaDoc;
39 import javax.xml.transform.Source JavaDoc;
40 import javax.xml.transform.Transformer JavaDoc;
41 import javax.xml.transform.TransformerFactory JavaDoc;
42 import javax.xml.transform.dom.DOMResult JavaDoc;
43 import javax.xml.transform.dom.DOMSource JavaDoc;
44 import javax.xml.transform.stream.StreamResult JavaDoc;
45 import javax.xml.transform.stream.StreamSource JavaDoc;
46
47 import org.apache.tools.ant.BuildException;
48 import org.apache.tools.ant.Project;
49 import org.w3c.dom.Document JavaDoc;
50 import org.w3c.dom.Node JavaDoc;
51
52
53 /**
54  * Transforms XML with XSL. Stolen from the JUnit optional task... kind of.
55  *
56  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
57  * @version $Date: 2004/04/15 05:48:25 $
58  * @since November 22, 2003
59  */

60 public class StyleTransformer
61 {
62     private Project project;
63     private Transformer JavaDoc tformer;
64     
65     public StyleTransformer( Project proj, String JavaDoc stylesheetID, File JavaDoc outDir )
66             throws BuildException
67     {
68         this.project = proj;
69         
70         try
71         {
72             TransformerFactory JavaDoc tfactory = TransformerFactory.newInstance();
73             Source JavaDoc xslSrc = new StreamSource JavaDoc( stylesheetID );
74             this.tformer = tfactory.newTransformer( xslSrc );
75             this.tformer.setParameter("output.dir", outDir.getAbsolutePath());
76         }
77         catch (javax.xml.transform.TransformerConfigurationException JavaDoc e)
78         {
79             throw new BuildException( "Error creating XSL transformer: "+
80                 e.getMessage(), e );
81         }
82
83     }
84     
85     
86     public void setParameter( String JavaDoc name, Object JavaDoc val )
87     {
88         this.tformer.setParameter( name, val );
89     }
90     
91     
92     
93     public Document JavaDoc transform( Document JavaDoc in )
94             throws BuildException
95     {
96         DOMResult JavaDoc result = new DOMResult JavaDoc();
97         DOMSource JavaDoc source = new DOMSource JavaDoc( in );
98         execute( source, result );
99         return getDocument( result.getNode() );
100     }
101     
102     
103     public void transform( Document JavaDoc in, File JavaDoc outFile )
104             throws BuildException, IOException JavaDoc
105     {
106         checkFileTree( outFile );
107         Source JavaDoc source = new DOMSource JavaDoc( in );
108         FileOutputStream JavaDoc fos = null;
109         try
110         {
111             fos = new FileOutputStream JavaDoc( outFile );
112             Result JavaDoc result = new StreamResult JavaDoc( fos );
113             execute( source, result );
114         }
115         finally
116         {
117             if (fos != null)
118             {
119                 fos.close();
120             }
121         }
122     }
123     
124     
125     public Document JavaDoc transform( File JavaDoc in )
126             throws BuildException, IOException JavaDoc
127     {
128         DOMResult JavaDoc result = new DOMResult JavaDoc();
129         FileInputStream JavaDoc fis = null;
130         try
131         {
132             fis = new FileInputStream JavaDoc( in );
133             Source JavaDoc source = new StreamSource JavaDoc( in );
134             execute( source, result );
135         }
136         finally
137         {
138             if (fis != null)
139             {
140                 fis.close();
141             }
142         }
143         return getDocument( result.getNode() );
144     }
145     
146     
147     public void transform( File JavaDoc in, File JavaDoc outFile )
148             throws BuildException, IOException JavaDoc
149     {
150         checkFileTree( outFile );
151         FileOutputStream JavaDoc fos = null;
152         FileInputStream JavaDoc fis = null;
153         try
154         {
155             fos = new FileOutputStream JavaDoc( outFile );
156             fis = new FileInputStream JavaDoc( in );
157             Source JavaDoc source = new StreamSource JavaDoc( fis );
158             Result JavaDoc result = new StreamResult JavaDoc( fos );
159             execute( source, result );
160         }
161         finally
162         {
163             if (fos != null)
164             {
165                 fos.close();
166             }
167             if (fis != null)
168             {
169                 fis.close();
170             }
171         }
172     }
173     
174     
175     protected final void execute( Source JavaDoc src, Result JavaDoc res )
176             throws BuildException
177     {
178         final long t0 = System.currentTimeMillis();
179         tformer.setParameter( "TSTAMP", DateFormat.getDateTimeInstance().
180             format( new Date JavaDoc( t0 ) ) );
181         try
182         {
183             tformer.transform( src, res );
184         }
185         catch (RuntimeException JavaDoc ex)
186         {
187             throw ex;
188         }
189         catch (Exception JavaDoc e)
190         {
191             throw new BuildException( "Errors while applying transformations: "
192                 + e.getMessage(), e );
193         }
194         finally
195         {
196             final long dt = System.currentTimeMillis() - t0;
197             this.project.log( "Transform time: " + dt + "ms",
198                 Project.MSG_DEBUG );
199         }
200     }
201     
202     
203     private final void checkFileTree( File JavaDoc f )
204             throws IOException JavaDoc
205     {
206         File JavaDoc p = f.getParentFile();
207         p.mkdirs();
208     }
209     
210     
211     private static Document JavaDoc getDocument( Node JavaDoc child )
212     {
213         if (child instanceof Document JavaDoc)
214         {
215             return (Document JavaDoc)child;
216         }
217         Document JavaDoc doc = child.getOwnerDocument();
218         if (doc == null)
219         {
220             doc = getDocumentBuilder().newDocument();
221             Node JavaDoc c = doc.importNode( child, true );
222             doc.appendChild( c );
223         }
224         return doc;
225     }
226     
227     
228     private static DocumentBuilder JavaDoc getDocumentBuilder()
229     {
230         try
231         {
232             return DocumentBuilderFactory.newInstance().newDocumentBuilder();
233         }
234         catch (Exception JavaDoc exc)
235         {
236             throw new ExceptionInInitializerError JavaDoc(exc);
237         }
238     }
239 }
240
241
Popular Tags