1 33 34 package edu.rice.cs.util; 35 36 import java.util.List ; 37 import java.util.LinkedList ; 38 39 48 public abstract class ArgumentTokenizer { 49 private static final int NO_TOKEN_STATE = 0; 50 private static final int NORMAL_TOKEN_STATE = 1; 51 private static final int SINGLE_QUOTE_STATE = 2; 52 private static final int DOUBLE_QUOTE_STATE = 3; 53 54 58 public static List <String > tokenize(String arguments) { 59 return tokenize(arguments, false); 60 } 61 62 67 public static List <String > tokenize(String arguments, boolean stringify) { 68 69 LinkedList <String > argList = new LinkedList <String >(); 70 StringBuilder currArg = new StringBuilder (); 71 boolean escaped = false; 72 int state = NO_TOKEN_STATE; int len = arguments.length(); 74 75 for (int i = 0; i < len; i++) { 77 char c = arguments.charAt(i); 78 if (escaped) { 79 escaped = false; 81 currArg.append(c); 82 } 83 else { 84 switch(state) { 85 case SINGLE_QUOTE_STATE: 86 if (c == '\'') { 87 state = NORMAL_TOKEN_STATE; 89 } 90 else { 91 currArg.append(c); 92 } 93 break; 94 case DOUBLE_QUOTE_STATE: 95 if (c == '"') { 96 state = NORMAL_TOKEN_STATE; 98 } 99 else if (c == '\\') { 100 i++; 102 char next = arguments.charAt(i); 103 if (next == '"' || next == '\\') { 104 currArg.append(next); 105 } 106 else { 107 currArg.append(c); 108 currArg.append(next); 109 } 110 } 111 else { 112 currArg.append(c); 113 } 114 break; 115 case NO_TOKEN_STATE: 137 case NORMAL_TOKEN_STATE: 138 switch(c) { 139 case '\\': 140 escaped = true; 141 state = NORMAL_TOKEN_STATE; 142 break; 143 case '\'': 144 state = SINGLE_QUOTE_STATE; 145 break; 146 case '"': 147 state = DOUBLE_QUOTE_STATE; 148 break; 149 default: 150 if (!Character.isWhitespace(c)) { 151 currArg.append(c); 152 state = NORMAL_TOKEN_STATE; 153 } 154 else if (state == NORMAL_TOKEN_STATE) { 155 argList.add(currArg.toString()); 157 currArg = new StringBuilder (); 158 state = NO_TOKEN_STATE; 159 } 160 } 161 break; 162 default: 163 throw new IllegalStateException ("ArgumentTokenizer state " + state + " is invalid!"); 164 } 165 } 166 } 167 168 if (escaped) { 170 currArg.append('\\'); 171 argList.add(currArg.toString()); 172 } 173 else if (state != NO_TOKEN_STATE) { 175 argList.add(currArg.toString()); 176 } 177 if (stringify) { 179 for (int i = 0; i < argList.size(); i++) { 180 argList.set(i, "\"" + _escapeQuotesAndBackslashes(argList.get(i)) + "\""); 181 } 182 } 183 return argList; 184 } 185 186 191 protected static String _escapeQuotesAndBackslashes(String s) { 192 final StringBuilder buf = new StringBuilder (s); 193 194 for (int i = s.length()-1; i >= 0; i--) { 199 char c = s.charAt(i); 200 if ((c == '\\') || (c == '"')) { 201 buf.insert(i, '\\'); 202 } 203 else if (c == '\n') { 205 buf.deleteCharAt(i); 206 buf.insert(i, "\\n"); 207 } 208 else if (c == '\t') { 209 buf.deleteCharAt(i); 210 buf.insert(i, "\\t"); 211 } 212 else if (c == '\r') { 213 buf.deleteCharAt(i); 214 buf.insert(i, "\\r"); 215 } 216 else if (c == '\b') { 217 buf.deleteCharAt(i); 218 buf.insert(i, "\\b"); 219 } 220 else if (c == '\f') { 221 buf.deleteCharAt(i); 222 buf.insert(i, "\\f"); 223 } 224 } 225 return buf.toString(); 226 } 227 } 228 | Popular Tags |