~ubuntu-branches/ubuntu/raring/apgdiff/raring

« back to all changes in this revision

Viewing changes to src/main/java/cz/startnet/utils/pgdiff/PgDiffArguments.java

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Berg
  • Date: 2008-09-09 15:42:54 UTC
  • Revision ID: james.westby@ubuntu.com-20080909154254-458sv7ew1rczdal1
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: PgDiffArguments.java 91 2008-08-01 17:35:19Z fordfrog $
 
3
 */
 
4
package cz.startnet.utils.pgdiff;
 
5
 
 
6
import java.io.BufferedReader;
 
7
import java.io.IOException;
 
8
import java.io.InputStreamReader;
 
9
import java.io.PrintWriter;
 
10
 
 
11
/**
 
12
 * Contains parsed command line arguments.
 
13
 *
 
14
 * @author fordfrog
 
15
 * @version $Id: PgDiffArguments.java 91 2008-08-01 17:35:19Z fordfrog $
 
16
 */
 
17
public class PgDiffArguments {
 
18
 
 
19
    /**
 
20
     * Input file charset name.
 
21
     */
 
22
    private String inCharsetName = "UTF-8";
 
23
    /**
 
24
     * Path to the new dump file.
 
25
     */
 
26
    private String newDumpFile;
 
27
    /**
 
28
     * Path to the original dump file.
 
29
     */
 
30
    private String oldDumpFile;
 
31
    /**
 
32
     * Output file charset name.
 
33
     */
 
34
    private String outCharsetName = "UTF-8";
 
35
    /**
 
36
     * Whether DEFAULT ... should be added in case new column has NOT
 
37
     * NULL constraint. The default value is dropped later.
 
38
     */
 
39
    private boolean addDefaults;
 
40
    /**
 
41
     * Whether to enclose all commands in transaction.
 
42
     */
 
43
    private boolean addTransaction;
 
44
    /**
 
45
     * Whether to ignore whitespace while comparing content of functions.
 
46
     */
 
47
    private boolean ignoreFunctionWhitespace;
 
48
    /**
 
49
     * Whether to ignore START WITH on SEQUENCEs.
 
50
     */
 
51
    private boolean ignoreStartWith;
 
52
    /**
 
53
     * Whether to quote names when creating the diff SQL commands.
 
54
     */
 
55
    private boolean quoteNames;
 
56
    /**
 
57
     * Whether to display apgdiff version.
 
58
     */
 
59
    private boolean version;
 
60
 
 
61
    /**
 
62
     * Setter for {@link #addDefaults}.
 
63
     *
 
64
     * @param addDefaults {@link #addDefaults}
 
65
     */
 
66
    public void setAddDefaults(final boolean addDefaults) {
 
67
        this.addDefaults = addDefaults;
 
68
    }
 
69
 
 
70
    /**
 
71
     * Getter for {@link #addDefaults}.
 
72
     *
 
73
     * @return {@link #addDefaults}
 
74
     */
 
75
    public boolean isAddDefaults() {
 
76
        return addDefaults;
 
77
    }
 
78
 
 
79
    /**
 
80
     * Setter for {@link #addTransaction}.
 
81
     *
 
82
     * @param addTransaction {@link #addTransaction}
 
83
     */
 
84
    public void setAddTransaction(final boolean addTransaction) {
 
85
        this.addTransaction = addTransaction;
 
86
    }
 
87
 
 
88
    /**
 
89
     * Getter for {@link #addTransaction}.
 
90
     *
 
91
     * @return {@link #addTransaction}
 
92
     */
 
93
    public boolean isAddTransaction() {
 
94
        return addTransaction;
 
95
    }
 
96
 
 
97
    /**
 
98
     * Setter for {@link #ignoreFunctionWhitespace}.
 
99
     *
 
100
     * @param ignoreStartWith {@link #ignoreFunctionWhitespace}
 
101
     */
 
102
    public void setIgnoreFunctionWhitespace(
 
103
        final boolean ignoreFunctionWhitespace) {
 
104
        this.ignoreFunctionWhitespace = ignoreFunctionWhitespace;
 
105
    }
 
106
 
 
107
    /**
 
108
     * Getter for {@link #ignoreFunctionWhitespace}.
 
109
     *
 
110
     * @return {@link #ignoreFunctionWhitespace}
 
111
     */
 
112
    public boolean isIgnoreFunctionWhitespace() {
 
113
        return ignoreFunctionWhitespace;
 
114
    }
 
115
 
 
116
    /**
 
117
     * Setter for {@link #ignoreStartWith}.
 
118
     *
 
119
     * @param ignoreStartWith {@link #ignoreStartWith}
 
120
     */
 
121
    public void setIgnoreStartWith(final boolean ignoreStartWith) {
 
122
        this.ignoreStartWith = ignoreStartWith;
 
123
    }
 
124
 
 
125
    /**
 
126
     * Getter for {@link #ignoreStartWith}.
 
127
     *
 
128
     * @return {@link #ignoreStartWith}
 
129
     */
 
130
    public boolean isIgnoreStartWith() {
 
131
        return ignoreStartWith;
 
132
    }
 
133
 
 
134
    /**
 
135
     * Setter for {@link #newDumpFile}.
 
136
     *
 
137
     * @param newDumpFile {@link #newDumpFile}
 
138
     */
 
139
    public void setNewDumpFile(final String newDumpFile) {
 
140
        this.newDumpFile = newDumpFile;
 
141
    }
 
142
 
 
143
    /**
 
144
     * Getter for {@link #newDumpFile}.
 
145
     *
 
146
     * @return {@link #newDumpFile}
 
147
     */
 
148
    public String getNewDumpFile() {
 
149
        return newDumpFile;
 
150
    }
 
151
 
 
152
    /**
 
153
     * Setter for {@link #oldDumpFile}.
 
154
     *
 
155
     * @param oldDumpFile {@link #oldDumpFile}
 
156
     */
 
157
    public void setOldDumpFile(final String oldDumpFile) {
 
158
        this.oldDumpFile = oldDumpFile;
 
159
    }
 
160
 
 
161
    /**
 
162
     * Getter for {@link #oldDumpFile}.
 
163
     *
 
164
     * @return {@link #oldDumpFile}
 
165
     */
 
166
    public String getOldDumpFile() {
 
167
        return oldDumpFile;
 
168
    }
 
169
 
 
170
    /**
 
171
     * Setter for {@link #quoteNames}.
 
172
     *
 
173
     * @param quoteNames {@link #quoteNames}
 
174
     */
 
175
    public void setQuoteNames(final boolean quoteNames) {
 
176
        this.quoteNames = quoteNames;
 
177
    }
 
178
 
 
179
    /**
 
180
     * Getter for {@link #quoteNames}.
 
181
     *
 
182
     * @return {@link #quoteNames}
 
183
     */
 
184
    public boolean isQuoteNames() {
 
185
        return quoteNames;
 
186
    }
 
187
 
 
188
    /**
 
189
     * Setter for {@link #version}.
 
190
     *
 
191
     * @param version {@link #version}
 
192
     */
 
193
    public void setVersion(final boolean version) {
 
194
        this.version = version;
 
195
    }
 
196
 
 
197
    /**
 
198
     * Getter for {@link #version}.
 
199
     *
 
200
     * @return {@link #version}
 
201
     */
 
202
    public boolean isVersion() {
 
203
        return version;
 
204
    }
 
205
 
 
206
    /**
 
207
     * Parses command line arguments or outputs instructions.
 
208
     *
 
209
     * @param writer writer to be used for info output
 
210
     * @param args array of arguments
 
211
     *
 
212
     * @return true if arguments were parsed and execution can continue,
 
213
     *         otherwise false
 
214
     */
 
215
    public boolean parse(final PrintWriter writer, final String[] args) {
 
216
        boolean success = true;
 
217
        final int argsLength;
 
218
 
 
219
        if (args.length >= 2) {
 
220
            argsLength = args.length - 2;
 
221
        } else {
 
222
            argsLength = args.length;
 
223
        }
 
224
 
 
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]);
 
236
                i++;
 
237
            } else if ("--out-charset-name".equals(args[i])) {
 
238
                setOutCharsetName(args[i + 1]);
 
239
                i++;
 
240
            } else if ("--quote-names".equals(args[i])) {
 
241
                setQuoteNames(true);
 
242
            } else if ("--version".equals(args[i])) {
 
243
                setVersion(true);
 
244
            } else {
 
245
                writer.println("ERROR: Unknown option: " + args[i]);
 
246
                success = false;
 
247
 
 
248
                break;
 
249
            }
 
250
        }
 
251
 
 
252
        if ((args.length == 1) && isVersion()) {
 
253
            printVersion(writer);
 
254
            success = false;
 
255
        } else if (args.length < 2) {
 
256
            printUsage(writer);
 
257
            success = false;
 
258
        } else if (success) {
 
259
            setOldDumpFile(args[args.length - 2]);
 
260
            setNewDumpFile(args[args.length - 1]);
 
261
        }
 
262
 
 
263
        return success;
 
264
    }
 
265
 
 
266
    /**
 
267
     * Prints program usage.
 
268
     *
 
269
     * @param writer writer to print the usage to
 
270
     *
 
271
     * @throws RuntimeException Thrown if problem occured while reading usage
 
272
     *         info.
 
273
     */
 
274
    private void printUsage(final PrintWriter writer) {
 
275
        final BufferedReader reader =
 
276
            new BufferedReader(
 
277
            new InputStreamReader(
 
278
            getClass().getResourceAsStream("usage.txt")));
 
279
 
 
280
        try {
 
281
            String line = reader.readLine();
 
282
 
 
283
            while (line != null) {
 
284
                writer.println(line);
 
285
                line = reader.readLine();
 
286
            }
 
287
        } catch (final IOException ex) {
 
288
            throw new RuntimeException(
 
289
                "Problem occured while reading usage file",
 
290
                ex);
 
291
        } finally {
 
292
            try {
 
293
                reader.close();
 
294
            } catch (final IOException ex) {
 
295
                throw new RuntimeException(
 
296
                    "Problem occured while closing reader",
 
297
                    ex);
 
298
            }
 
299
        }
 
300
    }
 
301
 
 
302
    /**
 
303
     * Prints program version.
 
304
     *
 
305
     * @param writer writer to print the usage to
 
306
     *
 
307
     * @throws RuntimeException Thrown if problem occured while reading program
 
308
     *         version.
 
309
     */
 
310
    private void printVersion(final PrintWriter writer) {
 
311
        final BufferedReader reader =
 
312
            new BufferedReader(
 
313
            new InputStreamReader(
 
314
            getClass().getResourceAsStream("build_info")));
 
315
        writer.print("Version: ");
 
316
 
 
317
        try {
 
318
            writer.println(reader.readLine());
 
319
        } catch (final IOException ex) {
 
320
            throw new RuntimeException("Cannot read program version", ex);
 
321
        } finally {
 
322
            try {
 
323
                reader.close();
 
324
            } catch (final IOException ex) {
 
325
                throw new RuntimeException(
 
326
                    "Problem occured while closing reader",
 
327
                    ex);
 
328
            }
 
329
        }
 
330
    }
 
331
 
 
332
    /**
 
333
     * Getter for {@link #inCharsetName}.
 
334
     *
 
335
     * @return {@link #inCharsetName}
 
336
     */
 
337
    public String getInCharsetName() {
 
338
        return inCharsetName;
 
339
    }
 
340
 
 
341
    /**
 
342
     * Setter for {@link #inCharsetName}.
 
343
     *
 
344
     * @param inCharsetName {@link #inCharsetName}
 
345
     */
 
346
    public void setInCharsetName(final String inCharsetName) {
 
347
        this.inCharsetName = inCharsetName;
 
348
    }
 
349
 
 
350
    /**
 
351
     * Getter for {@link #outCharsetName}.
 
352
     *
 
353
     * @return {@link #outCharsetName}
 
354
     */
 
355
    public String getOutCharsetName() {
 
356
        return outCharsetName;
 
357
    }
 
358
 
 
359
    /**
 
360
     * Setter for {@link #outCharsetName}.
 
361
     *
 
362
     * @param outCharsetName {@link #outCharsetName}
 
363
     */
 
364
    public void setOutCharsetName(final String outCharsetName) {
 
365
        this.outCharsetName = outCharsetName;
 
366
    }
 
367
}