Source code for /engineering/autohit-2003/src/autohit/common/CommandLine.javaOriginal file CommandLine.java
   1 /**
   2  * AUTOHIT 2003
   3  * Copyright Erich P Gatejen (c) 1989,1997,2003,2004
   4  * 
   5  * This program is free software; you can redistribute it and/or modify 
   6  * it under the terms of the GNU General Public License as published by 
   7  * the Free Software Foundation; either version 2 of the License, or (at
   8  * your option) any later version.
   9  * This program is distributed in the hope that it will be useful, but
  10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12  * more details.
  13  * 
  14  * You should have received a copy of the GNU General Public License along
  15  * with this program; if not, write to the Free Software Foundation, Inc.,
  16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17  *
  18  * Additional license information can be found in the documentation.
  19  * @author Erich P Gatejen
  20  */
  21 package autohit.common;
  22 
  23 /**
  24  * Manage a command line.  It will return sections with calls to
  25  * get().  Each section is delimited with whitespace, though a full
  26  * string of tokens can be batched into a section using quotes.
  27  * Quotes embedded in tokens are ignored.  Unterminated quotes
  28  * will just take the rest of the line as the section.
  29  * 
  30  * <code>
  31  * EXAMPLE
  32  * token1<space>token2<tab>"token 3 token 3"<space><space>token4
  33  * </code>
  34  *
  35  * @author Erich P. Gatejen
  36  * @version 1.0
  37  * <i>Version History</i>
  38  * <code>EPG - Rewrite - 3Aug03 
  39  * 
  40  */
  41 public class CommandLine {
  42 
  43 	// internal state values
  44 	private final static int STATE_START = 0;
  45 	private final static int STATE_KEEP = 1;
  46 	private final static int STATE_QUOTE = 2;
  47 	private final static int STATE_DONE = 3;
  48 
  49 	// Current command
  50 	private String current;
  51 	private int rover;
  52 	private int length;
  53 
  54 	/**
  55 	 * Start on a command line.
  56 	 * @param command the command line to process
  57 	 */
  58 	public void start(String command) {
  59 		current = command;
  60 		length = command.length();
  61 		rover = 0;
  62 	}
  63 
  64 	/**
  65 	 * Get the next section.  It will return null if there is nothing 
  66 	 * left (or it is not start()'ed.)
  67 	 * @return the next section or null
  68 	 */
  69 	public String get() {
  70 
  71 		String result = null;
  72 		int state = STATE_START;
  73 		char item;
  74 
  75 		try {
  76 			StringBuffer buf = new StringBuffer();
  77 			while ((rover < length) && (state != STATE_DONE)) {
  78 				item = current.charAt(rover);
  79 				switch (state) {
  80 
  81 					case STATE_START :
  82 						if (Character.isWhitespace(item)) {
  83 							// keep eating them
  84 						} else if (item == Constants.CHAR_DQUOTE) {
  85 							state = STATE_QUOTE;
  86 						} else {
  87 							state = STATE_KEEP;
  88 							buf.append(item);
  89 						}
  90 						break;
  91 
  92 					case STATE_KEEP :
  93 						if (Character.isWhitespace(item)) {
  94 							// DONE!
  95 							state = STATE_DONE;
  96 						} else {
  97 							buf.append(item);
  98 						}
  99 						break;
 100 
 101 					case STATE_QUOTE :
 102 						if (item == Constants.CHAR_DQUOTE) {
 103 							state = STATE_DONE;
 104 						} else {
 105 							buf.append(item);
 106 						}
 107 						break;
 108 
 109 					case STATE_DONE :
 110 					default :
 111 						// this shouldn't happen
 112 						break;
 113 
 114 				} // end switch
 115 				rover++;
 116 
 117 			} // end while
 118 
 119 			// Take what we got!
 120 			result = buf.toString();
 121 			if (result.length()<=0) result = null;
 122 
 123 		} catch (Exception e) {
 124 			// any exception invalidates the whole thing
 125 			current = null;
 126 			result = null;
 127 		}
 128 		return result;
 129 	}
 130 
 131 }