1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
5 | * library suite from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * Author: John Thompson and David Bainbridge,
|
---|
9 | * Greenstone Digital Library, University of Waikato
|
---|
10 | *
|
---|
11 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
12 | *
|
---|
13 | * This program is free software; you can redistribute it and/or modify
|
---|
14 | * it under the terms of the GNU General Public License as published by
|
---|
15 | * the Free Software Foundation; either version 2 of the License, or
|
---|
16 | * (at your option) any later version.
|
---|
17 | *
|
---|
18 | * This program is distributed in the hope that it will be useful,
|
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
21 | * GNU General Public License for more details.
|
---|
22 | *
|
---|
23 | * You should have received a copy of the GNU General Public License
|
---|
24 | * along with this program; if not, write to the Free Software
|
---|
25 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
26 | *########################################################################
|
---|
27 | */
|
---|
28 | package org.greenstone.gatherer;
|
---|
29 |
|
---|
30 | import java.io.*;
|
---|
31 | import org.greenstone.gatherer.util.StaticStrings;
|
---|
32 | import org.greenstone.gatherer.util.Utility;
|
---|
33 |
|
---|
34 | public class GetOpt
|
---|
35 | {
|
---|
36 | public boolean debug = false;
|
---|
37 | public boolean feedback_enabled = false;
|
---|
38 | public boolean mirroring_enabled = false;
|
---|
39 | public boolean no_load = false;
|
---|
40 |
|
---|
41 | public String exec_path = null;
|
---|
42 | public String extra = null;
|
---|
43 | public String filename = null;
|
---|
44 | public String gsdl_path = null;
|
---|
45 | public String gsdl3_path = null;
|
---|
46 | public String perl_path = null;
|
---|
47 | public String site_name = null; // for GS3
|
---|
48 | public String servlet_path = null;
|
---|
49 | public String wget_path = null;
|
---|
50 | public String wget_version_str = StaticStrings.NO_WGET_STR;
|
---|
51 |
|
---|
52 | GetOpt(String[] args)
|
---|
53 | {
|
---|
54 | // Default dictionary. Only used for starting error messages.
|
---|
55 | Dictionary dictionary = new Dictionary(null, null);
|
---|
56 |
|
---|
57 | // Parse arguments
|
---|
58 | int argument_index = 0;
|
---|
59 | String next_token = null;
|
---|
60 |
|
---|
61 | while(argument_index < args.length || next_token != null) {
|
---|
62 | // 1. We start by attempting to parse an argument name. An argument
|
---|
63 | // must start with a '-', and should not contain spaces. If
|
---|
64 | // anything else is encountered it is ignored.
|
---|
65 |
|
---|
66 | String argument_name = null;
|
---|
67 | if(next_token == null) {
|
---|
68 | next_token = args[argument_index];
|
---|
69 | argument_index++;
|
---|
70 | }
|
---|
71 |
|
---|
72 | if(next_token.startsWith(StaticStrings.MINUS_CHARACTER)) {
|
---|
73 | // Trim second '-' just to be kind to Unixy-type people
|
---|
74 | if(next_token.startsWith(StaticStrings.MINUS_CHARACTER + StaticStrings.MINUS_CHARACTER)) {
|
---|
75 | argument_name = next_token.substring(1);
|
---|
76 | }
|
---|
77 | else {
|
---|
78 | argument_name = next_token;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | next_token = null;
|
---|
82 | // 2. If we now have an argument name we continue by attempting
|
---|
83 | // to parse a value. A value is taken to be the sequence of
|
---|
84 | // space seperated Strings between the last argument name
|
---|
85 | // and up to but not including the next argument name. Of
|
---|
86 | // course an argument needn't have any value (ie -debug,
|
---|
87 | // -help), in which case value will be null.
|
---|
88 | if(argument_name != null) {
|
---|
89 | String argument_value = null;
|
---|
90 | StringBuffer argument_value_buffer = new StringBuffer("");
|
---|
91 | while(argument_index < args.length && next_token == null) {
|
---|
92 | next_token = args[argument_index];
|
---|
93 | argument_index++;
|
---|
94 | // If we just parsed an arbitary String then append it to value, followed by a single space
|
---|
95 | if(!next_token.startsWith(StaticStrings.MINUS_CHARACTER)) {
|
---|
96 | argument_value_buffer.append(next_token);
|
---|
97 | argument_value_buffer.append(StaticStrings.SPACE_CHARACTER);
|
---|
98 | next_token = null;
|
---|
99 | }
|
---|
100 | // If the argument token retrieved is an argument name,
|
---|
101 | // then leave it in next_token, which will cause the
|
---|
102 | // argument parsing process to move onto the next step.
|
---|
103 | }
|
---|
104 | // If a value now exists in argument buffer, retrieve
|
---|
105 | // it. Remove the last character as it will be an erroneous
|
---|
106 | // space.
|
---|
107 | if(argument_value_buffer.length() > 0) {
|
---|
108 | argument_value = argument_value_buffer.substring(0, argument_value_buffer.length() - 1);
|
---|
109 | }
|
---|
110 |
|
---|
111 | // 3. We now have the argument name, and any associated
|
---|
112 | // value. We are ready to store the data in the
|
---|
113 | // appropriate variables.
|
---|
114 | Gatherer.println("Parsed Argument: name=" + argument_name + (argument_value != null ? (", value=" + argument_value) : ", no value"));
|
---|
115 | // 3a. First those arguments that have no associated value
|
---|
116 | if(argument_value == null) {
|
---|
117 | if(argument_name.equals(StaticStrings.HELP_ARGUMENT)) {
|
---|
118 | Gatherer.printUsage(dictionary);
|
---|
119 | System.exit(0);
|
---|
120 | }
|
---|
121 | // Run GLI in debug mode. Produces debug log plus extra
|
---|
122 | // messages.
|
---|
123 | else if(argument_name.equals(StaticStrings.DEBUG_ARGUMENT)) {
|
---|
124 | debug = true;
|
---|
125 | }
|
---|
126 | // Run GLI with feedback enabled.
|
---|
127 | else if(argument_name.equals(StaticStrings.FEEDBACK_ARGUMENT)) {
|
---|
128 | feedback_enabled = true;
|
---|
129 | }
|
---|
130 | // Forces no loading on previous collection.
|
---|
131 | else if(argument_name.equals(StaticStrings.NO_LOAD_ARGUMENT)) {
|
---|
132 | no_load = true;
|
---|
133 | filename = null;
|
---|
134 | }
|
---|
135 | // Run the GLI with mirroring enabled
|
---|
136 | else if(argument_name.equals(StaticStrings.MIRROR_ARGUMENT)) {
|
---|
137 | mirroring_enabled = true;
|
---|
138 | }
|
---|
139 | /* I've got a sneak suspicion Aqua look and feel is not free domain
|
---|
140 | // Specify the use of Greenstone LAF.
|
---|
141 | else if(argument_name.equals(StaticStrings.SKIN_ARGUMENT)) {
|
---|
142 | // SkinLF
|
---|
143 | try {
|
---|
144 | SkinLookAndFeel.setSkin(SkinLookAndFeel.loadThemePackDefinition(SkinUtils.toURL(new File(SKIN_DEFINITION_FILE))));
|
---|
145 | SkinLookAndFeel.enable();
|
---|
146 | }
|
---|
147 | catch (Exception error) {
|
---|
148 | ///ystem.err.println("Error: " + error);
|
---|
149 | error.printStackTrace();
|
---|
150 | }
|
---|
151 | }
|
---|
152 | */
|
---|
153 | }
|
---|
154 | // 3b. Now for those that do
|
---|
155 | else {
|
---|
156 | // Parse the path to the GSDL. Required argument.
|
---|
157 | if(argument_name.equals(StaticStrings.GSDL_ARGUMENT)) {
|
---|
158 | if(argument_value.endsWith(File.separator)) {
|
---|
159 | gsdl_path = argument_value;
|
---|
160 | }
|
---|
161 | else {
|
---|
162 | gsdl_path = argument_value + File.separator;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | // GSDL3 path
|
---|
166 | if(argument_name.equals(StaticStrings.GSDL3_ARGUMENT)) {
|
---|
167 | if(argument_value.endsWith(File.separator)) {
|
---|
168 | gsdl3_path = argument_value;
|
---|
169 | }
|
---|
170 | else {
|
---|
171 | gsdl3_path = argument_value + File.separator;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | else if (argument_name.equals(StaticStrings.SITE_ARGUMENT)) {
|
---|
175 | site_name = argument_value;
|
---|
176 | }
|
---|
177 | else if (argument_name.equals(StaticStrings.SERVLET_ARGUMENT)) {
|
---|
178 | servlet_path = argument_value;
|
---|
179 | }
|
---|
180 | // Specify a collection to load initially. Could be used for file associations.
|
---|
181 | else if(argument_name.equals(StaticStrings.LOAD_ARGUMENT)) {
|
---|
182 | filename = argument_value;
|
---|
183 | no_load = false;
|
---|
184 | }
|
---|
185 | // Parse the url to a running web server, or a file
|
---|
186 | // path to the local library server.
|
---|
187 | else if(argument_name.equals(StaticStrings.LIBRARY_ARGUMENT)) {
|
---|
188 | exec_path = argument_value;
|
---|
189 | // If there is no colon in first five characters of
|
---|
190 | // the exec_path (which would either be an existing
|
---|
191 | // protocol, or a windows file path to say a local
|
---|
192 | // library), we can append the protocol http://.
|
---|
193 | if(argument_value.lastIndexOf(StaticStrings.COLON_CHARACTER, 5) == -1) {
|
---|
194 | exec_path = StaticStrings.HTTP_PROTOCOL_STR + argument_value;
|
---|
195 | }
|
---|
196 | else {
|
---|
197 | exec_path = argument_value;
|
---|
198 | }
|
---|
199 | // If the user has given us an address, but it ends
|
---|
200 | // with a '/' we assume we're using the greenstone
|
---|
201 | // library.cgi
|
---|
202 | if(exec_path.startsWith(StaticStrings.HTTP_PROTOCOL_STR) && exec_path.endsWith(StaticStrings.URL_SEPARATOR_CHARACTER)) {
|
---|
203 | exec_path = exec_path + StaticStrings.LIBRARY_STR;
|
---|
204 | }
|
---|
205 | }
|
---|
206 | // Parse the path to PERL. If not provided its assumes
|
---|
207 | // perl should be availble on the PATH.
|
---|
208 | else if(argument_name.equals(StaticStrings.PERL_ARGUMENT)) {
|
---|
209 | perl_path = argument_value;
|
---|
210 | // Test whether this points to the Perl bin
|
---|
211 | // directory or the Perl executable itself.
|
---|
212 | File perl_file = new File(perl_path);
|
---|
213 | if(perl_file.isDirectory()) {
|
---|
214 | // If this is windows we create a child file
|
---|
215 | // perl.exe, otherwise we create perl
|
---|
216 | if(Utility.isWindows()) {
|
---|
217 | perl_file = new File(perl_file, Utility.PERL_EXECUTABLE_WINDOWS);
|
---|
218 | }
|
---|
219 | else {
|
---|
220 | perl_file = new File(perl_file, Utility.PERL_EXECUTABLE_UNIX);
|
---|
221 | }
|
---|
222 | // And store this new path.
|
---|
223 | perl_path = perl_file.getAbsolutePath();
|
---|
224 | perl_file = null;
|
---|
225 | }
|
---|
226 | // Otherwise its fine as it is
|
---|
227 | }
|
---|
228 | // Test for the presence of a WGet version. This is
|
---|
229 | // only useful if the user has enabled mirroring. Note
|
---|
230 | // that mirroring can be enabled by running the GLI
|
---|
231 | // with -mirror, editing the config.xml for GLI, or
|
---|
232 | // through a new option on the connections page of the
|
---|
233 | // preferences.
|
---|
234 | else if(argument_name.equals(StaticStrings.WGET_ARGUMENT)) {
|
---|
235 | if(argument_value.startsWith(StaticStrings.WGET_STR)) {
|
---|
236 | wget_version_str = StaticStrings.WGET_STR;
|
---|
237 | wget_path = argument_value.substring(StaticStrings.WGET_STR.length());
|
---|
238 | }
|
---|
239 | else if(argument_value.startsWith(StaticStrings.WGET_OLD_STR)) {
|
---|
240 | wget_version_str = StaticStrings.WGET_OLD_STR;
|
---|
241 | wget_path = argument_value.substring(StaticStrings.WGET_OLD_STR.length());
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|
246 | // Argument name was null, nothing to be done.
|
---|
247 | }
|
---|
248 | next_token = null;
|
---|
249 | // Arguments all parsed.
|
---|
250 | }
|
---|
251 | }
|
---|