/** *######################################################################### * * A component of the Gatherer application, part of the Greenstone digital * library suite from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * * Author: John Thompson and David Bainbridge, * Greenstone Digital Library, University of Waikato * * Copyright (C) 1999 New Zealand Digital Library Project * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *######################################################################## */ package org.greenstone.gatherer; import java.io.*; import org.greenstone.gatherer.util.StaticStrings; import org.greenstone.gatherer.util.Utility; public class GetOpt { public boolean debug = false; public boolean feedback_enabled = false; public boolean mirroring_enabled = false; public boolean no_load = false; public String exec_path = null; public String extra = null; public String filename = null; public String gsdl_path = null; public String gsdl3_path = null; public String perl_path = null; public String site_name = null; // for GS3 public String servlet_path = null; public String wget_path = null; public String wget_version_str = StaticStrings.NO_WGET_STR; public GetOpt(String[] args) { // Default dictionary. Only used for starting error messages. Dictionary dictionary = new Dictionary(null, null); // Parse arguments int argument_index = 0; String next_token = null; while(argument_index < args.length || next_token != null) { // 1. We start by attempting to parse an argument name. An argument // must start with a '-', and should not contain spaces. If // anything else is encountered it is ignored. String argument_name = null; if(next_token == null) { next_token = args[argument_index]; argument_index++; } if(next_token.startsWith(StaticStrings.MINUS_CHARACTER)) { // Trim second '-' just to be kind to Unixy-type people if(next_token.startsWith(StaticStrings.MINUS_CHARACTER + StaticStrings.MINUS_CHARACTER)) { argument_name = next_token.substring(1); } else { argument_name = next_token; } } next_token = null; // 2. If we now have an argument name we continue by attempting // to parse a value. A value is taken to be the sequence of // space seperated Strings between the last argument name // and up to but not including the next argument name. Of // course an argument needn't have any value (ie -debug, // -help), in which case value will be null. if(argument_name != null) { String argument_value = null; StringBuffer argument_value_buffer = new StringBuffer(""); while(argument_index < args.length && next_token == null) { next_token = args[argument_index]; argument_index++; // If we just parsed an arbitary String then append it to value, followed by a single space if(!next_token.startsWith(StaticStrings.MINUS_CHARACTER)) { argument_value_buffer.append(next_token); argument_value_buffer.append(StaticStrings.SPACE_CHARACTER); next_token = null; } // If the argument token retrieved is an argument name, // then leave it in next_token, which will cause the // argument parsing process to move onto the next step. } // If a value now exists in argument buffer, retrieve // it. Remove the last character as it will be an erroneous // space. if(argument_value_buffer.length() > 0) { argument_value = argument_value_buffer.substring(0, argument_value_buffer.length() - 1); } // 3. We now have the argument name, and any associated // value. We are ready to store the data in the // appropriate variables. DebugStream.println("Parsed Argument: name=" + argument_name + (argument_value != null ? (", value=" + argument_value) : ", no value")); // 3a. First those arguments that have no associated value if(argument_value == null) { if(argument_name.equals(StaticStrings.HELP_ARGUMENT)) { System.out.println(Dictionary.get("General.Usage")); System.exit(0); } // Run GLI in debug mode. Produces debug log plus extra // messages. else if(argument_name.equals(StaticStrings.DEBUG_ARGUMENT)) { debug = true; } // Run GLI with feedback enabled. else if(argument_name.equals(StaticStrings.FEEDBACK_ARGUMENT)) { feedback_enabled = true; } // Forces no loading on previous collection. else if(argument_name.equals(StaticStrings.NO_LOAD_ARGUMENT)) { no_load = true; filename = null; } // Run the GLI with mirroring enabled else if(argument_name.equals(StaticStrings.MIRROR_ARGUMENT)) { mirroring_enabled = true; } /* I've got a sneak suspicion Aqua look and feel is not free domain // Specify the use of Greenstone LAF. else if(argument_name.equals(StaticStrings.SKIN_ARGUMENT)) { // SkinLF try { SkinLookAndFeel.setSkin(SkinLookAndFeel.loadThemePackDefinition(SkinUtils.toURL(new File(SKIN_DEFINITION_FILE)))); SkinLookAndFeel.enable(); } catch (Exception error) { ///ystem.err.println("Error: " + error); error.printStackTrace(); } } */ } // 3b. Now for those that do else { // Parse the path to the GSDL. Required argument. if(argument_name.equals(StaticStrings.GSDL_ARGUMENT)) { if(argument_value.endsWith(File.separator)) { gsdl_path = argument_value; } else { gsdl_path = argument_value + File.separator; } } // GSDL3 path if(argument_name.equals(StaticStrings.GSDL3_ARGUMENT)) { if(argument_value.endsWith(File.separator)) { gsdl3_path = argument_value; } else { gsdl3_path = argument_value + File.separator; } } else if (argument_name.equals(StaticStrings.SITE_ARGUMENT)) { site_name = argument_value; } else if (argument_name.equals(StaticStrings.SERVLET_ARGUMENT)) { servlet_path = argument_value; } // Specify a collection to load initially. Could be used for file associations. else if(argument_name.equals(StaticStrings.LOAD_ARGUMENT)) { filename = argument_value; no_load = false; } // Parse the url to a running web server, or a file // path to the local library server. else if(argument_name.equals(StaticStrings.LIBRARY_ARGUMENT)) { exec_path = argument_value; // If there is no colon in first five characters of // the exec_path (which would either be an existing // protocol, or a windows file path to say a local // library), we can append the protocol http://. if(argument_value.lastIndexOf(StaticStrings.COLON_CHARACTER, 5) == -1) { exec_path = StaticStrings.HTTP_PROTOCOL_STR + argument_value; } else { exec_path = argument_value; } // If the user has given us an address, but it ends // with a '/' we assume we're using the greenstone // library.cgi if(exec_path.startsWith(StaticStrings.HTTP_PROTOCOL_STR) && exec_path.endsWith(StaticStrings.URL_SEPARATOR_CHARACTER)) { exec_path = exec_path + StaticStrings.LIBRARY_STR; } } // Parse the path to PERL. If not provided its assumes // perl should be availble on the PATH. else if(argument_name.equals(StaticStrings.PERL_ARGUMENT)) { perl_path = argument_value; // Test whether this points to the Perl bin // directory or the Perl executable itself. File perl_file = new File(perl_path); if(perl_file.isDirectory()) { // If this is windows we create a child file // perl.exe, otherwise we create perl if(Utility.isWindows()) { perl_file = new File(perl_file, Utility.PERL_EXECUTABLE_WINDOWS); } else { perl_file = new File(perl_file, Utility.PERL_EXECUTABLE_UNIX); } // And store this new path. perl_path = perl_file.getAbsolutePath(); perl_file = null; } // Otherwise its fine as it is } // Test for the presence of a WGet version. This is // only useful if the user has enabled mirroring. Note // that mirroring can be enabled by running the GLI // with -mirror, editing the config.xml for GLI, or // through a new option on the connections page of the // preferences. else if(argument_name.equals(StaticStrings.WGET_ARGUMENT)) { if(argument_value.startsWith(StaticStrings.WGET_STR)) { wget_version_str = StaticStrings.WGET_STR; wget_path = argument_value.substring(StaticStrings.WGET_STR.length()); } else if(argument_value.startsWith(StaticStrings.WGET_OLD_STR)) { wget_version_str = StaticStrings.WGET_OLD_STR; wget_path = argument_value.substring(StaticStrings.WGET_OLD_STR.length()); } } } } // Argument name was null, nothing to be done. } next_token = null; // Arguments all parsed. } }