CommandLineSupport { public static List<String> split(final String commandline) { if (commandline == null || commandline.length() == 0) { return new ArrayList<String>(); } final List<String> args = new ArrayList<String>(); final StringBuilder current = new StringBuilder(); int mode = M_STRIPWHITESPACE; int endChar = BLANK; for (final char c : commandline.toCharArray()) { switch (mode) { case M_STRIPWHITESPACE: if (!Character.isWhitespace(c)) { if (c == QUOTE) { endChar = QUOTE; } else { current.append(c); endChar = BLANK; } mode = M_PARSEARGUMENT; } break; case M_PARSEARGUMENT: if (c == endChar) { addArgument(args, current); mode = M_STRIPWHITESPACE; } else if (c == SLASH) { current.append(SLASH); mode = M_ESCAPED; } else { current.append(c); } break; case M_ESCAPED: if (c == QUOTE || c == SLASH) { current.setCharAt(current.length() - 1, c); } else if (c == endChar) { addArgument(args, current); mode = M_STRIPWHITESPACE; } else { current.append(c); } mode = M_PARSEARGUMENT; break; default: continue; } } addArgument(args, current); return args; }  static String quote(final String arg); static String quote(final List<String> args); static List<String> split(final String commandline);  }
@Test public void testSplit1() { assertEquals(Arrays.asList(), CommandLineSupport.split(null)); }
@Test public void testSplit2() { assertEquals(Arrays.asList(), CommandLineSupport.split("")); }
@Test public void testSplit3() { assertEquals(Arrays.asList("abc"), CommandLineSupport.split("abc")); }
@Test public void testSplit4() { assertEquals(Arrays.asList("aa", "bbbb", "cccccc"), CommandLineSupport.split(" aa bbbb cccccc ")); }
@Test public void testSplit5() { assertEquals(Arrays.asList("a a", "b b "), CommandLineSupport.split("\"a a\" \"b b \" ")); }
@Test public void testSplit6() { assertEquals(Arrays.asList("a\"c"), CommandLineSupport.split("a\\\"c")); }
@Test public void testSplit7() { assertEquals(Arrays.asList("a\\c"), CommandLineSupport.split("a\\c")); }
@Test public void testSplit8() { assertEquals(Arrays.asList("a\\"), CommandLineSupport.split("a\\")); }
@Test public void testSplit9() { assertEquals(Arrays.asList("a\\", "b"), CommandLineSupport.split("a\\ b")); }
@Test public void testSplit10() { assertEquals(Arrays.asList("a\\b"), CommandLineSupport.split("a\\\\b")); }