KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > ant > IvyReport


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * #SNAPSHOT#
5  */

6 package fr.jayasoft.ivy.ant;
7
8 import java.io.File JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13
14 import org.apache.tools.ant.BuildException;
15 import org.apache.tools.ant.taskdefs.XSLTProcess;
16 import org.apache.tools.ant.types.Mapper;
17 import org.apache.tools.ant.util.FileNameMapper;
18 import org.apache.tools.ant.util.GlobPatternMapper;
19
20 import fr.jayasoft.ivy.Ivy;
21 import fr.jayasoft.ivy.ModuleId;
22 import fr.jayasoft.ivy.report.XmlReportOutputter;
23 import fr.jayasoft.ivy.util.FileUtil;
24 import fr.jayasoft.ivy.util.IvyPatternHelper;
25 import fr.jayasoft.ivy.util.Message;
26
27 /**
28  * This ant task let users generates reports (html, xml, graphml, ...) from the last resolve done.
29  *
30  * @author Xavier Hanin
31  */

32 public class IvyReport extends IvyTask {
33     private File JavaDoc _todir;
34     private String JavaDoc _organisation;
35     private String JavaDoc _module;
36     private String JavaDoc _conf;
37     private File JavaDoc _cache;
38     private boolean _graph = true;
39     private boolean _dot = false;
40     private boolean _xml = false;
41     private boolean _xsl = true;
42     private String JavaDoc _xslFile;
43     private String JavaDoc _outputpattern;
44     private String JavaDoc _xslext = "html";
45     private List JavaDoc _params = new ArrayList JavaDoc();
46
47     public File JavaDoc getTodir() {
48         return _todir;
49     }
50     public void setTodir(File JavaDoc todir) {
51         _todir = todir;
52     }
53     public File JavaDoc getCache() {
54         return _cache;
55     }
56     public void setCache(File JavaDoc cache) {
57         _cache = cache;
58     }
59     public String JavaDoc getConf() {
60         return _conf;
61     }
62     public void setConf(String JavaDoc conf) {
63         _conf = conf;
64     }
65     public String JavaDoc getModule() {
66         return _module;
67     }
68     public void setModule(String JavaDoc module) {
69         _module = module;
70     }
71     public String JavaDoc getOrganisation() {
72         return _organisation;
73     }
74     public void setOrganisation(String JavaDoc organisation) {
75         _organisation = organisation;
76     }
77     
78     public boolean isGraph() {
79         return _graph;
80     }
81     
82     public void setGraph(boolean graph) {
83         _graph = graph;
84     }
85     public String JavaDoc getXslfile() {
86         return _xslFile;
87     }
88     
89     public void setXslfile(String JavaDoc xslFile) {
90         _xslFile = xslFile;
91     }
92     public String JavaDoc getOutputpattern() {
93         return _outputpattern;
94     }
95     
96     public void setOutputpattern(String JavaDoc outputpattern) {
97         _outputpattern = outputpattern;
98     }
99
100
101     public void execute() throws BuildException {
102         Ivy ivy = getIvyInstance();
103         
104         _organisation = getProperty(_organisation, ivy, "ivy.organisation");
105         _module = getProperty(_module, ivy, "ivy.module");
106         if (_cache == null) {
107             _cache = ivy.getDefaultCache();
108         }
109         _conf = getProperty(_conf, ivy, "ivy.resolved.configurations");
110         if (_conf.equals("*")) {
111             _conf = getProperty(ivy, "ivy.resolved.configurations");
112         }
113         if (_conf == null) {
114             throw new BuildException("no conf provided for ivy report task: It can either be set explicitely via the attribute 'conf' or via 'ivy.resolved.configurations' property or a prior call to <resolve/>");
115         }
116         if (_todir == null) {
117             String JavaDoc t = getProperty(ivy, "ivy.report.todir");
118             if (t != null) {
119                 _todir = new File JavaDoc(t);
120             }
121         }
122         _outputpattern = getProperty(_outputpattern, ivy, "ivy.report.output.pattern");
123         if (_todir != null && _todir.exists()) {
124             _todir.mkdirs();
125         }
126         if (_outputpattern == null) {
127             _outputpattern = "[organisation]-[module]-[conf].[ext]";
128         }
129         
130         if (_todir != null && _todir.exists() && !_todir.isDirectory()) {
131             throw new BuildException("destination directory should be a directory !");
132         }
133         if (_organisation == null) {
134             throw new BuildException("no organisation provided for ivy report task: It can either be set explicitely via the attribute 'organisation' or via 'ivy.organisation' property or a prior call to <resolve/>");
135         }
136         if (_module == null) {
137             throw new BuildException("no module name provided for ivy report task: It can either be set explicitely via the attribute 'module' or via 'ivy.module' property or a prior call to <resolve/>");
138         }
139         try {
140             String JavaDoc[] confs = splitConfs(_conf);
141             if (_xsl) {
142                 genreport(_cache, _organisation, _module, confs);
143             }
144             if (_xml) {
145                 genxml(_cache, _organisation, _module, confs);
146             }
147             if (_graph) {
148                 genStyled(_cache, _organisation, _module, confs, getStylePath(_cache, "ivy-report-graph.xsl"), "graphml");
149             }
150             if (_dot) {
151                 genStyled(_cache, _organisation, _module, confs, getStylePath(_cache, "ivy-report-dot.xsl"), "dot");
152             }
153         } catch (IOException JavaDoc e) {
154             throw new BuildException("impossible to generate report: "+e, e);
155         }
156     }
157     
158     private void genxml(File JavaDoc cache, String JavaDoc organisation, String JavaDoc module, String JavaDoc[] confs) throws IOException JavaDoc {
159         for (int i = 0; i < confs.length; i++) {
160             File JavaDoc xml = new File JavaDoc(cache, XmlReportOutputter.getReportFileName(new ModuleId(organisation, module), confs[i]));
161
162             File JavaDoc out;
163             if (_todir != null) {
164                 out = new File JavaDoc(_todir, IvyPatternHelper.substitute(_outputpattern, organisation, module, "", "", "", "xml", confs[i]));
165             } else {
166                 out = new File JavaDoc(IvyPatternHelper.substitute(_outputpattern, organisation, module, "", "", "", "xml", confs[i]));
167             }
168         
169             FileUtil.copy(xml, out, null);
170         }
171     }
172     private void genreport(File JavaDoc cache, String JavaDoc organisation, String JavaDoc module, String JavaDoc[] confs) throws IOException JavaDoc {
173         genStyled(cache, organisation, module, confs, getReportStylePath(cache), _xslext);
174
175         // copy the css if required
176
if (_todir != null && _xslFile == null) {
177             File JavaDoc css = new File JavaDoc(_todir, "ivy-report.css");
178             if (!css.exists()) {
179                 Message.debug("copying report css to "+_todir);
180                 FileUtil.copy(XmlReportOutputter.class.getResourceAsStream("ivy-report.css"), css, null);
181             }
182             FileUtil.copy(XmlReportOutputter.class.getResourceAsStream("ivy-report.css"), new File JavaDoc(cache, "ivy-report.css"), null);
183         }
184     }
185     
186     private String JavaDoc getReportStylePath(File JavaDoc cache) throws IOException JavaDoc {
187         if (_xslFile != null) {
188             return _xslFile;
189         }
190         // style should be a file (and not an url)
191
// so we have to copy it from classpath to cache
192
File JavaDoc style = new File JavaDoc(cache, "ivy-report.xsl");
193         FileUtil.copy(XmlReportOutputter.class.getResourceAsStream("ivy-report.xsl"), style, null);
194         return style.getAbsolutePath();
195     }
196     
197     private void genStyled(File JavaDoc cache, String JavaDoc organisation, String JavaDoc module, String JavaDoc[] confs, String JavaDoc style, String JavaDoc ext) throws IOException JavaDoc {
198         // process the report with xslt to generate dot file
199
File JavaDoc out;
200         if (_todir != null) {
201             out = _todir;
202         } else {
203             out = new File JavaDoc(".");
204         }
205
206         XSLTProcess xslt = new XSLTProcess();
207         xslt.setTaskName(getTaskName());
208         xslt.setProject(getProject());
209         xslt.init();
210         
211         xslt.setDestdir(out);
212         xslt.setBasedir(cache);
213
214         Mapper mapper = new Mapper(getProject());
215         xslt.addMapper(mapper);
216         
217         for (int i = 0; i < confs.length; i++) {
218             String JavaDoc reportFileName = XmlReportOutputter.getReportFileName(new ModuleId(organisation, module), confs[i]);
219             xslt.setIncludes(reportFileName);
220             
221             FileNameMapper reportMapper = new GlobPatternMapper();
222             reportMapper.setFrom(reportFileName);
223             reportMapper.setTo(IvyPatternHelper.substitute(_outputpattern, organisation, module, "", "", "", ext, confs[i]));
224             mapper.add(reportMapper);
225         }
226         xslt.setStyle(style);
227         
228         XSLTProcess.Param param = xslt.createParam();
229         param.setName("confs");
230         param.setExpression(_conf);
231         param = xslt.createParam();
232         param.setName("extension");
233         param.setExpression(_xslext);
234
235         // add the provided XSLT parameters
236
for (Iterator JavaDoc it = _params.iterator(); it.hasNext(); ) {
237             param = (XSLTProcess.Param) it.next();
238             XSLTProcess.Param realParam = xslt.createParam();
239             realParam.setName(param.getName());
240             realParam.setExpression(param.getExpression());
241         }
242         
243         xslt.execute();
244     }
245     
246     private String JavaDoc getStylePath(File JavaDoc cache, String JavaDoc styleResourceName) throws IOException JavaDoc {
247         // style should be a file (and not an url)
248
// so we have to copy it from classpath to cache
249
File JavaDoc style = new File JavaDoc(cache, styleResourceName);
250         FileUtil.copy(XmlReportOutputter.class.getResourceAsStream(styleResourceName), style, null);
251         return style.getAbsolutePath();
252     }
253     
254     public boolean isXml() {
255         return _xml;
256     }
257     public void setXml(boolean xml) {
258         _xml = xml;
259     }
260     public boolean isXsl() {
261         return _xsl;
262     }
263     public void setXsl(boolean xsl) {
264         _xsl = xsl;
265     }
266     public String JavaDoc getXslext() {
267         return _xslext;
268     }
269     public void setXslext(String JavaDoc xslext) {
270         _xslext = xslext;
271     }
272     
273     public XSLTProcess.Param createParam() {
274         XSLTProcess.Param result = new XSLTProcess.Param();
275         _params.add(result);
276         return result;
277     }
278     public boolean isDot() {
279         return _dot;
280     }
281     public void setDot(boolean dot) {
282         _dot = dot;
283     }
284     
285 }
286
Popular Tags