2
* $Id: PgDiffArguments.java 91 2008-08-01 17:35:19Z fordfrog $
4
package cz.startnet.utils.pgdiff;
6
import java.io.BufferedReader;
7
import java.io.IOException;
8
import java.io.InputStreamReader;
9
import java.io.PrintWriter;
12
* Contains parsed command line arguments.
15
* @version $Id: PgDiffArguments.java 91 2008-08-01 17:35:19Z fordfrog $
17
public class PgDiffArguments {
20
* Input file charset name.
22
private String inCharsetName = "UTF-8";
24
* Path to the new dump file.
26
private String newDumpFile;
28
* Path to the original dump file.
30
private String oldDumpFile;
32
* Output file charset name.
34
private String outCharsetName = "UTF-8";
36
* Whether DEFAULT ... should be added in case new column has NOT
37
* NULL constraint. The default value is dropped later.
39
private boolean addDefaults;
41
* Whether to enclose all commands in transaction.
43
private boolean addTransaction;
45
* Whether to ignore whitespace while comparing content of functions.
47
private boolean ignoreFunctionWhitespace;
49
* Whether to ignore START WITH on SEQUENCEs.
51
private boolean ignoreStartWith;
53
* Whether to quote names when creating the diff SQL commands.
55
private boolean quoteNames;
57
* Whether to display apgdiff version.
59
private boolean version;
62
* Setter for {@link #addDefaults}.
64
* @param addDefaults {@link #addDefaults}
66
public void setAddDefaults(final boolean addDefaults) {
67
this.addDefaults = addDefaults;
71
* Getter for {@link #addDefaults}.
73
* @return {@link #addDefaults}
75
public boolean isAddDefaults() {
80
* Setter for {@link #addTransaction}.
82
* @param addTransaction {@link #addTransaction}
84
public void setAddTransaction(final boolean addTransaction) {
85
this.addTransaction = addTransaction;
89
* Getter for {@link #addTransaction}.
91
* @return {@link #addTransaction}
93
public boolean isAddTransaction() {
94
return addTransaction;
98
* Setter for {@link #ignoreFunctionWhitespace}.
100
* @param ignoreStartWith {@link #ignoreFunctionWhitespace}
102
public void setIgnoreFunctionWhitespace(
103
final boolean ignoreFunctionWhitespace) {
104
this.ignoreFunctionWhitespace = ignoreFunctionWhitespace;
108
* Getter for {@link #ignoreFunctionWhitespace}.
110
* @return {@link #ignoreFunctionWhitespace}
112
public boolean isIgnoreFunctionWhitespace() {
113
return ignoreFunctionWhitespace;
117
* Setter for {@link #ignoreStartWith}.
119
* @param ignoreStartWith {@link #ignoreStartWith}
121
public void setIgnoreStartWith(final boolean ignoreStartWith) {
122
this.ignoreStartWith = ignoreStartWith;
126
* Getter for {@link #ignoreStartWith}.
128
* @return {@link #ignoreStartWith}
130
public boolean isIgnoreStartWith() {
131
return ignoreStartWith;
135
* Setter for {@link #newDumpFile}.
137
* @param newDumpFile {@link #newDumpFile}
139
public void setNewDumpFile(final String newDumpFile) {
140
this.newDumpFile = newDumpFile;
144
* Getter for {@link #newDumpFile}.
146
* @return {@link #newDumpFile}
148
public String getNewDumpFile() {
153
* Setter for {@link #oldDumpFile}.
155
* @param oldDumpFile {@link #oldDumpFile}
157
public void setOldDumpFile(final String oldDumpFile) {
158
this.oldDumpFile = oldDumpFile;
162
* Getter for {@link #oldDumpFile}.
164
* @return {@link #oldDumpFile}
166
public String getOldDumpFile() {
171
* Setter for {@link #quoteNames}.
173
* @param quoteNames {@link #quoteNames}
175
public void setQuoteNames(final boolean quoteNames) {
176
this.quoteNames = quoteNames;
180
* Getter for {@link #quoteNames}.
182
* @return {@link #quoteNames}
184
public boolean isQuoteNames() {
189
* Setter for {@link #version}.
191
* @param version {@link #version}
193
public void setVersion(final boolean version) {
194
this.version = version;
198
* Getter for {@link #version}.
200
* @return {@link #version}
202
public boolean isVersion() {
207
* Parses command line arguments or outputs instructions.
209
* @param writer writer to be used for info output
210
* @param args array of arguments
212
* @return true if arguments were parsed and execution can continue,
215
public boolean parse(final PrintWriter writer, final String[] args) {
216
boolean success = true;
217
final int argsLength;
219
if (args.length >= 2) {
220
argsLength = args.length - 2;
222
argsLength = args.length;
225
for (int i = 0; i < argsLength; i++) {
226
if ("--add-defaults".equals(args[i])) {
227
setAddDefaults(true);
228
} else if ("--add-transaction".equals(args[i])) {
229
setAddTransaction(true);
230
} else if ("--ignore-function-whitespace".equals(args[i])) {
231
setIgnoreFunctionWhitespace(true);
232
} else if ("--ignore-start-with".equals(args[i])) {
233
setIgnoreStartWith(true);
234
} else if ("--in-charset-name".equals(args[i])) {
235
setInCharsetName(args[i + 1]);
237
} else if ("--out-charset-name".equals(args[i])) {
238
setOutCharsetName(args[i + 1]);
240
} else if ("--quote-names".equals(args[i])) {
242
} else if ("--version".equals(args[i])) {
245
writer.println("ERROR: Unknown option: " + args[i]);
252
if ((args.length == 1) && isVersion()) {
253
printVersion(writer);
255
} else if (args.length < 2) {
258
} else if (success) {
259
setOldDumpFile(args[args.length - 2]);
260
setNewDumpFile(args[args.length - 1]);
267
* Prints program usage.
269
* @param writer writer to print the usage to
271
* @throws RuntimeException Thrown if problem occured while reading usage
274
private void printUsage(final PrintWriter writer) {
275
final BufferedReader reader =
277
new InputStreamReader(
278
getClass().getResourceAsStream("usage.txt")));
281
String line = reader.readLine();
283
while (line != null) {
284
writer.println(line);
285
line = reader.readLine();
287
} catch (final IOException ex) {
288
throw new RuntimeException(
289
"Problem occured while reading usage file",
294
} catch (final IOException ex) {
295
throw new RuntimeException(
296
"Problem occured while closing reader",
303
* Prints program version.
305
* @param writer writer to print the usage to
307
* @throws RuntimeException Thrown if problem occured while reading program
310
private void printVersion(final PrintWriter writer) {
311
final BufferedReader reader =
313
new InputStreamReader(
314
getClass().getResourceAsStream("build_info")));
315
writer.print("Version: ");
318
writer.println(reader.readLine());
319
} catch (final IOException ex) {
320
throw new RuntimeException("Cannot read program version", ex);
324
} catch (final IOException ex) {
325
throw new RuntimeException(
326
"Problem occured while closing reader",
333
* Getter for {@link #inCharsetName}.
335
* @return {@link #inCharsetName}
337
public String getInCharsetName() {
338
return inCharsetName;
342
* Setter for {@link #inCharsetName}.
344
* @param inCharsetName {@link #inCharsetName}
346
public void setInCharsetName(final String inCharsetName) {
347
this.inCharsetName = inCharsetName;
351
* Getter for {@link #outCharsetName}.
353
* @return {@link #outCharsetName}
355
public String getOutCharsetName() {
356
return outCharsetName;
360
* Setter for {@link #outCharsetName}.
362
* @param outCharsetName {@link #outCharsetName}
364
public void setOutCharsetName(final String outCharsetName) {
365
this.outCharsetName = outCharsetName;