KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependencyfinder > ant > DependencyClosure


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.dependencyfinder.ant;
34
35 import java.io.*;
36 import java.util.*;
37
38 import org.apache.tools.ant.*;
39 import org.apache.tools.ant.types.*;
40
41 import org.xml.sax.*;
42
43 import com.jeantessier.dependency.*;
44
45 public class DependencyClosure extends GraphTask {
46     private String JavaDoc startIncludes = "//";
47     private String JavaDoc startExcludes = "";
48     private String JavaDoc packageStartIncludes = "";
49     private String JavaDoc packageStartExcludes = "";
50     private String JavaDoc classStartIncludes = "";
51     private String JavaDoc classStartExcludes = "";
52     private String JavaDoc featureStartIncludes = "";
53     private String JavaDoc featureStartExcludes = "";
54     private String JavaDoc stopIncludes = "";
55     private String JavaDoc stopExcludes = "";
56     private String JavaDoc packageStopIncludes = "";
57     private String JavaDoc packageStopExcludes = "";
58     private String JavaDoc classStopIncludes = "";
59     private String JavaDoc classStopExcludes = "";
60     private String JavaDoc featureStopIncludes = "";
61     private String JavaDoc featureStopExcludes = "";
62
63     private String JavaDoc maximumInboundDepth = "";
64     private String JavaDoc maximumOutboundDepth = "";
65     private boolean xml = false;
66     private String JavaDoc encoding = XMLPrinter.DEFAULT_ENCODING;
67     private String JavaDoc dtdPrefix = XMLPrinter.DEFAULT_DTD_PREFIX;
68     private String JavaDoc indentText;
69
70     public String JavaDoc getStartincludes() {
71         return startIncludes;
72     }
73
74     public void setStartincludes(String JavaDoc startIncludes) {
75         this.startIncludes = startIncludes;
76     }
77     
78     public String JavaDoc getStartexcludes() {
79         return startExcludes;
80     }
81
82     public void setStartexcludes(String JavaDoc startExcludes) {
83         this.startExcludes = startExcludes;
84     }
85     
86     public String JavaDoc getPackagestartincludes() {
87         return packageStartIncludes;
88     }
89
90     public void setPackagestartincludes(String JavaDoc packageStartIncludes) {
91         this.packageStartIncludes = packageStartIncludes;
92     }
93     
94     public String JavaDoc getPackagestartexcludes() {
95         return packageStartExcludes;
96     }
97
98     public void setPackagestartexcludes(String JavaDoc packageStartExcludes) {
99         this.packageStartExcludes = packageStartExcludes;
100     }
101     
102     public String JavaDoc getClassstartincludes() {
103         return classStartIncludes;
104     }
105
106     public void setClassstartincludes(String JavaDoc classStartIncludes) {
107         this.classStartIncludes = classStartIncludes;
108     }
109     
110     public String JavaDoc getClassstartexcludes() {
111         return classStartExcludes;
112     }
113
114     public void setClassstartexcludes(String JavaDoc classStartExcludes) {
115         this.classStartExcludes = classStartExcludes;
116     }
117     
118     public String JavaDoc getFeaturestartincludes() {
119         return featureStartIncludes;
120     }
121
122     public void setFeaturestartincludes(String JavaDoc featureStartIncludes) {
123         this.featureStartIncludes = featureStartIncludes;
124     }
125     
126     public String JavaDoc getFeaturestartexcludes() {
127         return featureStartExcludes;
128     }
129
130     public void setFeaturestartexcludes(String JavaDoc featureStartExcludes) {
131         this.featureStartExcludes = featureStartExcludes;
132     }
133
134     public String JavaDoc getStopincludes() {
135         return stopIncludes;
136     }
137
138     public void setStopincludes(String JavaDoc stopIncludes) {
139         this.stopIncludes = stopIncludes;
140     }
141     
142     public String JavaDoc getStopexcludes() {
143         return stopExcludes;
144     }
145
146     public void setStopexcludes(String JavaDoc stopExcludes) {
147         this.stopExcludes = stopExcludes;
148     }
149     
150     public String JavaDoc getPackagestopincludes() {
151         return packageStopIncludes;
152     }
153
154     public void setPackagestopincludes(String JavaDoc packageStopIncludes) {
155         this.packageStopIncludes = packageStopIncludes;
156     }
157     
158     public String JavaDoc getPackagestopexcludes() {
159         return packageStopExcludes;
160     }
161
162     public void setPackagestopexcludes(String JavaDoc packageStopExcludes) {
163         this.packageStopExcludes = packageStopExcludes;
164     }
165     
166     public String JavaDoc getClassstopincludes() {
167         return classStopIncludes;
168     }
169
170     public void setClassstopincludes(String JavaDoc classStopIncludes) {
171         this.classStopIncludes = classStopIncludes;
172     }
173     
174     public String JavaDoc getClassstopexcludes() {
175         return classStopExcludes;
176     }
177
178     public void setClassstopexcludes(String JavaDoc classStopExcludes) {
179         this.classStopExcludes = classStopExcludes;
180     }
181     
182     public String JavaDoc getFeaturestopincludes() {
183         return featureStopIncludes;
184     }
185
186     public void setFeaturestopincludes(String JavaDoc featureStopIncludes) {
187         this.featureStopIncludes = featureStopIncludes;
188     }
189     
190     public String JavaDoc getFeaturestopexcludes() {
191         return featureStopExcludes;
192     }
193
194     public void setFeaturestopexcludes(String JavaDoc featureStopExcludes) {
195         this.featureStopExcludes = featureStopExcludes;
196     }
197
198     public String JavaDoc getMaximuminbounddepth() {
199         return maximumInboundDepth;
200     }
201
202     public void setMaximuminbounddepth(String JavaDoc maximumInboundDepth) {
203         this.maximumInboundDepth = maximumInboundDepth;
204     }
205     
206     public String JavaDoc getMaximumoutbounddepth() {
207         return maximumOutboundDepth;
208     }
209
210     public void setMaximumoutbounddepth(String JavaDoc maximumOutboundDepth) {
211         this.maximumOutboundDepth = maximumOutboundDepth;
212     }
213
214     public boolean getXml() {
215         return xml;
216     }
217
218     public void setXml(boolean xml) {
219         this.xml = xml;
220     }
221
222     public String JavaDoc getEncoding() {
223         return encoding;
224     }
225     
226     public void setEncoding(String JavaDoc encoding) {
227         this.encoding = encoding;
228     }
229
230     public String JavaDoc getDtdprefix() {
231         return dtdPrefix;
232     }
233     
234     public void setDtdprefix(String JavaDoc dtdPrefix) {
235         this.dtdPrefix = dtdPrefix;
236     }
237
238     public String JavaDoc getIndenttext() {
239         return indentText;
240     }
241     
242     public void setIntenttext(String JavaDoc indentText) {
243         this.indentText = indentText;
244     }
245     
246     public void execute() throws BuildException {
247         // first off, make sure that we've got what we need
248
validateParameters();
249
250         VerboseListener verboseListener = new VerboseListener(this);
251
252         try {
253             NodeFactory factory = new NodeFactory();
254         
255             String JavaDoc[] filenames = getSrc().list();
256             for (int i=0; i<filenames.length; i++) {
257                 log("Reading graph from " + filenames[i]);
258                 
259                 if (filenames[i].endsWith(".xml")) {
260                     NodeLoader loader = new NodeLoader(factory, getValidate());
261                     loader.addDependencyListener(verboseListener);
262                     loader.load(filenames[i]);
263                 }
264             }
265
266             TransitiveClosure selector = new TransitiveClosure(getStartCriteria(), getStopCriteria());
267
268             try {
269                 if (getMaximuminbounddepth() != null) {
270                     selector.setMaximumInboundDepth(Long.parseLong(getMaximuminbounddepth()));
271                 }
272             } catch (NumberFormatException JavaDoc ex) {
273                 selector.setMaximumInboundDepth(TransitiveClosure.UNBOUNDED_DEPTH);
274             }
275             
276             try {
277                 if (getMaximumoutbounddepth() != null) {
278                     selector.setMaximumOutboundDepth(Long.parseLong(getMaximumoutbounddepth()));
279                 }
280             } catch (NumberFormatException JavaDoc ex) {
281                 selector.setMaximumOutboundDepth(TransitiveClosure.UNBOUNDED_DEPTH);
282             }
283                 
284             selector.traverseNodes(factory.getPackages().values());
285         
286             log("Saving dependency graph to " + getDestfile().getAbsolutePath());
287         
288             PrintWriter out = new PrintWriter(new FileWriter(getDestfile()));
289
290             Printer printer;
291             if (getXml()) {
292                 printer = new XMLPrinter(out, getEncoding(), getDtdprefix());
293             } else {
294                 printer = new TextPrinter(out);
295             }
296                 
297             if (getIndenttext() != null) {
298                 printer.setIndentText(getIndenttext());
299             }
300                 
301             printer.traverseNodes(selector.getFactory().getPackages().values());
302                 
303             out.close();
304         } catch (SAXException ex) {
305             throw new BuildException(ex);
306         } catch (IOException ex) {
307             throw new BuildException(ex);
308         }
309     }
310
311     private SelectionCriteria getStartCriteria() throws BuildException {
312         RegularExpressionSelectionCriteria result = new RegularExpressionSelectionCriteria();
313
314         result.setGlobalIncludes(getStartincludes());
315         result.setGlobalExcludes(getStartexcludes());
316         result.setPackageIncludes(getPackagestartincludes());
317         result.setPackageExcludes(getPackagestartexcludes());
318         result.setClassIncludes(getClassstartincludes());
319         result.setClassExcludes(getClassstartexcludes());
320         result.setFeatureIncludes(getFeaturestartincludes());
321         result.setFeatureExcludes(getFeaturestartexcludes());
322
323         return result;
324     }
325
326     private SelectionCriteria getStopCriteria() throws BuildException {
327         RegularExpressionSelectionCriteria result = new RegularExpressionSelectionCriteria();
328
329         result.setGlobalIncludes(getStopincludes());
330         result.setGlobalExcludes(getStopexcludes());
331         result.setPackageIncludes(getPackagestopincludes());
332         result.setPackageExcludes(getPackagestopexcludes());
333         result.setClassIncludes(getClassstopincludes());
334         result.setClassExcludes(getClassstopexcludes());
335         result.setFeatureIncludes(getFeaturestopincludes());
336         result.setFeatureExcludes(getFeaturestopexcludes());
337
338         return result;
339     }
340 }
341
Popular Tags