001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependencyfinder.ant;
034:
035: import java.io.*;
036:
037: import javax.xml.parsers.*;
038:
039: import org.apache.tools.ant.*;
040:
041: import org.xml.sax.*;
042:
043: import com.jeantessier.dependency.*;
044:
045: public class DependencyClosure extends GraphTask {
046: private String startIncludes = "//";
047: private String startExcludes = "";
048: private String packageStartIncludes = "";
049: private String packageStartExcludes = "";
050: private String classStartIncludes = "";
051: private String classStartExcludes = "";
052: private String featureStartIncludes = "";
053: private String featureStartExcludes = "";
054: private String stopIncludes = "";
055: private String stopExcludes = "";
056: private String packageStopIncludes = "";
057: private String packageStopExcludes = "";
058: private String classStopIncludes = "";
059: private String classStopExcludes = "";
060: private String featureStopIncludes = "";
061: private String featureStopExcludes = "";
062:
063: private String maximumInboundDepth = "";
064: private String maximumOutboundDepth = "";
065: private boolean xml = false;
066: private String encoding = XMLPrinter.DEFAULT_ENCODING;
067: private String dtdPrefix = XMLPrinter.DEFAULT_DTD_PREFIX;
068: private String indentText;
069:
070: public String getStartincludes() {
071: return startIncludes;
072: }
073:
074: public void setStartincludes(String startIncludes) {
075: this .startIncludes = startIncludes;
076: }
077:
078: public String getStartexcludes() {
079: return startExcludes;
080: }
081:
082: public void setStartexcludes(String startExcludes) {
083: this .startExcludes = startExcludes;
084: }
085:
086: public String getPackagestartincludes() {
087: return packageStartIncludes;
088: }
089:
090: public void setPackagestartincludes(String packageStartIncludes) {
091: this .packageStartIncludes = packageStartIncludes;
092: }
093:
094: public String getPackagestartexcludes() {
095: return packageStartExcludes;
096: }
097:
098: public void setPackagestartexcludes(String packageStartExcludes) {
099: this .packageStartExcludes = packageStartExcludes;
100: }
101:
102: public String getClassstartincludes() {
103: return classStartIncludes;
104: }
105:
106: public void setClassstartincludes(String classStartIncludes) {
107: this .classStartIncludes = classStartIncludes;
108: }
109:
110: public String getClassstartexcludes() {
111: return classStartExcludes;
112: }
113:
114: public void setClassstartexcludes(String classStartExcludes) {
115: this .classStartExcludes = classStartExcludes;
116: }
117:
118: public String getFeaturestartincludes() {
119: return featureStartIncludes;
120: }
121:
122: public void setFeaturestartincludes(String featureStartIncludes) {
123: this .featureStartIncludes = featureStartIncludes;
124: }
125:
126: public String getFeaturestartexcludes() {
127: return featureStartExcludes;
128: }
129:
130: public void setFeaturestartexcludes(String featureStartExcludes) {
131: this .featureStartExcludes = featureStartExcludes;
132: }
133:
134: public String getStopincludes() {
135: return stopIncludes;
136: }
137:
138: public void setStopincludes(String stopIncludes) {
139: this .stopIncludes = stopIncludes;
140: }
141:
142: public String getStopexcludes() {
143: return stopExcludes;
144: }
145:
146: public void setStopexcludes(String stopExcludes) {
147: this .stopExcludes = stopExcludes;
148: }
149:
150: public String getPackagestopincludes() {
151: return packageStopIncludes;
152: }
153:
154: public void setPackagestopincludes(String packageStopIncludes) {
155: this .packageStopIncludes = packageStopIncludes;
156: }
157:
158: public String getPackagestopexcludes() {
159: return packageStopExcludes;
160: }
161:
162: public void setPackagestopexcludes(String packageStopExcludes) {
163: this .packageStopExcludes = packageStopExcludes;
164: }
165:
166: public String getClassstopincludes() {
167: return classStopIncludes;
168: }
169:
170: public void setClassstopincludes(String classStopIncludes) {
171: this .classStopIncludes = classStopIncludes;
172: }
173:
174: public String getClassstopexcludes() {
175: return classStopExcludes;
176: }
177:
178: public void setClassstopexcludes(String classStopExcludes) {
179: this .classStopExcludes = classStopExcludes;
180: }
181:
182: public String getFeaturestopincludes() {
183: return featureStopIncludes;
184: }
185:
186: public void setFeaturestopincludes(String featureStopIncludes) {
187: this .featureStopIncludes = featureStopIncludes;
188: }
189:
190: public String getFeaturestopexcludes() {
191: return featureStopExcludes;
192: }
193:
194: public void setFeaturestopexcludes(String featureStopExcludes) {
195: this .featureStopExcludes = featureStopExcludes;
196: }
197:
198: public String getMaximuminbounddepth() {
199: return maximumInboundDepth;
200: }
201:
202: public void setMaximuminbounddepth(String maximumInboundDepth) {
203: this .maximumInboundDepth = maximumInboundDepth;
204: }
205:
206: public String getMaximumoutbounddepth() {
207: return maximumOutboundDepth;
208: }
209:
210: public void setMaximumoutbounddepth(String 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 getEncoding() {
223: return encoding;
224: }
225:
226: public void setEncoding(String encoding) {
227: this .encoding = encoding;
228: }
229:
230: public String getDtdprefix() {
231: return dtdPrefix;
232: }
233:
234: public void setDtdprefix(String dtdPrefix) {
235: this .dtdPrefix = dtdPrefix;
236: }
237:
238: public String getIndenttext() {
239: return indentText;
240: }
241:
242: public void setIntenttext(String 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: for (String filename : getSrc().list()) {
256: log("Reading graph from " + filename);
257:
258: if (filename.endsWith(".xml")) {
259: NodeLoader loader = new NodeLoader(factory,
260: getValidate());
261: loader.addDependencyListener(verboseListener);
262: loader.load(filename);
263: }
264: }
265:
266: TransitiveClosure selector = new TransitiveClosure(
267: getStartCriteria(), getStopCriteria());
268:
269: try {
270: if (getMaximuminbounddepth() != null) {
271: selector.setMaximumInboundDepth(Long
272: .parseLong(getMaximuminbounddepth()));
273: }
274: } catch (NumberFormatException ex) {
275: selector
276: .setMaximumInboundDepth(TransitiveClosure.UNBOUNDED_DEPTH);
277: }
278:
279: try {
280: if (getMaximumoutbounddepth() != null) {
281: selector.setMaximumOutboundDepth(Long
282: .parseLong(getMaximumoutbounddepth()));
283: }
284: } catch (NumberFormatException ex) {
285: selector
286: .setMaximumOutboundDepth(TransitiveClosure.UNBOUNDED_DEPTH);
287: }
288:
289: selector.traverseNodes(factory.getPackages().values());
290:
291: log("Saving dependency graph to "
292: + getDestfile().getAbsolutePath());
293:
294: PrintWriter out = new PrintWriter(new FileWriter(
295: getDestfile()));
296:
297: Printer printer;
298: if (getXml()) {
299: printer = new XMLPrinter(out, getEncoding(),
300: getDtdprefix());
301: } else {
302: printer = new TextPrinter(out);
303: }
304:
305: if (getIndenttext() != null) {
306: printer.setIndentText(getIndenttext());
307: }
308:
309: printer.traverseNodes(selector.getFactory().getPackages()
310: .values());
311:
312: out.close();
313: } catch (SAXException ex) {
314: throw new BuildException(ex);
315: } catch (ParserConfigurationException ex) {
316: throw new BuildException(ex);
317: } catch (IOException ex) {
318: throw new BuildException(ex);
319: }
320: }
321:
322: private SelectionCriteria getStartCriteria() throws BuildException {
323: RegularExpressionSelectionCriteria result = new RegularExpressionSelectionCriteria();
324:
325: result.setGlobalIncludes(getStartincludes());
326: result.setGlobalExcludes(getStartexcludes());
327: result.setPackageIncludes(getPackagestartincludes());
328: result.setPackageExcludes(getPackagestartexcludes());
329: result.setClassIncludes(getClassstartincludes());
330: result.setClassExcludes(getClassstartexcludes());
331: result.setFeatureIncludes(getFeaturestartincludes());
332: result.setFeatureExcludes(getFeaturestartexcludes());
333:
334: return result;
335: }
336:
337: private SelectionCriteria getStopCriteria() throws BuildException {
338: RegularExpressionSelectionCriteria result = new RegularExpressionSelectionCriteria();
339:
340: result.setGlobalIncludes(getStopincludes());
341: result.setGlobalExcludes(getStopexcludes());
342: result.setPackageIncludes(getPackagestopincludes());
343: result.setPackageExcludes(getPackagestopexcludes());
344: result.setClassIncludes(getClassstopincludes());
345: result.setClassExcludes(getClassstopexcludes());
346: result.setFeatureIncludes(getFeaturestopincludes());
347: result.setFeatureExcludes(getFeaturestopexcludes());
348:
349: return result;
350: }
351: }
|