1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
package com.jeantessier.dependencyfinder.cli; |
34 | |
|
35 | |
import java.io.*; |
36 | |
import java.lang.reflect.*; |
37 | |
import java.util.*; |
38 | |
|
39 | |
import org.apache.log4j.*; |
40 | |
|
41 | |
import com.jeantessier.commandline.*; |
42 | |
import com.jeantessier.diff.*; |
43 | |
|
44 | 2 | public abstract class DiffCommand extends Command { |
45 | |
public static final String API_STRATEGY = "api"; |
46 | |
public static final String INCOMPATIBLE_STRATEGY = "incompatible"; |
47 | |
|
48 | |
public static final String DEFAULT_LEVEL = API_STRATEGY; |
49 | |
|
50 | |
protected void showSpecificUsage(PrintStream out) { |
51 | 2 | out.println(); |
52 | 2 | out.println("Defaults is text output to the console."); |
53 | 2 | out.println(); |
54 | 2 | } |
55 | |
|
56 | |
protected void populateCommandLineSwitches() { |
57 | 16 | super.populateCommandLineSwitches(); |
58 | 16 | populateCommandLineSwitchesForXMLOutput(Report.DEFAULT_ENCODING, Report.DEFAULT_DTD_PREFIX, Report.DEFAULT_INDENT_TEXT); |
59 | |
|
60 | 16 | getCommandLine().addSingleValueSwitch("name"); |
61 | 16 | getCommandLine().addMultipleValuesSwitch("old", true); |
62 | 16 | getCommandLine().addSingleValueSwitch("old-label"); |
63 | 16 | getCommandLine().addMultipleValuesSwitch("new", true); |
64 | 16 | getCommandLine().addSingleValueSwitch("new-label"); |
65 | 16 | getCommandLine().addSingleValueSwitch("filter"); |
66 | 16 | getCommandLine().addToggleSwitch("code"); |
67 | 16 | getCommandLine().addSingleValueSwitch("level", DEFAULT_LEVEL); |
68 | 16 | } |
69 | |
|
70 | |
protected Collection<CommandLineException> parseCommandLine(String[] args) { |
71 | 14 | Collection<CommandLineException> exceptions = super.parseCommandLine(args); |
72 | |
|
73 | 14 | if (!getCommandLine().isPresent("old-label")) { |
74 | 14 | getCommandLine().getSwitch("old-label").setValue(getCommandLine().getMultipleSwitch("old").toString()); |
75 | |
} |
76 | |
|
77 | 14 | if (!getCommandLine().isPresent("new-label")) { |
78 | 14 | getCommandLine().getSwitch("new-label").setValue(getCommandLine().getMultipleSwitch("new").toString()); |
79 | |
} |
80 | |
|
81 | 14 | return exceptions; |
82 | |
} |
83 | |
|
84 | |
protected DifferencesFactory getDifferencesFactory() throws IOException { |
85 | 0 | DifferenceStrategy baseStrategy = getBaseStrategy(getCommandLine().getToggleSwitch("code")); |
86 | 0 | DifferenceStrategy strategy = getStrategy(getCommandLine().getSingleSwitch("level"), baseStrategy); |
87 | |
|
88 | 0 | if (getCommandLine().isPresent("filter")) { |
89 | 0 | strategy = new ListBasedDifferenceStrategy(strategy, getCommandLine().getSingleSwitch("filter")); |
90 | |
} |
91 | |
|
92 | 0 | return new DifferencesFactory(strategy); |
93 | |
} |
94 | |
|
95 | |
private DifferenceStrategy getBaseStrategy(boolean useCode) { |
96 | |
DifferenceStrategy result; |
97 | |
|
98 | 0 | if (useCode) { |
99 | 0 | result = new CodeDifferenceStrategy(); |
100 | |
} else { |
101 | 0 | result = new NoDifferenceStrategy(); |
102 | |
} |
103 | |
|
104 | 0 | return result; |
105 | |
} |
106 | |
|
107 | |
private DifferenceStrategy getStrategy(String level, DifferenceStrategy baseStrategy) { |
108 | |
DifferenceStrategy result; |
109 | |
|
110 | 0 | if (API_STRATEGY.equals(level)) { |
111 | 0 | result = new APIDifferenceStrategy(baseStrategy); |
112 | 0 | } else if (INCOMPATIBLE_STRATEGY.equals(level)) { |
113 | 0 | result = new IncompatibleDifferenceStrategy(baseStrategy); |
114 | |
} else { |
115 | |
try { |
116 | |
Constructor constructor; |
117 | |
try { |
118 | 0 | constructor = Class.forName(level).getConstructor(DifferenceStrategy.class); |
119 | 0 | result = (DifferenceStrategy) constructor.newInstance(baseStrategy); |
120 | 0 | } catch (NoSuchMethodException ex) { |
121 | 0 | result = (DifferenceStrategy) Class.forName(level).newInstance(); |
122 | 0 | } |
123 | 0 | } catch (InvocationTargetException ex) { |
124 | 0 | Logger.getLogger(getClass()).error("Unknown level \"" + level + "\", using default level \"" + DEFAULT_LEVEL + "\"", ex); |
125 | 0 | result = getDefaultStrategy(baseStrategy); |
126 | 0 | } catch (InstantiationException ex) { |
127 | 0 | Logger.getLogger(getClass()).error("Unknown level \"" + level + "\", using default level \"" + DEFAULT_LEVEL + "\"", ex); |
128 | 0 | result = getDefaultStrategy(baseStrategy); |
129 | 0 | } catch (IllegalAccessException ex) { |
130 | 0 | Logger.getLogger(getClass()).error("Unknown level \"" + level + "\", using default level \"" + DEFAULT_LEVEL + "\"", ex); |
131 | 0 | result = getDefaultStrategy(baseStrategy); |
132 | 0 | } catch (ClassNotFoundException ex) { |
133 | 0 | Logger.getLogger(getClass()).error("Unknown level \"" + level + "\", using default level \"" + DEFAULT_LEVEL + "\"", ex); |
134 | 0 | result = getDefaultStrategy(baseStrategy); |
135 | 0 | } catch (ClassCastException ex) { |
136 | 0 | Logger.getLogger(getClass()).error("Unknown level \"" + level + "\", using default level \"" + DEFAULT_LEVEL + "\"", ex); |
137 | 0 | result = getDefaultStrategy(baseStrategy); |
138 | 0 | } |
139 | |
} |
140 | |
|
141 | 0 | return result; |
142 | |
} |
143 | |
|
144 | |
private DifferenceStrategy getDefaultStrategy(DifferenceStrategy strategy) { |
145 | 0 | return new APIDifferenceStrategy(strategy); |
146 | |
} |
147 | |
} |