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.cli;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: import org.apache.log4j.*;
039:
040: import com.jeantessier.dependency.*;
041: import com.jeantessier.dependency.Printer;
042: import com.jeantessier.dependency.TextPrinter;
043: import com.jeantessier.commandline.*;
044:
045: public class DependencyReporter extends Command {
046: public DependencyReporter() throws CommandLineException {
047: super ("DependencyReporter");
048: }
049:
050: protected void showSpecificUsage(PrintStream out) {
051: out.println();
052: out.println("Default is text output to the console.");
053: out.println();
054: }
055:
056: protected void populateCommandLineSwitches() {
057: super .populateCommandLineSwitches();
058: populateCommandLineSwitchesForXMLOutput(
059: XMLPrinter.DEFAULT_ENCODING,
060: XMLPrinter.DEFAULT_DTD_PREFIX);
061:
062: populateCommandLineSwitchesForScoping();
063: populateCommandLineSwitchesForFiltering();
064:
065: getCommandLine().addAliasSwitch("p2p", "package-scope",
066: "package-filter");
067: getCommandLine().addAliasSwitch("c2p", "class-scope",
068: "package-filter");
069: getCommandLine().addAliasSwitch("c2c", "class-scope",
070: "class-filter");
071: getCommandLine().addAliasSwitch("f2f", "feature-scope",
072: "feature-filter");
073: getCommandLine().addAliasSwitch("includes", "scope-includes",
074: "filter-includes");
075: getCommandLine().addAliasSwitch("excludes", "scope-excludes",
076: "filter-excludes");
077:
078: getCommandLine().addToggleSwitch("show-inbounds");
079: getCommandLine().addToggleSwitch("show-outbounds");
080: getCommandLine().addToggleSwitch("show-empty-nodes");
081:
082: getCommandLine().addToggleSwitch("xml");
083: getCommandLine().addToggleSwitch("validate");
084: getCommandLine().addToggleSwitch("minimize");
085: getCommandLine().addToggleSwitch("maximize");
086: getCommandLine().addToggleSwitch("copy-only");
087: }
088:
089: protected ParameterStrategy getParameterStrategy() {
090: return new AtLeastParameterStrategy(1);
091: }
092:
093: protected Collection<CommandLineException> parseCommandLine(
094: String[] args) {
095: Collection<CommandLineException> exceptions = super
096: .parseCommandLine(args);
097:
098: exceptions.addAll(validateCommandLineForScoping());
099: exceptions.addAll(validateCommandLineForFiltering());
100:
101: if (getCommandLine().getToggleSwitch("maximize")
102: && getCommandLine().getToggleSwitch("minimize")) {
103: exceptions.add(new CommandLineException(
104: "Only one of -maximize or -minimize is allowed"));
105: }
106:
107: return exceptions;
108: }
109:
110: protected void doProcessing() throws Exception {
111: SelectionCriteria scopeCriteria = getScopeCriteria();
112: SelectionCriteria filterCriteria = getFilterCriteria();
113:
114: GraphCopier copier;
115: if (getCommandLine().getToggleSwitch("copy-only")
116: || getCommandLine().getToggleSwitch("maximize")) {
117: copier = new GraphCopier(new SelectiveTraversalStrategy(
118: scopeCriteria, filterCriteria));
119: } else {
120: copier = new GraphSummarizer(scopeCriteria, filterCriteria);
121: }
122:
123: for (String filename : getCommandLine().getParameters()) {
124: Logger.getLogger(DependencyReporter.class).info(
125: "Reading " + filename);
126: getVerboseListener().print("Reading " + filename);
127:
128: Collection<PackageNode> packages = Collections.emptyList();
129:
130: if (filename.endsWith(".xml")) {
131: NodeLoader loader = new NodeLoader(getCommandLine()
132: .getToggleSwitch("validate"));
133: loader.addDependencyListener(getVerboseListener());
134: packages = loader.load(filename).getPackages().values();
135: }
136:
137: Logger.getLogger(DependencyReporter.class).info(
138: "Read in " + packages.size()
139: + " package(s) from \"" + filename + "\".");
140:
141: if (getCommandLine().getToggleSwitch("maximize")) {
142: new LinkMaximizer().traverseNodes(packages);
143: } else if (getCommandLine().getToggleSwitch("minimize")) {
144: new LinkMinimizer().traverseNodes(packages);
145: }
146:
147: copier.traverseNodes(packages);
148: }
149:
150: Logger.getLogger(DependencyReporter.class).info(
151: "Reporting "
152: + copier.getScopeFactory().getPackages()
153: .values().size() + " package(s) ...");
154:
155: getVerboseListener().print("Printing the graph ...");
156:
157: Printer printer;
158: if (getCommandLine().isPresent("xml")) {
159: printer = new XMLPrinter(out, getCommandLine()
160: .getSingleSwitch("encoding"), getCommandLine()
161: .getSingleSwitch("dtd-prefix"));
162: } else {
163: printer = new TextPrinter(out);
164: }
165:
166: if (getCommandLine().isPresent("indent-text")) {
167: printer.setIndentText(getCommandLine().getSingleSwitch(
168: "indent-text"));
169: }
170:
171: if (getCommandLine().isPresent("show-inbounds")
172: || getCommandLine().isPresent("show-outbounds")
173: || getCommandLine().isPresent("show-empty-nodes")) {
174: printer.setShowInbounds(getCommandLine().isPresent(
175: "show-inbounds"));
176: printer.setShowOutbounds(getCommandLine().isPresent(
177: "show-outbounds"));
178: printer.setShowEmptyNodes(getCommandLine().isPresent(
179: "show-empty-nodes"));
180: }
181:
182: printer.traverseNodes(copier.getScopeFactory().getPackages()
183: .values());
184: }
185:
186: public static void main(String[] args) throws Exception {
187: new DependencyReporter().run(args);
188: }
189: }
|