KickJava   Java API By Example, From Geeks To Geeks.

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


1 package fr.jayasoft.ivy.ant;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.apache.tools.ant.BuildException;
10 import org.apache.tools.ant.taskdefs.XSLTProcess;
11
12 import fr.jayasoft.ivy.DefaultModuleDescriptor;
13 import fr.jayasoft.ivy.Ivy;
14 import fr.jayasoft.ivy.ModuleId;
15 import fr.jayasoft.ivy.ModuleRevisionId;
16 import fr.jayasoft.ivy.filter.FilterHelper;
17 import fr.jayasoft.ivy.matcher.PatternMatcher;
18 import fr.jayasoft.ivy.report.ResolveReport;
19 import fr.jayasoft.ivy.report.XmlReportOutputter;
20 import fr.jayasoft.ivy.util.FileUtil;
21
22 /**
23  * Generates a report of dependencies of a set of modules in the repository.
24  *
25  * The set of modules is specified using organisation/module and matcher.
26  *
27  * @author Xavier Hanin
28  *
29  */

30 public class IvyRepositoryReport extends IvyTask {
31     private String JavaDoc _organisation = "*";
32     private String JavaDoc _module;
33     private String JavaDoc _branch;
34     private String JavaDoc _revision = "latest.integration";
35     private File JavaDoc _cache;
36     private String JavaDoc _matcher = PatternMatcher.EXACT_OR_REGEXP;
37     
38     private File JavaDoc _todir = new File JavaDoc(".");
39     private boolean _graph = false;
40     private boolean _dot = false;
41     private boolean _xml = true;
42     private boolean _xsl = false;
43     private String JavaDoc _xslFile;
44     private String JavaDoc _outputname = "ivy-repository-report";
45     private String JavaDoc _xslext = "html";
46     private List JavaDoc _params = new ArrayList JavaDoc();
47     
48     public void execute() throws BuildException {
49         Ivy ivy = getIvyInstance();
50         if (_cache == null) {
51             _cache = ivy.getDefaultCache();
52         }
53         if (_xsl && _xslFile == null) {
54             throw new BuildException("xsl file is mandatory when using xsl generation");
55         }
56         if (_module == null && PatternMatcher.EXACT.equals(_matcher)) {
57             throw new BuildException("no module name provided for ivy repository graph task: It can either be set explicitely via the attribute 'module' or via 'ivy.module' property or a prior call to <resolve/>");
58         } else if (_module == null && !PatternMatcher.EXACT.equals(_matcher)) {
59             _module = PatternMatcher.ANY_EXPRESSION;
60         }
61         ModuleRevisionId mrid = ModuleRevisionId.newInstance(_organisation, _module, _revision);
62         try {
63             ModuleId[] mids = ivy.listModules(new ModuleId(_organisation, _module), ivy.getMatcher(_matcher));
64             ModuleRevisionId[] mrids = new ModuleRevisionId[mids.length];
65             for (int i = 0; i < mrids.length; i++) {
66                 if (_branch != null) {
67                     mrids[i] = new ModuleRevisionId(mids[i], _branch, _revision);
68                 } else {
69                     mrids[i] = new ModuleRevisionId(mids[i], _revision);
70                 }
71             }
72             DefaultModuleDescriptor md = DefaultModuleDescriptor.newCallerInstance(mrids, true, false);
73             ResolveReport report = ivy.resolve(md, new String JavaDoc[] {"*"}, _cache, null, doValidate(ivy), false, true, false, false, FilterHelper.NO_FILTER);
74             new XmlReportOutputter().output(report, _cache);
75             if (_graph) {
76                 gengraph(_cache, md.getModuleRevisionId().getOrganisation(), md.getModuleRevisionId().getName());
77             }
78             if (_dot) {
79                 gendot(_cache, md.getModuleRevisionId().getOrganisation(), md.getModuleRevisionId().getName());
80             }
81             if (_xml) {
82                 FileUtil.copy(new File JavaDoc(_cache, XmlReportOutputter.getReportFileName(md.getModuleRevisionId().getModuleId(), "default")), new File JavaDoc(_todir, _outputname+".xml"), null);
83             }
84             if (_xsl) {
85                 genreport(_cache, md.getModuleRevisionId().getOrganisation(), md.getModuleRevisionId().getName());
86             }
87         } catch (Exception JavaDoc e) {
88             throw new BuildException("impossible to generate graph for "+ mrid +": "+e, e);
89         }
90     }
91     
92     private void genreport(File JavaDoc cache, String JavaDoc organisation, String JavaDoc module) throws IOException JavaDoc {
93         // first process the report with xslt
94
XSLTProcess xslt = new XSLTProcess();
95         xslt.setTaskName(getTaskName());
96         xslt.setProject(getProject());
97         xslt.init();
98         
99         xslt.setIn(new File JavaDoc(cache, XmlReportOutputter.getReportFileName(new ModuleId(organisation, module), "default")));
100         xslt.setOut(new File JavaDoc(_todir, _outputname+"."+_xslext));
101         
102         xslt.setStyle(_xslFile);
103         
104         XSLTProcess.Param param = xslt.createParam();
105         param.setName("extension");
106         param.setExpression(_xslext);
107
108         // add the provided XSLT parameters
109
for (Iterator JavaDoc it = _params.iterator(); it.hasNext(); ) {
110             param = (XSLTProcess.Param) it.next();
111             XSLTProcess.Param realParam = xslt.createParam();
112             realParam.setName(param.getName());
113             realParam.setExpression(param.getExpression());
114         }
115         
116         xslt.execute();
117     }
118
119     private void gengraph(File JavaDoc cache, String JavaDoc organisation, String JavaDoc module) throws IOException JavaDoc {
120         gen(cache, organisation, module, getGraphStylePath(cache), "graphml");
121     }
122     
123     private String JavaDoc getGraphStylePath(File JavaDoc cache) throws IOException JavaDoc {
124         // style should be a file (and not an url)
125
// so we have to copy it from classpath to cache
126
File JavaDoc style = new File JavaDoc(cache, "ivy-report-graph-all.xsl");
127         FileUtil.copy(XmlReportOutputter.class.getResourceAsStream("ivy-report-graph-all.xsl"), style, null);
128         return style.getAbsolutePath();
129     }
130     
131     private void gendot(File JavaDoc cache, String JavaDoc organisation, String JavaDoc module) throws IOException JavaDoc {
132         gen(cache, organisation, module, getDotStylePath(cache), "dot");
133     }
134     
135     private String JavaDoc getDotStylePath(File JavaDoc cache) throws IOException JavaDoc {
136         // style should be a file (and not an url)
137
// so we have to copy it from classpath to cache
138
File JavaDoc style = new File JavaDoc(cache, "ivy-report-dot-all.xsl");
139         FileUtil.copy(XmlReportOutputter.class.getResourceAsStream("ivy-report-dot-all.xsl"), style, null);
140         return style.getAbsolutePath();
141     }
142     
143     private void gen(File JavaDoc cache, String JavaDoc organisation, String JavaDoc module, String JavaDoc style, String JavaDoc ext) throws IOException JavaDoc {
144         XSLTProcess xslt = new XSLTProcess();
145         xslt.setTaskName(getTaskName());
146         xslt.setProject(getProject());
147         xslt.init();
148         
149         xslt.setIn(new File JavaDoc(cache, XmlReportOutputter.getReportFileName(new ModuleId(organisation, module), "default")));
150         xslt.setOut(new File JavaDoc(_todir, _outputname+"."+ext));
151         xslt.setBasedir(cache);
152         xslt.setStyle(style);
153         xslt.execute();
154     }
155     
156     public File JavaDoc getTodir() {
157         return _todir;
158     }
159     public void setTodir(File JavaDoc todir) {
160         _todir = todir;
161     }
162     public boolean isGraph() {
163         return _graph;
164     }
165     
166     public void setGraph(boolean graph) {
167         _graph = graph;
168     }
169     public String JavaDoc getXslfile() {
170         return _xslFile;
171     }
172     
173     public void setXslfile(String JavaDoc xslFile) {
174         _xslFile = xslFile;
175     }
176     public boolean isXml() {
177         return _xml;
178     }
179     public void setXml(boolean xml) {
180         _xml = xml;
181     }
182     public boolean isXsl() {
183         return _xsl;
184     }
185     public void setXsl(boolean xsl) {
186         _xsl = xsl;
187     }
188     public String JavaDoc getXslext() {
189         return _xslext;
190     }
191     public void setXslext(String JavaDoc xslext) {
192         _xslext = xslext;
193     }
194     
195     public XSLTProcess.Param createParam() {
196         XSLTProcess.Param result = new XSLTProcess.Param();
197         _params.add(result);
198         return result;
199     }
200     
201     public String JavaDoc getOutputname() {
202         return _outputname;
203     }
204     
205     public void setOutputname(String JavaDoc outputpattern) {
206         _outputname = outputpattern;
207     }
208
209     public File JavaDoc getCache() {
210         return _cache;
211     }
212     public void setCache(File JavaDoc cache) {
213         _cache = cache;
214     }
215     public String JavaDoc getMatcher() {
216         return _matcher;
217     }
218     public void setMatcher(String JavaDoc matcher) {
219         _matcher = matcher;
220     }
221     public String JavaDoc getModule() {
222         return _module;
223     }
224     public void setModule(String JavaDoc module) {
225         _module = module;
226     }
227     public String JavaDoc getOrganisation() {
228         return _organisation;
229     }
230     public void setOrganisation(String JavaDoc organisation) {
231         _organisation = organisation;
232     }
233     public String JavaDoc getRevision() {
234         return _revision;
235     }
236     public void setRevision(String JavaDoc revision) {
237         _revision = revision;
238     }
239
240
241     public String JavaDoc getBranch() {
242         return _branch;
243     }
244
245
246     public void setBranch(String JavaDoc branch) {
247         _branch = branch;
248     }
249
250     public boolean isDot() {
251         return _dot;
252     }
253
254     public void setDot(boolean dot) {
255         _dot = dot;
256     }
257     
258
259 }
260
Popular Tags