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