KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.jeantessier.classreader.*;
42 import com.jeantessier.dependency.*;
43
44 public class DependencyExtractor extends Task {
45     private boolean xml = false;
46     private boolean minimize = false;
47     private boolean maximize = false;
48     private String JavaDoc encoding = com.jeantessier.dependency.XMLPrinter.DEFAULT_ENCODING;
49     private String JavaDoc dtdPrefix = com.jeantessier.dependency.XMLPrinter.DEFAULT_DTD_PREFIX;
50     private String JavaDoc indentText;
51     private File destfile;
52     private Path path;
53
54     public boolean getXml() {
55         return xml;
56     }
57
58     public void setXml(boolean xml) {
59         this.xml = xml;
60     }
61
62     public boolean getMinimize() {
63         return minimize;
64     }
65
66     public void setMinimize(boolean minimize) {
67         this.minimize = minimize;
68     }
69
70     public boolean getMaximize() {
71         return maximize;
72     }
73
74     public void setMaximize(boolean maximize) {
75         this.maximize = maximize;
76     }
77
78     public String JavaDoc getEncoding() {
79         return encoding;
80     }
81     
82     public void setEncoding(String JavaDoc encoding) {
83         this.encoding = encoding;
84     }
85
86     public String JavaDoc getDtdprefix() {
87         return dtdPrefix;
88     }
89     
90     public void setDtdprefix(String JavaDoc dtdPrefix) {
91         this.dtdPrefix = dtdPrefix;
92     }
93
94     public String JavaDoc getIndenttext() {
95         return indentText;
96     }
97     
98     public void setIntenttext(String JavaDoc indentText) {
99         this.indentText = indentText;
100     }
101
102     public File getDestfile() {
103         return destfile;
104     }
105     
106     public void setDestfile(File destfile) {
107         this.destfile = destfile;
108     }
109     
110     public Path createPath() {
111         if (path == null) {
112             path = new Path(getProject());
113         }
114
115         return path;
116     }
117     
118     public Path getPath() {
119         return path;
120     }
121     
122     public void execute() throws BuildException {
123         // first off, make sure that we've got what we need
124

125         if (getPath() == null) {
126             throw new BuildException("path must be set!");
127         }
128
129         if (getDestfile() == null) {
130             throw new BuildException("destfile must be set!");
131         }
132
133         log("Reading classes from path " + getPath());
134
135         VerboseListener verboseListener = new VerboseListener(this);
136
137         NodeFactory factory = new NodeFactory();
138         CodeDependencyCollector collector = new CodeDependencyCollector(factory);
139         
140         ClassfileLoader loader = new TransientClassfileLoader();
141         loader.addLoadListener(new LoadListenerVisitorAdapter(collector));
142         loader.addLoadListener(verboseListener);
143         loader.load(Arrays.asList(getPath().list()));
144
145         if (getMinimize()) {
146             LinkMinimizer minimizer = new LinkMinimizer();
147             minimizer.traverseNodes(factory.getPackages().values());
148         } else if (getMaximize()) {
149             LinkMaximizer maximizer = new LinkMaximizer();
150             maximizer.traverseNodes(factory.getPackages().values());
151         }
152
153         log("Saving dependency graph to " + getDestfile().getAbsolutePath());
154         
155         try {
156             PrintWriter out = new PrintWriter(new FileWriter(getDestfile()));
157
158             com.jeantessier.dependency.Printer printer;
159             if (getXml()) {
160                 printer = new com.jeantessier.dependency.XMLPrinter(out, getEncoding(), getDtdprefix());
161             } else {
162                 printer = new com.jeantessier.dependency.TextPrinter(out);
163             }
164                 
165             if (getIndenttext() != null) {
166                 printer.setIndentText(getIndenttext());
167             }
168                 
169             printer.traverseNodes(factory.getPackages().values());
170                 
171             out.close();
172         } catch (IOException ex) {
173             throw new BuildException(ex);
174         }
175     }
176 }
177
Popular Tags