@Test public void testParseCommandLineArguments() throws Exception { assertEquals(list("x", "y", "z"), Capsule.parseCommandLineArguments("x y z")); assertEquals(list("x", "y z"), Capsule.parseCommandLineArguments("x 'y z'")); assertEquals(list("x y", "z"), Capsule.parseCommandLineArguments("\"x y\" z")); }
static List<String> parseCommandLineArguments(final String str) { if (str == null || str.length() == 0) return emptyList(); final int NORMAL = 0, IN_QUOTE = 1, IN_DOUBLE_QUOTE = 2; final StringTokenizer tok = new StringTokenizer(str, "\"\'\\ ", true); final ArrayList<String> result = new ArrayList<>(); final StringBuilder current = new StringBuilder(); int state = NORMAL; boolean lastTokenHasBeenQuoted = false; while (tok.hasMoreTokens()) { final String nextTok = tok.nextToken(); switch (state) { case IN_QUOTE: if ("\'".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = NORMAL; } else current.append(nextTok); break; case IN_DOUBLE_QUOTE: if ("\"".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = NORMAL; } else current.append(nextTok); break; default: if ("\'".equals(nextTok)) state = IN_QUOTE; else if ("\"".equals(nextTok)) state = IN_DOUBLE_QUOTE; else if (" ".equals(nextTok)) { if (lastTokenHasBeenQuoted || current.length() != 0) { result.add(current.toString()); current.setLength(0); } } else current.append(nextTok); lastTokenHasBeenQuoted = false; break; } } if (lastTokenHasBeenQuoted || current.length() != 0) result.add(current.toString()); if (state == IN_QUOTE || state == IN_DOUBLE_QUOTE) throw new IllegalArgumentException("unbalanced quotes in " + str); return result; }
Capsule implements Runnable, InvocationHandler { static List<String> parseCommandLineArguments(final String str) { if (str == null || str.length() == 0) return emptyList(); final int NORMAL = 0, IN_QUOTE = 1, IN_DOUBLE_QUOTE = 2; final StringTokenizer tok = new StringTokenizer(str, "\"\'\\ ", true); final ArrayList<String> result = new ArrayList<>(); final StringBuilder current = new StringBuilder(); int state = NORMAL; boolean lastTokenHasBeenQuoted = false; while (tok.hasMoreTokens()) { final String nextTok = tok.nextToken(); switch (state) { case IN_QUOTE: if ("\'".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = NORMAL; } else current.append(nextTok); break; case IN_DOUBLE_QUOTE: if ("\"".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = NORMAL; } else current.append(nextTok); break; default: if ("\'".equals(nextTok)) state = IN_QUOTE; else if ("\"".equals(nextTok)) state = IN_DOUBLE_QUOTE; else if (" ".equals(nextTok)) { if (lastTokenHasBeenQuoted || current.length() != 0) { result.add(current.toString()); current.setLength(0); } } else current.append(nextTok); lastTokenHasBeenQuoted = false; break; } } if (lastTokenHasBeenQuoted || current.length() != 0) result.add(current.toString()); if (state == IN_QUOTE || state == IN_DOUBLE_QUOTE) throw new IllegalArgumentException("unbalanced quotes in " + str); return result; } }
Capsule implements Runnable, InvocationHandler { static List<String> parseCommandLineArguments(final String str) { if (str == null || str.length() == 0) return emptyList(); final int NORMAL = 0, IN_QUOTE = 1, IN_DOUBLE_QUOTE = 2; final StringTokenizer tok = new StringTokenizer(str, "\"\'\\ ", true); final ArrayList<String> result = new ArrayList<>(); final StringBuilder current = new StringBuilder(); int state = NORMAL; boolean lastTokenHasBeenQuoted = false; while (tok.hasMoreTokens()) { final String nextTok = tok.nextToken(); switch (state) { case IN_QUOTE: if ("\'".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = NORMAL; } else current.append(nextTok); break; case IN_DOUBLE_QUOTE: if ("\"".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = NORMAL; } else current.append(nextTok); break; default: if ("\'".equals(nextTok)) state = IN_QUOTE; else if ("\"".equals(nextTok)) state = IN_DOUBLE_QUOTE; else if (" ".equals(nextTok)) { if (lastTokenHasBeenQuoted || current.length() != 0) { result.add(current.toString()); current.setLength(0); } } else current.append(nextTok); lastTokenHasBeenQuoted = false; break; } } if (lastTokenHasBeenQuoted || current.length() != 0) result.add(current.toString()); if (state == IN_QUOTE || state == IN_DOUBLE_QUOTE) throw new IllegalArgumentException("unbalanced quotes in " + str); return result; } @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"}) protected  Capsule(Path jarFile); @SuppressWarnings("LeakingThisInConstructor") protected  Capsule(Capsule pred); }
Capsule implements Runnable, InvocationHandler { static List<String> parseCommandLineArguments(final String str) { if (str == null || str.length() == 0) return emptyList(); final int NORMAL = 0, IN_QUOTE = 1, IN_DOUBLE_QUOTE = 2; final StringTokenizer tok = new StringTokenizer(str, "\"\'\\ ", true); final ArrayList<String> result = new ArrayList<>(); final StringBuilder current = new StringBuilder(); int state = NORMAL; boolean lastTokenHasBeenQuoted = false; while (tok.hasMoreTokens()) { final String nextTok = tok.nextToken(); switch (state) { case IN_QUOTE: if ("\'".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = NORMAL; } else current.append(nextTok); break; case IN_DOUBLE_QUOTE: if ("\"".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = NORMAL; } else current.append(nextTok); break; default: if ("\'".equals(nextTok)) state = IN_QUOTE; else if ("\"".equals(nextTok)) state = IN_DOUBLE_QUOTE; else if (" ".equals(nextTok)) { if (lastTokenHasBeenQuoted || current.length() != 0) { result.add(current.toString()); current.setLength(0); } } else current.append(nextTok); lastTokenHasBeenQuoted = false; break; } } if (lastTokenHasBeenQuoted || current.length() != 0) result.add(current.toString()); if (state == IN_QUOTE || state == IN_DOUBLE_QUOTE) throw new IllegalArgumentException("unbalanced quotes in " + str); return result; } @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"}) protected  Capsule(Path jarFile); @SuppressWarnings("LeakingThisInConstructor") protected  Capsule(Capsule pred); static final void main(String[] args); @SuppressWarnings({"BroadCatchBlock", "UnusedAssignment"}) static int main0(String[] args0); static void premain(String agentArgs, Instrumentation inst); static Object getCapsule(String capletClassName); boolean isAgent(); @Override final void run(); @Override Object invoke(Object proxy, Method method, Object[] args); @Override final int hashCode(); @Override final boolean equals(Object obj); @Override String toString(); }
Capsule implements Runnable, InvocationHandler { static List<String> parseCommandLineArguments(final String str) { if (str == null || str.length() == 0) return emptyList(); final int NORMAL = 0, IN_QUOTE = 1, IN_DOUBLE_QUOTE = 2; final StringTokenizer tok = new StringTokenizer(str, "\"\'\\ ", true); final ArrayList<String> result = new ArrayList<>(); final StringBuilder current = new StringBuilder(); int state = NORMAL; boolean lastTokenHasBeenQuoted = false; while (tok.hasMoreTokens()) { final String nextTok = tok.nextToken(); switch (state) { case IN_QUOTE: if ("\'".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = NORMAL; } else current.append(nextTok); break; case IN_DOUBLE_QUOTE: if ("\"".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = NORMAL; } else current.append(nextTok); break; default: if ("\'".equals(nextTok)) state = IN_QUOTE; else if ("\"".equals(nextTok)) state = IN_DOUBLE_QUOTE; else if (" ".equals(nextTok)) { if (lastTokenHasBeenQuoted || current.length() != 0) { result.add(current.toString()); current.setLength(0); } } else current.append(nextTok); lastTokenHasBeenQuoted = false; break; } } if (lastTokenHasBeenQuoted || current.length() != 0) result.add(current.toString()); if (state == IN_QUOTE || state == IN_DOUBLE_QUOTE) throw new IllegalArgumentException("unbalanced quotes in " + str); return result; } @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"}) protected  Capsule(Path jarFile); @SuppressWarnings("LeakingThisInConstructor") protected  Capsule(Capsule pred); static final void main(String[] args); @SuppressWarnings({"BroadCatchBlock", "UnusedAssignment"}) static int main0(String[] args0); static void premain(String agentArgs, Instrumentation inst); static Object getCapsule(String capletClassName); boolean isAgent(); @Override final void run(); @Override Object invoke(Object proxy, Method method, Object[] args); @Override final int hashCode(); @Override final boolean equals(Object obj); @Override String toString(); static final String VERSION; }