1 23 24 27 28 package com.sun.enterprise.cli.framework; 29 30 import java.util.ListIterator ; 31 32 74 public class CLITokenizer 75 { 76 private final static char ESCAPE_CHAR = '\\'; 77 private final static String QUOTE_STRING = "\""; 78 private int size = 0; 79 private ListIterator tokenIterator = null; 80 81 82 88 public CLITokenizer(String stringToken, String delimiter) 89 throws CommandException 90 { 91 if (!checkForMatchingQuotes(stringToken)) 92 throw new CommandException(getLocalizedString("UnclosedString")); 93 94 if (stringToken != null && delimiter != null) 95 tokenIterator = populateList(stringToken, delimiter); 96 else 97 throw new CommandException(getLocalizedString("CouldNotCreateCliTokenizer")); 98 } 99 100 104 public int countTokens() 105 { 106 return size; 107 } 108 109 113 public boolean hasMoreTokens() 114 { 115 return tokenIterator.hasNext(); 116 } 117 118 122 public String nextTokenWithoutEscapeAndQuoteChars() throws CommandException 123 { 124 final String strWOEscape = removeEscapeChars((String )tokenIterator.next()); 125 final String strWOQuotes = removeQuoteChars(strWOEscape); 126 return removeEscapeCharsFromQuotes(strWOQuotes); 127 } 128 129 130 134 public String nextToken() throws CommandException 135 { 136 return (String )tokenIterator.next(); 137 } 138 139 140 146 private boolean checkForMatchingQuotes(String str) throws CommandException 147 { 148 int beginQuote = getStringDelimiterIndex(str, QUOTE_STRING, 0); 150 151 while (beginQuote != -1) 152 { 153 int endQuote = getStringDelimiterIndex(str, QUOTE_STRING, beginQuote+1); 154 if (endQuote == -1) return false; 155 beginQuote = getStringDelimiterIndex(str, QUOTE_STRING, endQuote+1); 156 } 157 return true; 158 } 159 160 161 168 private ListIterator populateList(String strToken, String delimiter) 169 throws CommandException 170 { 171 java.util.List tokenList = new java.util.Vector (); 172 int endIndex = getStringDelimiterIndex(strToken, delimiter, 0); 173 if (endIndex == -1) tokenList.add(strToken); 174 else 175 { 176 int beginIndex = 0; 177 while (endIndex > -1) 178 { 179 if (beginIndex != endIndex) 181 tokenList.add(strToken.substring(beginIndex, endIndex)); 182 beginIndex = endIndex + 1; 183 endIndex = getStringDelimiterIndex(strToken, delimiter, beginIndex); 184 } 185 if (beginIndex != strToken.length()) 187 tokenList.add(strToken.substring(beginIndex)); 188 } 189 size = tokenList.size(); 190 try { 191 return tokenList.listIterator(); 192 } 193 catch (java.lang.IndexOutOfBoundsException ioe) { 194 throw new CommandException(ioe); 195 } 196 } 197 198 199 204 private String removeEscapeChars(String strValue) 205 throws CommandException 206 { 207 int prefixIndex = 0; 208 java.lang.StringBuffer strbuff = new java.lang.StringBuffer (); 209 210 while (prefixIndex < strValue.length()) 211 { 212 int delimeterIndex = getStringDelimiterIndex(strValue, 213 String.valueOf(ESCAPE_CHAR), prefixIndex); 214 if (delimeterIndex == -1) 215 { 216 strbuff.append(strValue.substring(prefixIndex)); 217 break; 218 } 219 220 if (delimeterIndex+1 < strValue.length() && 222 String.valueOf(strValue.charAt(delimeterIndex+1)).equals(QUOTE_STRING)) 223 strbuff.append(strValue.substring(prefixIndex, delimeterIndex+1)); 224 else 225 strbuff.append(strValue.substring(prefixIndex, delimeterIndex)); 226 227 prefixIndex = delimeterIndex+1; 228 } 229 return strbuff.toString(); 230 } 231 232 237 private String removeEscapeCharsFromQuotes(String strValue) throws CommandException 238 { 239 int prefixIndex = 0; 240 java.lang.StringBuffer strbuff = new java.lang.StringBuffer (); 241 242 while (prefixIndex < strValue.length()) 243 { 244 int delimeterIndex = strValue.indexOf(String.valueOf(ESCAPE_CHAR), prefixIndex); 245 if (delimeterIndex == -1) 246 { 247 strbuff.append(strValue.substring(prefixIndex)); 248 break; 249 } 250 if (String.valueOf(strValue.charAt(delimeterIndex+1)).equals(QUOTE_STRING)) 252 strbuff.append(strValue.substring(prefixIndex, delimeterIndex)); 253 else 254 strbuff.append(strValue.substring(prefixIndex, delimeterIndex+1)); 255 256 prefixIndex = delimeterIndex+1; 257 } 258 return strbuff.toString(); 259 } 260 261 262 266 private String removeQuoteChars(String strValue) 267 throws CommandException 268 { 269 int prefixIndex = 0; 270 java.lang.StringBuffer strbuff = new java.lang.StringBuffer (); 271 272 while (prefixIndex < strValue.length()) 273 { 274 int delimeterIndex = getStringDelimiterIndex(strValue, 275 QUOTE_STRING, prefixIndex); 276 if (delimeterIndex == -1) 277 { 278 strbuff.append(strValue.substring(prefixIndex)); 279 break; 280 } 281 strbuff.append(strValue.substring(prefixIndex, delimeterIndex)); 282 prefixIndex = delimeterIndex+1; 283 } 284 return strbuff.toString(); 285 } 286 287 288 297 private int getStringDelimiterIndex(String strToken, String delimiter, 298 int fromIndex) 299 throws CommandException 300 { 301 if (fromIndex > strToken.length()-1) return -1; 302 303 final int hasDelimiter = strToken.indexOf(delimiter, fromIndex); 305 306 final int quoteBeginIndex = strToken.indexOf(QUOTE_STRING, fromIndex); 308 309 if ((quoteBeginIndex != -1) && (hasDelimiter != -1) && 312 (quoteBeginIndex < hasDelimiter)) 313 { 314 final int quoteEndIndex = strToken.indexOf(QUOTE_STRING, quoteBeginIndex+1); 316 317 if (quoteEndIndex == -1) 318 throw new CommandException(getLocalizedString("UnclosedString")); 319 if (quoteEndIndex != (strToken.length()-1)) 320 { 321 return getStringDelimiterIndex(strToken, delimiter, quoteEndIndex + 1); 322 } 323 else 324 { 325 return -1; 326 } 327 } 328 if ((hasDelimiter > 0) && (strToken.charAt(hasDelimiter-1) == ESCAPE_CHAR)) 329 { 330 return getStringDelimiterIndex(strToken, delimiter, hasDelimiter+1); 331 } 332 else 333 { 334 return hasDelimiter; 335 } 336 } 337 338 339 347 private String getLocalizedString(String key) 348 { 349 LocalStringsManager lsm = null; 350 try 351 { 352 lsm = LocalStringsManagerFactory.getCommandLocalStringsManager(); 353 } 354 catch (CommandValidationException cve) 355 { 356 return LocalStringsManager.DEFAULT_STRING_VALUE; 357 } 358 return lsm.getString(key); 359 } 360 361 362 public static void main(String [] args) 363 { 364 try { 365 final CLITokenizer ct = new CLITokenizer(args[0], ":"); 366 while (ct.hasMoreTokens()) { 367 final String nameAndvalue = ct.nextToken(); 368 final CLITokenizer ct2 = new CLITokenizer(nameAndvalue, "="); 369 System.out.println("+++++ ct2 tokens = " + ct2.countTokens() + " +++++"); 370 if (ct2.countTokens() == 1) 371 { 372 System.out.println(ct2.nextTokenWithoutEscapeAndQuoteChars()); 373 } 374 else if (ct2.countTokens() == 2) 375 { 376 System.out.println(ct2.nextTokenWithoutEscapeAndQuoteChars() + " " + 377 ct2.nextTokenWithoutEscapeAndQuoteChars()); 378 } 379 System.out.println("+++++ " + nameAndvalue + " +++++"); 380 } 381 System.out.println("***** the end *****"); 382 } 383 catch (Exception e) { 384 e.printStackTrace(); 385 } 386 387 } 388 389 390 391 } 392 | Popular Tags |