[7738] | 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 | {
|
---|
[7927] | 36 | public boolean debug = false;
|
---|
[7738] | 37 | public boolean feedback_enabled = false;
|
---|
| 38 | public boolean no_load = false;
|
---|
[10540] | 39 | public boolean use_remote_greenstone = false;
|
---|
[12552] | 40 | public boolean new_set = false;
|
---|
[7738] | 41 |
|
---|
[13014] | 42 | public String client_operating_system = null;
|
---|
[9055] | 43 | public String collect_directory_path = null;
|
---|
[7738] | 44 | public String filename = null;
|
---|
[10367] | 45 | public String gliserver_url_string = null;
|
---|
[7738] | 46 | public String gsdl_path = null;
|
---|
| 47 | public String gsdl3_path = null;
|
---|
[10330] | 48 | public String gsdl3_src_path = null;
|
---|
[8629] | 49 | public String library_url_string = null;
|
---|
[8618] | 50 | public String local_library_path = null;
|
---|
[7738] | 51 | public String perl_path = null;
|
---|
| 52 | public String site_name = null; // for GS3
|
---|
| 53 | public String servlet_path = null;
|
---|
[12552] | 54 | public String metadata_path = null;
|
---|
[7738] | 55 |
|
---|
[8229] | 56 | public GetOpt(String[] args)
|
---|
[7738] | 57 | {
|
---|
| 58 | // Default dictionary. Only used for starting error messages.
|
---|
| 59 | Dictionary dictionary = new Dictionary(null, null);
|
---|
| 60 |
|
---|
| 61 | // Parse arguments
|
---|
| 62 | int argument_index = 0;
|
---|
| 63 | String next_token = null;
|
---|
| 64 |
|
---|
| 65 | while(argument_index < args.length || next_token != null) {
|
---|
| 66 | // 1. We start by attempting to parse an argument name. An argument
|
---|
| 67 | // must start with a '-', and should not contain spaces. If
|
---|
| 68 | // anything else is encountered it is ignored.
|
---|
| 69 |
|
---|
| 70 | String argument_name = null;
|
---|
| 71 | if(next_token == null) {
|
---|
| 72 | next_token = args[argument_index];
|
---|
| 73 | argument_index++;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | if(next_token.startsWith(StaticStrings.MINUS_CHARACTER)) {
|
---|
| 77 | // Trim second '-' just to be kind to Unixy-type people
|
---|
| 78 | if(next_token.startsWith(StaticStrings.MINUS_CHARACTER + StaticStrings.MINUS_CHARACTER)) {
|
---|
| 79 | argument_name = next_token.substring(1);
|
---|
| 80 | }
|
---|
| 81 | else {
|
---|
| 82 | argument_name = next_token;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | next_token = null;
|
---|
| 86 | // 2. If we now have an argument name we continue by attempting
|
---|
| 87 | // to parse a value. A value is taken to be the sequence of
|
---|
| 88 | // space seperated Strings between the last argument name
|
---|
| 89 | // and up to but not including the next argument name. Of
|
---|
| 90 | // course an argument needn't have any value (ie -debug,
|
---|
| 91 | // -help), in which case value will be null.
|
---|
| 92 | if(argument_name != null) {
|
---|
| 93 | String argument_value = null;
|
---|
| 94 | StringBuffer argument_value_buffer = new StringBuffer("");
|
---|
| 95 | while(argument_index < args.length && next_token == null) {
|
---|
| 96 | next_token = args[argument_index];
|
---|
| 97 | argument_index++;
|
---|
| 98 | // If we just parsed an arbitary String then append it to value, followed by a single space
|
---|
| 99 | if(!next_token.startsWith(StaticStrings.MINUS_CHARACTER)) {
|
---|
| 100 | argument_value_buffer.append(next_token);
|
---|
| 101 | argument_value_buffer.append(StaticStrings.SPACE_CHARACTER);
|
---|
| 102 | next_token = null;
|
---|
| 103 | }
|
---|
| 104 | // If the argument token retrieved is an argument name,
|
---|
| 105 | // then leave it in next_token, which will cause the
|
---|
| 106 | // argument parsing process to move onto the next step.
|
---|
| 107 | }
|
---|
| 108 | // If a value now exists in argument buffer, retrieve
|
---|
| 109 | // it. Remove the last character as it will be an erroneous
|
---|
| 110 | // space.
|
---|
| 111 | if(argument_value_buffer.length() > 0) {
|
---|
| 112 | argument_value = argument_value_buffer.substring(0, argument_value_buffer.length() - 1);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | // 3. We now have the argument name, and any associated
|
---|
| 116 | // value. We are ready to store the data in the
|
---|
| 117 | // appropriate variables.
|
---|
[8236] | 118 | DebugStream.println("Parsed Argument: name=" + argument_name + (argument_value != null ? (", value=" + argument_value) : ", no value"));
|
---|
[7738] | 119 | // 3a. First those arguments that have no associated value
|
---|
| 120 | if(argument_value == null) {
|
---|
| 121 | if(argument_name.equals(StaticStrings.HELP_ARGUMENT)) {
|
---|
[8236] | 122 | System.out.println(Dictionary.get("General.Usage"));
|
---|
[7738] | 123 | System.exit(0);
|
---|
| 124 | }
|
---|
| 125 | // Run GLI in debug mode. Produces debug log plus extra
|
---|
| 126 | // messages.
|
---|
| 127 | else if(argument_name.equals(StaticStrings.DEBUG_ARGUMENT)) {
|
---|
| 128 | debug = true;
|
---|
| 129 | }
|
---|
| 130 | // Run GLI with feedback enabled.
|
---|
| 131 | else if(argument_name.equals(StaticStrings.FEEDBACK_ARGUMENT)) {
|
---|
| 132 | feedback_enabled = true;
|
---|
| 133 | }
|
---|
| 134 | // Forces no loading on previous collection.
|
---|
| 135 | else if(argument_name.equals(StaticStrings.NO_LOAD_ARGUMENT)) {
|
---|
| 136 | no_load = true;
|
---|
| 137 | filename = null;
|
---|
| 138 | }
|
---|
[10540] | 139 | // Use a remote Greenstone rather than a local one
|
---|
| 140 | else if (argument_name.equals(StaticStrings.USE_REMOTE_GREENSTONE_ARGUMENT)) {
|
---|
| 141 | use_remote_greenstone = true;
|
---|
| 142 | }
|
---|
[12552] | 143 | else if (argument_name.equals(StaticStrings.NEW_METADATASET)) {
|
---|
| 144 | new_set = true;
|
---|
| 145 | }
|
---|
[7738] | 146 | }
|
---|
| 147 | // 3b. Now for those that do
|
---|
| 148 | else {
|
---|
| 149 | // Parse the path to the GSDL. Required argument.
|
---|
| 150 | if(argument_name.equals(StaticStrings.GSDL_ARGUMENT)) {
|
---|
| 151 | if(argument_value.endsWith(File.separator)) {
|
---|
| 152 | gsdl_path = argument_value;
|
---|
| 153 | }
|
---|
| 154 | else {
|
---|
| 155 | gsdl_path = argument_value + File.separator;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | // GSDL3 path
|
---|
| 159 | if(argument_name.equals(StaticStrings.GSDL3_ARGUMENT)) {
|
---|
| 160 | if(argument_value.endsWith(File.separator)) {
|
---|
| 161 | gsdl3_path = argument_value;
|
---|
| 162 | }
|
---|
| 163 | else {
|
---|
| 164 | gsdl3_path = argument_value + File.separator;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
[10330] | 167 | // GSDL3 src path
|
---|
| 168 | if(argument_name.equals(StaticStrings.GSDL3_SRC_ARGUMENT)) {
|
---|
| 169 | if(argument_value.endsWith(File.separator)) {
|
---|
| 170 | gsdl3_src_path = argument_value;
|
---|
| 171 | }
|
---|
| 172 | else {
|
---|
| 173 | gsdl3_src_path = argument_value + File.separator;
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
[13014] | 176 | // Client operating system
|
---|
| 177 | else if (argument_name.equals(StaticStrings.GSDLOS_ARGUMENT)) {
|
---|
| 178 | client_operating_system = argument_value;
|
---|
| 179 | }
|
---|
[10330] | 180 |
|
---|
[7738] | 181 | else if (argument_name.equals(StaticStrings.SITE_ARGUMENT)) {
|
---|
| 182 | site_name = argument_value;
|
---|
| 183 | }
|
---|
| 184 | else if (argument_name.equals(StaticStrings.SERVLET_ARGUMENT)) {
|
---|
| 185 | servlet_path = argument_value;
|
---|
| 186 | }
|
---|
[9055] | 187 | // Specify a non-standard collect directory to use (for running one GLI in a network environment)
|
---|
| 188 | else if (argument_name.equals(StaticStrings.COLLECTDIR_ARGUMENT)) {
|
---|
| 189 | collect_directory_path = argument_value;
|
---|
| 190 | System.err.println("Non standard collect directory specified: " + collect_directory_path);
|
---|
| 191 | }
|
---|
[7738] | 192 | // Specify a collection to load initially. Could be used for file associations.
|
---|
| 193 | else if(argument_name.equals(StaticStrings.LOAD_ARGUMENT)) {
|
---|
| 194 | filename = argument_value;
|
---|
| 195 | no_load = false;
|
---|
| 196 | }
|
---|
[8618] | 197 | // Parse the file path of the local library server
|
---|
| 198 | else if (argument_name.equals(StaticStrings.LOCAL_LIBRARY_ARGUMENT)) {
|
---|
[8620] | 199 | local_library_path = argument_value;
|
---|
[7738] | 200 | }
|
---|
[8625] | 201 | // Manually specify the Greenstone library URL
|
---|
| 202 | else if (argument_name.equals(StaticStrings.LIBRARY_URL_ARGUMENT)) {
|
---|
[8629] | 203 | library_url_string = argument_value;
|
---|
[8625] | 204 | }
|
---|
[10367] | 205 | // Specify the URL to the gliserver CGI script for remote collection building
|
---|
| 206 | else if (argument_name.equals(StaticStrings.GLISERVER_URL_ARGUMENT)) {
|
---|
| 207 | gliserver_url_string = argument_value;
|
---|
| 208 | }
|
---|
[9217] | 209 | // Parse the path to PERL. If not provided it assumes
|
---|
[7738] | 210 | // perl should be availble on the PATH.
|
---|
| 211 | else if(argument_name.equals(StaticStrings.PERL_ARGUMENT)) {
|
---|
| 212 | perl_path = argument_value;
|
---|
| 213 | // Test whether this points to the Perl bin
|
---|
| 214 | // directory or the Perl executable itself.
|
---|
| 215 | File perl_file = new File(perl_path);
|
---|
| 216 | if(perl_file.isDirectory()) {
|
---|
| 217 | // If this is windows we create a child file
|
---|
| 218 | // perl.exe, otherwise we create perl
|
---|
| 219 | if(Utility.isWindows()) {
|
---|
| 220 | perl_file = new File(perl_file, Utility.PERL_EXECUTABLE_WINDOWS);
|
---|
| 221 | }
|
---|
| 222 | else {
|
---|
| 223 | perl_file = new File(perl_file, Utility.PERL_EXECUTABLE_UNIX);
|
---|
| 224 | }
|
---|
| 225 | // And store this new path.
|
---|
| 226 | perl_path = perl_file.getAbsolutePath();
|
---|
| 227 | perl_file = null;
|
---|
| 228 | }
|
---|
[9217] | 229 | // Otherwise it is fine as it is
|
---|
[7738] | 230 | }
|
---|
[12552] | 231 | else if(argument_name.equals(StaticStrings.METADATA_PATH)){
|
---|
| 232 | if (argument_value != null && !argument_value.equals("")) {
|
---|
| 233 | File metadata_file = new File(argument_value);
|
---|
| 234 | if (metadata_file.exists()) {
|
---|
| 235 | metadata_path = argument_value;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[7738] | 240 | }
|
---|
| 241 | }
|
---|
| 242 | // Argument name was null, nothing to be done.
|
---|
| 243 | }
|
---|
| 244 | next_token = null;
|
---|
| 245 | // Arguments all parsed.
|
---|
| 246 | }
|
---|
| 247 | }
|
---|