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.classreader.*;
039: import com.jeantessier.dependency.*;
040: import com.jeantessier.commandline.*;
041:
042: public class DependencyExtractor extends Command {
043: public DependencyExtractor() throws CommandLineException {
044: super ("DependencyExtractor");
045: }
046:
047: protected void showSpecificUsage(PrintStream out) {
048: out.println();
049: out
050: .println("If no files are specified, it processes the current directory.");
051: out.println();
052: out
053: .println("If file is a directory, it is recusively scanned for files");
054: out.println("ending in \".class\".");
055: out.println();
056: out.println("Defaults is text output to the console.");
057: out.println();
058: }
059:
060: protected void populateCommandLineSwitches() {
061: super .populateCommandLineSwitches();
062: populateCommandLineSwitchesForXMLOutput(
063: com.jeantessier.dependency.XMLPrinter.DEFAULT_ENCODING,
064: com.jeantessier.dependency.XMLPrinter.DEFAULT_DTD_PREFIX);
065: populateCommandLineSwitchesForFiltering();
066:
067: getCommandLine().addToggleSwitch("maximize");
068: getCommandLine().addToggleSwitch("minimize");
069:
070: getCommandLine().addToggleSwitch("xml");
071: }
072:
073: protected Collection<CommandLineException> parseCommandLine(
074: String[] args) {
075: Collection<CommandLineException> exceptions = super
076: .parseCommandLine(args);
077:
078: exceptions.addAll(validateCommandLineForFiltering());
079:
080: if (getCommandLine().getToggleSwitch("maximize")
081: && getCommandLine().getToggleSwitch("minimize")) {
082: exceptions.add(new CommandLineException(
083: "Only one of -maximize or -minimize is allowed"));
084: }
085:
086: return exceptions;
087: }
088:
089: protected void doProcessing() throws Exception {
090: SelectionCriteria filterCriteria = getFilterCriteria();
091:
092: List<String> parameters = getCommandLine().getParameters();
093: if (parameters.size() == 0) {
094: parameters.add(".");
095: }
096:
097: NodeFactory factory = new NodeFactory();
098: CodeDependencyCollector collector = new CodeDependencyCollector(
099: factory, filterCriteria);
100:
101: ClassfileLoader loader = new TransientClassfileLoader();
102: loader
103: .addLoadListener(new LoadListenerVisitorAdapter(
104: collector));
105: loader.addLoadListener(getVerboseListener());
106: loader.load(parameters);
107:
108: if (getCommandLine().getToggleSwitch("minimize")) {
109: LinkMinimizer minimizer = new LinkMinimizer();
110: minimizer.traverseNodes(factory.getPackages().values());
111: } else if (getCommandLine().getToggleSwitch("maximize")) {
112: LinkMaximizer maximizer = new LinkMaximizer();
113: maximizer.traverseNodes(factory.getPackages().values());
114: }
115:
116: getVerboseListener().print("Printing the graph ...");
117:
118: com.jeantessier.dependency.Printer printer;
119: if (getCommandLine().getToggleSwitch("xml")) {
120: printer = new com.jeantessier.dependency.XMLPrinter(out,
121: getCommandLine().getSingleSwitch("encoding"),
122: getCommandLine().getSingleSwitch("dtd-prefix"));
123: } else {
124: printer = new com.jeantessier.dependency.TextPrinter(out);
125: }
126:
127: if (getCommandLine().isPresent("indent-text")) {
128: printer.setIndentText(getCommandLine().getSingleSwitch(
129: "indent-text"));
130: }
131:
132: printer.traverseNodes(factory.getPackages().values());
133: }
134:
135: public static void main(String[] args) throws Exception {
136: new DependencyExtractor().run(args);
137: }
138: }
|