KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > ant > MackerReportAntTask


1 /*______________________________________________________________________________
2  *
3  * Macker http://innig.net/macker/
4  *
5  * Copyright 2003 Paul Cantrell
6  *
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License version 2, as published by the
9  * Free Software Foundation. See the file LICENSE.html for more information.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the license for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
17  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
18  *______________________________________________________________________________
19  */

20  
21 package net.innig.macker.ant;
22
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.util.*;
29
30 import org.mpr.util.StreamSplitter;
31
32 import org.apache.tools.ant.Task;
33 import org.apache.tools.ant.BuildException;
34
35 import javax.xml.transform.Transformer JavaDoc;
36 import javax.xml.transform.TransformerFactory JavaDoc;
37 import javax.xml.transform.TransformerException JavaDoc;
38 import javax.xml.transform.stream.StreamSource JavaDoc;
39 import javax.xml.transform.stream.StreamResult JavaDoc;
40
41 /**
42     A task which formats Macker reports using XSLT.
43     Requires Xalan 2 or some other well-behaved XSLT implementation.
44     
45     @see <a HREF="http://ant.apache.org/manual/">The Ant manual</a>
46 */

47 public class MackerReportAntTask extends Task
48     {
49     public MackerReportAntTask()
50         { tFactory = TransformerFactory.newInstance(); }
51     
52     public void setFormat(String JavaDoc formatName)
53         throws BuildException
54         { formatUrl = resolveInternalResource(formatName, "format", "xsl"); }
55     
56     public void setFormatFile(File JavaDoc formatFile)
57         throws BuildException
58         { formatUrl = resolveFile(formatFile, "format"); }
59     
60     public void setFormatUrl(String JavaDoc formatUrlS)
61         throws BuildException
62         { formatUrl = resolveUrl(formatUrlS, "format"); }
63     
64     public void setSkin(String JavaDoc skinName)
65         throws BuildException
66         { skinUrl = resolveInternalResource(skinName, "skin", "css"); }
67     
68     public void setSkinFile(File JavaDoc skinFile)
69         throws BuildException
70         { skinUrl = resolveFile(skinFile, "skin"); }
71     
72     public void setSkinUrl(String JavaDoc skinUrlS)
73         throws BuildException
74         { skinUrl = resolveUrl(skinUrlS, "skin"); }
75     
76     public void setXmlReportFile(File JavaDoc xmlReportFile)
77         throws BuildException
78         { reportUrl = resolveFile(xmlReportFile, "report"); }
79     
80     public void setXmlReportUrl(String JavaDoc xmlReportUrlS)
81         throws BuildException
82         { reportUrl = resolveUrl(xmlReportUrlS, "report"); }
83     
84     public void setOutputFile(File JavaDoc outputFile)
85         { this.outputFile = outputFile; }
86     
87     private URL JavaDoc resolveFile(File JavaDoc file, String JavaDoc kind)
88         throws BuildException
89         {
90         if(!file.exists())
91             throw new BuildException(kind + " file " + file + " does not exist");
92         if(!file.isFile())
93             throw new BuildException(kind + " file " + file + " is not a file");
94             
95         try { return file.toURL(); }
96         catch(MalformedURLException JavaDoc murle)
97             { throw new BuildException("Invalid " + kind + " file " + file, murle); }
98         }
99     
100     private URL JavaDoc resolveUrl(String JavaDoc urlS, String JavaDoc kind)
101         throws BuildException
102         {
103         try { return new URL JavaDoc(urlS); }
104         catch(MalformedURLException JavaDoc murle)
105             { throw new BuildException("Invalid " + kind + " URL " + urlS, murle); }
106         }
107     
108     private URL JavaDoc resolveInternalResource(String JavaDoc name, String JavaDoc kind, String JavaDoc extension)
109         throws BuildException
110         {
111         String JavaDoc resourceName = "net/innig/macker/report/" + kind + '/' + name + '.' + extension;
112         URL JavaDoc resource = getClass().getClassLoader().getResource(resourceName);
113         if(resource == null)
114             throw new BuildException(
115                 "No internal Macker report " + kind + " named \"" + name
116                 + "\" (can't find \"" + resourceName + "\")");
117         return resource;
118         }
119     
120     public void execute()
121         throws BuildException
122         {
123         if(reportUrl == null)
124             throw new BuildException("xmlReportFile or xmlReportUrl required");
125         if(outputFile == null)
126             throw new BuildException("outputFile required");
127             
128         if(formatUrl == null)
129             setFormat("html-basic");
130         if(skinUrl == null)
131             setSkin("vanilla");
132         
133         File JavaDoc outputDir = outputFile.getParentFile();
134         
135         try {
136             Transformer JavaDoc transformer = tFactory.newTransformer(new StreamSource JavaDoc(formatUrl.openStream()));
137             transformer.transform(
138                 new StreamSource JavaDoc(reportUrl.openStream()),
139                 new StreamResult JavaDoc(new FileOutputStream JavaDoc(outputFile)));
140             }
141         catch(IOException JavaDoc ioe)
142             { throw new BuildException("Unable to process report: " + ioe, ioe); }
143         catch(TransformerException JavaDoc te)
144             { throw new BuildException("Unable to apply report formatting: " + te.getMessage(), te); }
145         
146         File JavaDoc skinOutputFile = new File JavaDoc(outputDir, "macker-report.css");
147         try {
148             new StreamSplitter(
149                     skinUrl.openStream(),
150                     new FileOutputStream JavaDoc(skinOutputFile))
151                 .run();
152             }
153         catch(IOException JavaDoc ioe)
154             { throw new BuildException("Unable to copy skin to " + skinOutputFile, ioe); }
155         }
156     
157     private URL JavaDoc formatUrl, skinUrl;
158     private URL JavaDoc reportUrl;
159     private File JavaDoc outputFile;
160     private TransformerFactory JavaDoc tFactory;
161     }
162
163
Popular Tags