001: package com.jeantessier.dependencyfinder.cli;
002:
003: import java.lang.reflect.*;
004: import java.io.*;
005:
006: import com.jeantessier.commandline.*;
007: import com.jeantessier.diff.*;
008: import org.apache.log4j.*;
009:
010: public abstract class DiffCommand extends Command {
011: public static final String API_STRATEGY = "api";
012: public static final String INCOMPATIBLE_STRATEGY = "incompatible";
013:
014: public static final String DEFAULT_LEVEL = API_STRATEGY;
015:
016: public DiffCommand(String name) throws CommandLineException {
017: super (name);
018: }
019:
020: protected void showSpecificUsage(PrintStream out) {
021: out.println();
022: out.println("Defaults is text output to the console.");
023: out.println();
024: }
025:
026: protected void populateCommandLineSwitches() {
027: super .populateCommandLineSwitches();
028: populateCommandLineSwitchesForXMLOutput(
029: Report.DEFAULT_ENCODING, Report.DEFAULT_DTD_PREFIX);
030:
031: getCommandLine().addSingleValueSwitch("name");
032: getCommandLine().addMultipleValuesSwitch("old", true);
033: getCommandLine().addSingleValueSwitch("old-label");
034: getCommandLine().addMultipleValuesSwitch("new", true);
035: getCommandLine().addSingleValueSwitch("new-label");
036: getCommandLine().addSingleValueSwitch("filter");
037: getCommandLine().addToggleSwitch("code");
038: getCommandLine().addSingleValueSwitch("level", DEFAULT_LEVEL);
039: }
040:
041: protected DifferenceStrategy getStrategy(String level,
042: DifferenceStrategy baseStrategy) {
043: DifferenceStrategy result;
044:
045: if (API_STRATEGY.equals(level)) {
046: result = new APIDifferenceStrategy(baseStrategy);
047: } else if (INCOMPATIBLE_STRATEGY.equals(level)) {
048: result = new IncompatibleDifferenceStrategy(baseStrategy);
049: } else {
050: try {
051: Constructor constructor;
052: try {
053: constructor = Class.forName(level).getConstructor(
054: DifferenceStrategy.class);
055: result = (DifferenceStrategy) constructor
056: .newInstance(baseStrategy);
057: } catch (NoSuchMethodException ex) {
058: result = (DifferenceStrategy) Class.forName(level)
059: .newInstance();
060: }
061: } catch (InvocationTargetException ex) {
062: Logger.getLogger(getClass()).error(
063: "Unknown level \"" + level
064: + "\", using default level \""
065: + DEFAULT_LEVEL + "\"", ex);
066: result = getDefaultStrategy(baseStrategy);
067: } catch (InstantiationException ex) {
068: Logger.getLogger(getClass()).error(
069: "Unknown level \"" + level
070: + "\", using default level \""
071: + DEFAULT_LEVEL + "\"", ex);
072: result = getDefaultStrategy(baseStrategy);
073: } catch (IllegalAccessException ex) {
074: Logger.getLogger(getClass()).error(
075: "Unknown level \"" + level
076: + "\", using default level \""
077: + DEFAULT_LEVEL + "\"", ex);
078: result = getDefaultStrategy(baseStrategy);
079: } catch (ClassNotFoundException ex) {
080: Logger.getLogger(getClass()).error(
081: "Unknown level \"" + level
082: + "\", using default level \""
083: + DEFAULT_LEVEL + "\"", ex);
084: result = getDefaultStrategy(baseStrategy);
085: } catch (ClassCastException ex) {
086: Logger.getLogger(getClass()).error(
087: "Unknown level \"" + level
088: + "\", using default level \""
089: + DEFAULT_LEVEL + "\"", ex);
090: result = getDefaultStrategy(baseStrategy);
091: }
092: }
093:
094: return result;
095: }
096:
097: protected DifferenceStrategy getBaseStrategy(boolean useCode) {
098: DifferenceStrategy result;
099:
100: if (useCode) {
101: result = new CodeDifferenceStrategy();
102: } else {
103: result = new NoDifferenceStrategy();
104: }
105:
106: return result;
107: }
108:
109: private DifferenceStrategy getDefaultStrategy(
110: DifferenceStrategy strategy) {
111: return new APIDifferenceStrategy(strategy);
112: }
113: }
|