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 com.jeantessier.dependency.*;
039: import com.jeantessier.commandline.*;
040:
041: import org.apache.log4j.*;
042:
043: public class DependencyMetrics extends Command {
044: public DependencyMetrics() throws CommandLineException {
045: super ("DependencyMetrics");
046: }
047:
048: protected void showSpecificUsage(PrintStream out) {
049: out.println();
050: out
051: .println("If no files are specified, it processes the current directory.");
052: out.println();
053: out
054: .println("If file is a directory, it is recusively scanned for files");
055: out.println("ending in \".class\".");
056: out.println();
057: out.println("Default is text output to the console.");
058: out.println();
059: }
060:
061: protected void populateCommandLineSwitches() {
062: super .populateCommandLineSwitches();
063:
064: populateCommandLineSwitchesForScoping();
065: populateCommandLineSwitchesForFiltering();
066:
067: getCommandLine().addAliasSwitch("p2p", "package-scope",
068: "package-filter");
069: getCommandLine().addAliasSwitch("c2p", "class-scope",
070: "package-filter");
071: getCommandLine().addAliasSwitch("c2c", "class-scope",
072: "class-filter");
073: getCommandLine().addAliasSwitch("f2f", "feature-scope",
074: "feature-filter");
075: getCommandLine().addAliasSwitch("includes", "scope-includes",
076: "filter-includes");
077: getCommandLine().addAliasSwitch("excludes", "scope-excludes",
078: "filter-excludes");
079:
080: getCommandLine().addToggleSwitch("list");
081: getCommandLine().addToggleSwitch("chart-classes-per-package");
082: getCommandLine().addToggleSwitch("chart-features-per-class");
083: getCommandLine().addToggleSwitch("chart-inbounds-per-package");
084: getCommandLine().addToggleSwitch("chart-outbounds-per-package");
085: getCommandLine().addToggleSwitch("chart-inbounds-per-class");
086: getCommandLine().addToggleSwitch("chart-outbounds-per-class");
087: getCommandLine().addToggleSwitch("chart-inbounds-per-feature");
088: getCommandLine().addToggleSwitch("chart-outbounds-per-feature");
089:
090: getCommandLine().addAliasSwitch("chart-inbounds",
091: "chart-inbounds-per-package",
092: "chart-inbounds-per-class",
093: "chart-inbounds-per-feature");
094: getCommandLine().addAliasSwitch("chart-outbounds",
095: "chart-outbounds-per-package",
096: "chart-outbounds-per-class",
097: "chart-outbounds-per-feature");
098: getCommandLine().addAliasSwitch("chart-packages",
099: "chart-classes-per-package",
100: "chart-inbounds-per-package",
101: "chart-outbounds-per-package");
102: getCommandLine().addAliasSwitch("chart-classes",
103: "chart-features-per-class", "chart-inbounds-per-class",
104: "chart-outbounds-per-class");
105: getCommandLine().addAliasSwitch("chart-features",
106: "chart-inbounds-per-feature",
107: "chart-outbounds-per-feature");
108: getCommandLine().addAliasSwitch("chart-all",
109: "chart-classes-per-package",
110: "chart-features-per-class",
111: "chart-inbounds-per-package",
112: "chart-outbounds-per-package",
113: "chart-inbounds-per-class",
114: "chart-outbounds-per-class",
115: "chart-inbounds-per-feature",
116: "chart-outbounds-per-feature");
117:
118: getCommandLine().addToggleSwitch("validate");
119: }
120:
121: protected ParameterStrategy getParameterStrategy() {
122: return new AtLeastParameterStrategy(1);
123: }
124:
125: protected Collection<CommandLineException> parseCommandLine(
126: String[] args) {
127: Collection<CommandLineException> exceptions = super
128: .parseCommandLine(args);
129:
130: exceptions.addAll(validateCommandLineForScoping());
131: exceptions.addAll(validateCommandLineForFiltering());
132:
133: return exceptions;
134: }
135:
136: public void doProcessing() throws Exception {
137: NodeFactory factory = new NodeFactory();
138:
139: for (String filename : getCommandLine().getParameters()) {
140: Logger.getLogger(getClass()).info("Reading " + filename);
141: getVerboseListener().print("Reading " + filename);
142:
143: if (filename.endsWith(".xml")) {
144: NodeLoader loader = new NodeLoader(factory,
145: getCommandLine().getToggleSwitch("validate"));
146: loader.addDependencyListener(getVerboseListener());
147: loader.load(filename);
148: }
149:
150: Logger.getLogger(getClass()).info(
151: "Read \"" + filename + "\".");
152: }
153:
154: MetricsReport reporter = new MetricsReport(out);
155:
156: reporter.setListingElements(getCommandLine().getToggleSwitch(
157: "list"));
158: reporter.setChartingClassesPerPackage(getCommandLine()
159: .getToggleSwitch("chart-classes-per-package"));
160: reporter.setChartingFeaturesPerClass(getCommandLine()
161: .getToggleSwitch("chart-features-per-class"));
162: reporter.setChartingInboundsPerPackage(getCommandLine()
163: .getToggleSwitch("chart-inbounds-per-package"));
164: reporter.setChartingOutboundsPerPackage(getCommandLine()
165: .getToggleSwitch("chart-outbounds-per-package"));
166: reporter.setChartingInboundsPerClass(getCommandLine()
167: .getToggleSwitch("chart-inbounds-per-class"));
168: reporter.setChartingOutboundsPerClass(getCommandLine()
169: .getToggleSwitch("chart-outbounds-per-class"));
170: reporter.setChartingInboundsPerFeature(getCommandLine()
171: .getToggleSwitch("chart-inbounds-per-feature"));
172: reporter.setChartingOutboundsPerFeature(getCommandLine()
173: .getToggleSwitch("chart-outbounds-per-feature"));
174:
175: SelectionCriteria scopeCriteria = getScopeCriteria();
176: SelectionCriteria filterCriteria = getFilterCriteria();
177:
178: Logger.getLogger(getClass()).info(
179: "Reporting on " + factory.getPackages().size()
180: + " package(s) ...");
181: getVerboseListener().print(
182: "Reporting on " + factory.getPackages().size()
183: + " package(s) ...");
184:
185: MetricsGatherer metrics = new MetricsGatherer(
186: new SelectiveTraversalStrategy(scopeCriteria,
187: filterCriteria));
188: metrics.traverseNodes(factory.getPackages().values());
189: reporter.process(metrics);
190: }
191:
192: public static void main(String[] args) throws Exception {
193: new DependencyMetrics().run(args);
194: }
195: }
|