source: main/trunk/gli/src/org/greenstone/gatherer/GetOpt.java@ 36272

Last change on this file since 36272 was 36091, checked in by kjdon, 2 years ago

check that servlet arg starts with /, and add one if it doesn't

  • Property svn:keywords set to Author Date Id Revision
File size: 11.8 KB
Line 
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 */
28package org.greenstone.gatherer;
29
30import java.io.*;
31import org.greenstone.gatherer.util.StaticStrings;
32import org.greenstone.gatherer.util.Utility;
33
34public class GetOpt
35{
36 public boolean testing_mode = false; // true if running GLI for tutorial testing: want names assigned to GUI components
37
38 public boolean debug = false;
39 public boolean feedback_enabled = false;
40 public boolean no_load = false;
41 public boolean use_remote_greenstone = false;
42 public boolean new_set = false;
43 public boolean run_gsdl3 = false;
44
45 public String client_operating_system = null;
46 public String collect_directory_path = null;
47 public String filename = null;
48 public String gliserver_url_string = null;
49 public String gsdl_path = null;
50 public String gsdl3_path = null;
51 public String gsdl3_src_path = null;
52 public String library_url_string = null;
53 public String local_library_path = null;
54 public String perl_path = null;
55 public String site_name = null; // for GS3
56 public String servlet_path = null;
57 public String metadata_path = null;
58
59 protected FedoraInfo fedora_info = null;
60
61 public GetOpt(String[] args)
62 {
63 // Default dictionary. Only used for starting error messages.
64 Dictionary dictionary = new Dictionary(null, null);
65
66 fedora_info = new FedoraInfo();
67
68 // Parse arguments
69 int argument_index = 0;
70 String next_token = null;
71
72 while(argument_index < args.length || next_token != null) {
73 // 1. We start by attempting to parse an argument name. An argument
74 // must start with a '-', and should not contain spaces. If
75 // anything else is encountered it is ignored.
76
77 String argument_name = null;
78 if(next_token == null) {
79 next_token = args[argument_index];
80 argument_index++;
81 }
82
83 if(next_token.startsWith(StaticStrings.MINUS_CHARACTER)) {
84 // Trim second '-' just to be kind to Unixy-type people
85 if(next_token.startsWith(StaticStrings.MINUS_CHARACTER + StaticStrings.MINUS_CHARACTER)) {
86 argument_name = next_token.substring(1);
87 }
88 else {
89 argument_name = next_token;
90 }
91 }
92 next_token = null;
93 // 2. If we now have an argument name we continue by attempting
94 // to parse a value. A value is taken to be the sequence of
95 // space seperated Strings between the last argument name
96 // and up to but not including the next argument name. Of
97 // course an argument needn't have any value (ie -debug,
98 // -help), in which case value will be null.
99 if(argument_name != null) {
100 String argument_value = null;
101 StringBuffer argument_value_buffer = new StringBuffer("");
102 while(argument_index < args.length && next_token == null) {
103 next_token = args[argument_index];
104 argument_index++;
105 // If we just parsed an arbitary String then append it to value, followed by a single space
106 if(!next_token.startsWith(StaticStrings.MINUS_CHARACTER)) {
107 argument_value_buffer.append(next_token);
108 argument_value_buffer.append(StaticStrings.SPACE_CHARACTER);
109 next_token = null;
110 }
111 // If the argument token retrieved is an argument name,
112 // then leave it in next_token, which will cause the
113 // argument parsing process to move onto the next step.
114 }
115 // If a value now exists in argument buffer, retrieve
116 // it. Remove the last character as it will be an erroneous
117 // space.
118 if(argument_value_buffer.length() > 0) {
119 argument_value = argument_value_buffer.substring(0, argument_value_buffer.length() - 1);
120 }
121
122 // 3. We now have the argument name, and any associated
123 // value. We are ready to store the data in the
124 // appropriate variables.
125 DebugStream.println("Parsed Argument: name=" + argument_name + (argument_value != null ? (", value=" + argument_value) : ", no value"));
126 // 3a. First those arguments that have no associated value
127 if(argument_value == null) {
128 if(argument_name.equals(StaticStrings.HELP_ARGUMENT)) {
129 System.out.println(Dictionary.get("General.Usage"));
130 System.exit(0);
131 }
132 // Run GLI in testing mode. Will assign names to GUI Components
133 else if(argument_name.equals(StaticStrings.TESTING_ARGUMENT)) {
134 testing_mode = true;
135 }
136 // Run GLI in debug mode. Produces debug log plus extra
137 // messages.
138 else if(argument_name.equals(StaticStrings.DEBUG_ARGUMENT)) {
139 debug = true;
140 }
141 // Run GLI with feedback enabled.
142 else if(argument_name.equals(StaticStrings.FEEDBACK_ARGUMENT)) {
143 feedback_enabled = true;
144 }
145 // Forces no loading on previous collection.
146 else if(argument_name.equals(StaticStrings.NO_LOAD_ARGUMENT)) {
147 no_load = true;
148 filename = null;
149 }
150 // Use a remote Greenstone rather than a local one
151 else if (argument_name.equals(StaticStrings.USE_REMOTE_GREENSTONE_ARGUMENT)) {
152 use_remote_greenstone = true;
153 //Use a remote Greenstone
154 }
155 else if (argument_name.equals(StaticStrings.GSDL3_ARGUMENT)){
156 //Use a remote Greenstone3
157 run_gsdl3=true;
158 }
159 else if (argument_name.equals(StaticStrings.NEW_METADATASET)) {
160 new_set = true;
161 }
162 else if(argument_name.equals(StaticStrings.FEDORA_MODE)) {
163 // Running FLI remotely
164 fedora_info.setActive(true);
165 }
166 }
167 // 3b. Now for those that do
168 else {
169 // Parse the path to the GSDL. Required argument.
170 if(argument_name.equals(StaticStrings.GSDL_ARGUMENT)) {
171 if(argument_value.endsWith(File.separator)) {
172 gsdl_path = argument_value;
173 }
174 else {
175 gsdl_path = argument_value + File.separator;
176 }
177 }
178 // GSDL3 path
179 if(argument_name.equals(StaticStrings.GSDL3_ARGUMENT)) {
180 if(argument_value.endsWith(File.separator)) {
181 gsdl3_path = argument_value;
182 }
183 else {
184 gsdl3_path = argument_value + File.separator;
185 }
186 }
187 // GSDL3 src path
188 if(argument_name.equals(StaticStrings.GSDL3_SRC_ARGUMENT)) {
189 if(argument_value.endsWith(File.separator)) {
190 gsdl3_src_path = argument_value;
191 }
192 else {
193 gsdl3_src_path = argument_value + File.separator;
194 }
195 }
196 // Client operating system
197 else if (argument_name.equals(StaticStrings.GSDLOS_ARGUMENT)) {
198 client_operating_system = argument_value;
199 }
200
201 else if (argument_name.equals(StaticStrings.SITE_ARGUMENT)) {
202 site_name = argument_value;
203 }
204 else if (argument_name.equals(StaticStrings.SERVLET_ARGUMENT)) {
205 if (argument_value.startsWith(StaticStrings.URL_SEPARATOR_CHARACTER)) {
206 servlet_path = argument_value;
207 } else {
208 servlet_path = StaticStrings.URL_SEPARATOR_CHARACTER + argument_value;
209 }
210 }
211 // Specify a non-standard collect directory to use (for running one GLI in a network environment)
212 else if (argument_name.equals(StaticStrings.COLLECTDIR_ARGUMENT)) {
213 collect_directory_path = argument_value;
214 System.err.println("Non standard collect directory specified: " + collect_directory_path);
215 }
216 // Specify a collection to load initially. Could be used for file associations.
217 else if(argument_name.equals(StaticStrings.LOAD_ARGUMENT)) {
218 filename = argument_value;
219 no_load = false;
220 }
221 // Parse the file path of the local library server
222 else if (argument_name.equals(StaticStrings.LOCAL_LIBRARY_ARGUMENT)) {
223 local_library_path = argument_value;
224 }
225 // Manually specify the Greenstone library URL
226 else if (argument_name.equals(StaticStrings.LIBRARY_URL_ARGUMENT)) {
227 library_url_string = argument_value;
228 }
229 // Specify the URL to the gliserver CGI script for remote collection building
230 else if (argument_name.equals(StaticStrings.GLISERVER_URL_ARGUMENT)) {
231 gliserver_url_string = argument_value;
232 }
233 // Parse the path to PERL. If not provided it assumes
234 // perl should be available on the PATH.
235 else if(argument_name.equals(StaticStrings.PERL_ARGUMENT)) {
236 perl_path = argument_value;
237 // Test whether this points to the Perl bin
238 // directory or the Perl executable itself.
239 File perl_file = new File(perl_path);
240 if(perl_file.isDirectory()) {
241 // If this is windows we create a child file
242 // perl.exe, otherwise we create perl
243 if(Utility.isWindows()) {
244 perl_file = new File(perl_file, Utility.PERL_EXECUTABLE_WINDOWS);
245 }
246 else {
247 perl_file = new File(perl_file, Utility.PERL_EXECUTABLE_UNIX);
248 }
249 // And store this new path.
250 perl_path = perl_file.getAbsolutePath();
251 perl_file = null;
252 }
253 // Otherwise it is fine as it is
254 }
255 else if(argument_name.equals(StaticStrings.METADATA_PATH)){
256 if (argument_value != null && !argument_value.equals("")) {
257 File metadata_file = new File(argument_value);
258 if (metadata_file.exists()) {
259 metadata_path = argument_value;
260 }
261 }
262 }
263
264 // Fedora home - when running Fedora locally
265 if(argument_name.equals(StaticStrings.FEDORA_HOME)) {
266 if(argument_value.endsWith(File.separator)) {
267 fedora_info.setHome(argument_value);
268 }
269 else {
270 fedora_info.setHome(argument_value + File.separator);
271 }
272 }
273
274 if(argument_name.equals(StaticStrings.FEDORA_VERSION)) {
275 fedora_info.setVersion(argument_value);
276 }
277
278 // Fedora hostname
279 if(argument_name.equals(StaticStrings.FEDORA_HOSTNAME)) {
280 if(argument_value.endsWith(File.separator)) {
281 fedora_info.setHostname(argument_value);
282 }
283 else {
284 fedora_info.setHostname(argument_value + File.separator);
285 }
286 }
287 // Fedora port
288 if(argument_name.equals(StaticStrings.FEDORA_PORT)) {
289 if(argument_value.endsWith(File.separator)) {
290 fedora_info.setPort(argument_value);
291 }
292 else {
293 fedora_info.setPort(argument_value + File.separator);
294 }
295 }
296 // Fedora username
297 if(argument_name.equals(StaticStrings.FEDORA_USERNAME)) {
298 if(argument_value.endsWith(File.separator)) {
299 fedora_info.setUsername(argument_value);
300 }
301 else {
302 fedora_info.setUsername(argument_value + File.separator);
303 }
304 }
305 // Fedora password
306 if(argument_name.equals(StaticStrings.FEDORA_PASSWORD)) {
307 if(argument_value.endsWith(File.separator)) {
308 fedora_info.setPassword(argument_value);
309 }
310 else {
311 fedora_info.setPassword(argument_value + File.separator);
312 }
313 }
314 // Fedora protocol, e.g. http or https
315 if(argument_name.equals(StaticStrings.FEDORA_PROTOCOL)) {
316 if(argument_value.endsWith(File.separator)) {
317 fedora_info.setProtocol(argument_value);
318 }
319 else {
320 fedora_info.setProtocol(argument_value + File.separator);
321 }
322 }
323
324
325
326
327
328 }
329 }
330 // Argument name was null, nothing to be done.
331 }
332 next_token = null;
333 // Arguments all parsed.
334 }
335}
Note: See TracBrowser for help on using the repository browser.