View Javadoc
1 /*** 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Exoffice Technologies. For written permission, 18 * please contact info@exolab.org. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Exoffice Technologies. Exolab is a registered 23 * trademark of Exoffice Technologies. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: CommandLine.java,v 1.3 2003/08/07 13:33:12 tanderson Exp $ 44 * 45 * Date Author Changes 46 * 1/6/2000 jima Created 47 * 1/9/2000 jima changed package name from com.comware to org.exolab 48 * 7/1/2000 jima removed the CW prefix from the class name 49 * 1/8/2001 jima Removed it from jtf library and imported it into 50 * the openjms library 51 */ 52 53 package org.exolab.jms.util; 54 55 import java.util.Hashtable; 56 import java.util.Vector; 57 58 59 /*** 60 * This core class is responsible for processing the command line and 61 * storing away the list of options and parameters specified. The 62 * difference between an option and a command line is that an option 63 * is a boolean value (true if it is specified and false otherwise) 64 * and a parameter always has an associated value. 65 * 66 * @version $version$ 67 * @author jima 68 **/ 69 public class CommandLine { 70 71 /*** 72 * A list of option of switches on the command line. A switch 73 * is either set or not 74 */ 75 private Vector _switches = new Vector(); 76 77 /*** 78 * A dictionary of all the options and their associated values 79 */ 80 private Hashtable _options = new Hashtable(); 81 82 /*** 83 * Construct an instance of this class with the specified string 84 * array. 85 * 86 * @param args command line argument 87 */ 88 public CommandLine(String[] args) { 89 processCommandLine(args); 90 } 91 92 /*** 93 * Default constructor which simply initialised the class 94 */ 95 public CommandLine() { 96 } 97 98 /*** 99 * Check if the following option or command has been specified 100 * 101 * @param name name of option or command 102 * @return boolean true if it has been specified 103 */ 104 public boolean exists(String name) { 105 return _switches.contains(name) || _options.containsKey(name); 106 } 107 108 /*** 109 * Check if the following option has been specified. 110 * 111 * @param name name of the option 112 * @return boolean true if it has been specified 113 */ 114 public boolean isSwitch(String name) { 115 return _switches.contains(name); 116 } 117 118 /*** 119 * Check if the following parameter has been specified. 120 * 121 * @param name name of the parameter 122 * @return boolean true if it has been specified 123 */ 124 public boolean isParameter(String name) { 125 return _options.containsKey(name); 126 } 127 128 /*** 129 * Return the value of the parameter or option. If the string nominates 130 * an option then return null 131 * 132 * @param name name of option or parameter 133 * @return String value of parameter or null 134 */ 135 public String value(String name) { 136 String result = null; 137 138 if (_options.containsKey(name)) { 139 result = (String) _options.get(name); 140 } 141 142 return result; 143 } 144 145 /*** 146 * Return the value of the parameter or option, returning a default 147 * value if none is specified 148 * 149 * @param name name of option or parameter 150 * @param defaultValue the default value 151 * @return String value of parameter 152 */ 153 public String value(String name, String defaultValue) { 154 String result = value(name); 155 return (result != null) ? result : defaultValue; 156 } 157 158 /*** 159 * Add the following option or parameter to the list. An option will 160 * have a null value, whereas a parameter will have a non-null value. 161 * <p> 162 * This will automatically overwrite the previous value, if one has been 163 * specified. 164 * 165 * @param name name of option or parameter 166 * @param value value of name 167 * @return boolean true if it was successfully added 168 */ 169 public boolean add(String name, String value) { 170 return add(name, value, true); 171 } 172 173 /*** 174 * Add the following option or parameter to the list. An option will 175 * have a null value, whereas a parameter will have a non-null value. 176 * <p> 177 * If the overwrite flag is true then this value will overwrite the 178 * previous value. If the overwrite flag is false and the name already 179 * exists then it will not overwrite it and the function will return 180 * false. In all other circumstances it will return true. 181 * 182 * @param name name of option or parameter 183 * @param value value of name 184 * @param overwrite true to overwrite previous value 185 * @return boolean true if it was successfully added 186 */ 187 public boolean add(String name, String value, boolean overwrite) { 188 boolean result = false; 189 190 if (value == null) { 191 // it is an option 192 if ((_switches.contains(name)) && 193 (overwrite)) { 194 _switches.addElement(name); 195 result = true; 196 } else if (!_switches.contains(name)) { 197 _switches.addElement(name); 198 result = true; 199 } 200 } else { 201 // parameter 202 if ((_options.containsKey(name)) && 203 (overwrite)) { 204 _options.put(name, value); 205 result = true; 206 } else if (!_options.containsKey(name)) { 207 _options.put(name, value); 208 result = true; 209 } 210 } 211 212 return result; 213 } 214 215 /*** 216 * This method processes the command line and extracts the list of 217 * options and command lines. It doesn't intepret the meaning of the 218 * entities, which is left to the application. 219 * 220 * @param args command line as a collection of tokens 221 */ 222 private void processCommandLine(String[] args) { 223 boolean prev_was_hyphen = false; 224 String prev_key = null; 225 226 for (int index = 0; index < args.length; index++) { 227 if (args[index].startsWith("-")) { 228 // if the previous string started with a hyphen then 229 // it was an option store store it, without the hyphen 230 // in the _switches vector. Otherwise if the previous was 231 // not a hyphen then store key and value in the _options 232 // hashtable 233 if (prev_was_hyphen) { 234 add(prev_key, null); 235 } 236 237 prev_key = args[index].substring(1); 238 prev_was_hyphen = true; 239 240 // check to see whether it is the last element in the 241 // arg list. If it is then assume it is an option and 242 // break the processing 243 if (index == args.length - 1) { 244 add(prev_key, null); 245 break; 246 } 247 } else { 248 // it does not start with a hyphen. If the prev_key is 249 // not null then set the value to the prev_value. 250 if (prev_key != null) { 251 add(prev_key, args[index]); 252 prev_key = null; 253 } 254 prev_was_hyphen = false; 255 } 256 } 257 } 258 259 }

This page was automatically generated by Maven