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.commandline.*;
042:
043: public class DependencyCycles extends Command {
044: public DependencyCycles() throws CommandLineException {
045: super ("DependencyCycles");
046: }
047:
048: protected void showSpecificUsage(PrintStream out) {
049: out.println();
050: out.println("Default is text output to the console.");
051: out.println();
052: }
053:
054: protected void populateCommandLineSwitches() {
055: super .populateCommandLineSwitches();
056: populateCommandLineSwitchesForXMLOutput(
057: XMLPrinter.DEFAULT_ENCODING,
058: XMLPrinter.DEFAULT_DTD_PREFIX);
059:
060: populateCommandLineSwitchesForStartCondition();
061:
062: getCommandLine().addSingleValueSwitch("maximum-cycle-length");
063:
064: getCommandLine().addToggleSwitch("xml");
065: getCommandLine().addToggleSwitch("validate");
066: }
067:
068: protected ParameterStrategy getParameterStrategy() {
069: return new AtLeastParameterStrategy(1);
070: }
071:
072: protected Collection<CommandLineException> parseCommandLine(
073: String[] args) {
074: Collection<CommandLineException> exceptions = super
075: .parseCommandLine(args);
076:
077: exceptions.addAll(validateCommandLineForScoping());
078:
079: return exceptions;
080: }
081:
082: protected void doProcessing() throws Exception {
083: NodeFactory factory = new NodeFactory();
084:
085: for (String filename : getCommandLine().getParameters()) {
086: Logger.getLogger(DependencyMetrics.class).info(
087: "Reading " + filename);
088: getVerboseListener().print("Reading " + filename);
089:
090: if (filename.endsWith(".xml")) {
091: NodeLoader loader = new NodeLoader(factory,
092: getCommandLine().getToggleSwitch("validate"));
093: loader.addDependencyListener(getVerboseListener());
094: loader.load(filename);
095: }
096:
097: Logger.getLogger(DependencyMetrics.class).info(
098: "Read \"" + filename + "\".");
099: }
100:
101: CycleDetector detector = new CycleDetector(getStartCriteria());
102:
103: if (getCommandLine().isPresent("maximum-cycle-length")) {
104: detector.setMaximumCycleLength(Integer
105: .parseInt(getCommandLine().getSingleSwitch(
106: "maximum-cycle-length")));
107: }
108:
109: detector.traverseNodes(factory.getPackages().values());
110:
111: getVerboseListener().print("Printing the graph ...");
112:
113: CyclePrinter printer;
114: if (getCommandLine().isPresent("xml")) {
115: printer = new XMLCyclePrinter(out, getCommandLine()
116: .getSingleSwitch("encoding"), getCommandLine()
117: .getSingleSwitch("dtd-prefix"));
118: } else {
119: printer = new TextCyclePrinter(out);
120: }
121:
122: if (getCommandLine().isPresent("indent-text")) {
123: printer.setIndentText(getCommandLine().getSingleSwitch(
124: "indent-text"));
125: }
126:
127: printer.visitCycles(detector.getCycles());
128: }
129:
130: public static void main(String[] args) throws Exception {
131: new DependencyCycles().run(args);
132: }
133: }
|