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:
037: import org.apache.log4j.*;
038:
039: import com.jeantessier.dependency.*;
040: import com.jeantessier.dependency.Printer;
041: import com.jeantessier.dependency.TextPrinter;
042: import com.jeantessier.commandline.*;
043:
044: public class DependencyClosure extends Command {
045: public DependencyClosure() throws CommandLineException {
046: super ("DependencyClosure");
047: }
048:
049: protected void showSpecificUsage(PrintStream out) {
050: out.println();
051: out.println("Defaults is text output to the console.");
052: out.println();
053: }
054:
055: protected void populateCommandLineSwitches() {
056: super .populateCommandLineSwitches();
057: populateCommandLineSwitchesForXMLOutput(
058: XMLPrinter.DEFAULT_ENCODING,
059: XMLPrinter.DEFAULT_DTD_PREFIX);
060:
061: populateCommandLineSwitchesForStartCondition();
062: populateCommandLineSwitchesForStopCondition();
063:
064: getCommandLine()
065: .addOptionalValueSwitch("maximum-inbound-depth");
066: getCommandLine().addOptionalValueSwitch(
067: "maximum-outbound-depth");
068:
069: getCommandLine().addToggleSwitch("xml");
070: getCommandLine().addToggleSwitch("validate");
071: }
072:
073: protected ParameterStrategy getParameterStrategy() {
074: return new AtLeastParameterStrategy(1);
075: }
076:
077: protected void doProcessing() throws Exception {
078: NodeFactory factory = new NodeFactory();
079:
080: for (String filename : getCommandLine().getParameters()) {
081: if (filename.endsWith(".xml")) {
082: getVerboseListener().print("Reading " + filename);
083:
084: NodeLoader loader = new NodeLoader(factory,
085: getCommandLine().getToggleSwitch("validate"));
086: loader.addDependencyListener(getVerboseListener());
087: loader.load(filename);
088:
089: getVerboseListener()
090: .print("Read \"" + filename + "\".");
091: } else {
092: getVerboseListener().print(
093: "Skipping \"" + filename + "\".");
094: }
095: }
096:
097: TransitiveClosure selector = new TransitiveClosure(
098: getStartCriteria(), getStopCriteria());
099:
100: try {
101: if (getCommandLine().isPresent("maximum-inbound-depth")) {
102: selector.setMaximumInboundDepth(Long
103: .parseLong(getCommandLine().getSingleSwitch(
104: "maximum-inbound-depth")));
105: }
106: } catch (NumberFormatException ex) {
107: selector
108: .setMaximumInboundDepth(TransitiveClosure.UNBOUNDED_DEPTH);
109: }
110:
111: try {
112: if (getCommandLine().isPresent("maximum-outbound-depth")) {
113: selector.setMaximumOutboundDepth(Long
114: .parseLong(getCommandLine().getSingleSwitch(
115: "maximum-outbound-depth")));
116: }
117: } catch (NumberFormatException ex) {
118: selector
119: .setMaximumOutboundDepth(TransitiveClosure.UNBOUNDED_DEPTH);
120: }
121:
122: Logger.getLogger(getClass()).info(
123: "Operating on " + factory.getPackages().values().size()
124: + " package(s) ...");
125:
126: selector.traverseNodes(factory.getPackages().values());
127:
128: Logger.getLogger(getClass()).info(
129: "Reporting "
130: + selector.getFactory().getPackages().values()
131: .size() + " package(s) ...");
132:
133: getVerboseListener().print("Printing the graph ...");
134:
135: Printer printer;
136: if (getCommandLine().isPresent("xml")) {
137: printer = new XMLPrinter(out, getCommandLine()
138: .getSingleSwitch("encoding"), getCommandLine()
139: .getSingleSwitch("dtd-prefix"));
140: } else {
141: printer = new TextPrinter(out);
142: }
143:
144: if (getCommandLine().isPresent("indent-text")) {
145: printer.setIndentText(getCommandLine().getSingleSwitch(
146: "indent-text"));
147: }
148:
149: printer.traverseNodes(selector.getFactory().getPackages()
150: .values());
151: }
152:
153: public static void main(String[] args) throws Exception {
154: new DependencyClosure().run(args);
155: }
156: }
|