1 22 package org.aspectj.util; 23 24 import java.util.*; 25 26 public class ArgumentScanner { 27 28 private final static char quote = '"'; 29 30 public List scanToList(String str) { 31 str = (str == null ? "" : str.trim()) + " "; 32 List args = new Vector(); 33 boolean inQuotes = false; 34 boolean wasInQuotes = false; 35 boolean add = false; 36 int start = 0; 37 int i = -1; 38 try { 39 for (i = start; i < str.length(); i++) { 40 char c = str.charAt(i); 41 if (Character.isWhitespace(c) && !inQuotes) { 42 args.add(str.substring(start,i-(wasInQuotes?1:0))); 43 start = i+1; 44 } else if (c == quote && (inQuotes = !(wasInQuotes = inQuotes))) { 45 start = i+1; 46 } else if (c == '\\') { 47 i++; 48 } 49 } 50 } catch (IndexOutOfBoundsException ioobe) { 51 ioobe.printStackTrace(); 52 throw new RuntimeException ("at " + i); 53 } 54 if (inQuotes) { 55 throw new RuntimeException ("Unmatched quotes"); 56 } 57 return args; 58 } 59 60 public String [] scanToArray(String str) { 61 List list = scanToList(str); 62 String [] args = new String [list.size()]; 63 for (int i = 0; i < args.length; i++) { 64 args[i] = list.get(i) + ""; 65 } 66 return args; 67 } 68 69 static String [] strings = { 70 " a a \"asdf\"", 71 }; 72 public static void main(String [] args) { 73 for (int i = 0; i < strings.length; i++) { 74 test(strings[i]); 75 } 76 } 77 static void test(String argsStr) { 78 List newList = new ArgumentScanner().scanToList(argsStr); 79 System.out.println("argsStr=" + argsStr); 80 System.out.println("newList=" + newList); 81 } 82 } 83 | Popular Tags |