StringUtils { public static List<String> tokenize(String stringToBeParsed) { final String TOKEN = "\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"|\\S+"; final String QUOTED_TOKEN = "^\"(.*)\"$"; List<String> options = new ArrayList<String>(); if (stringToBeParsed != null && stringToBeParsed.length() != 0) { Pattern p = Pattern.compile(TOKEN, Pattern.DOTALL); Matcher m = p.matcher(stringToBeParsed); while (m.find()) { if (!(m.group().trim().equals(""))) { options.add(Pattern.compile(QUOTED_TOKEN, Pattern.DOTALL).matcher(m.group().trim()).replaceAll("$1")); } } } return options; }  static List<String> tokenize(String stringToBeParsed); static String trimMultiline(String toTrim);  }
@Test public void tokenizeQuotedNonClosedTokens2() { List<String> list = StringUtils.tokenize("\"foo bar\" baz\""); Assert.assertEquals("List is not empty", 2, list.size()); Assert.assertEquals("Expecting foo bar", "foo bar", list.get(0)); Assert.assertEquals("Expecting baz\"", "baz\"", list.get(1)); }
@Test public void tokenizeQuotedNonClosedTokens3() { List<String> list = StringUtils.tokenize("\"foo bar\" baz \""); Assert.assertEquals("List is not empty", 3, list.size()); Assert.assertEquals("Expecting foo bar", "foo bar", list.get(0)); Assert.assertEquals("Expecting bar", "baz", list.get(1)); Assert.assertEquals("Expecting \"", "\"", list.get(2)); }
@Test public void tokenizeEmptyString() { List<String> list = StringUtils.tokenize(""); Assert.assertEquals("List is empty", 0, list.size()); }
@Test public void tokenizeNull() { List<String> list = StringUtils.tokenize(null); Assert.assertEquals("List is empty", 0, list.size()); }
@Test public void tokenizeSimpleTokens() { List<String> list = StringUtils.tokenize("foo bar"); Assert.assertEquals("List is not empty", 2, list.size()); Assert.assertEquals("Expecting foo", "foo", list.get(0)); Assert.assertEquals("Expecting bar", "bar", list.get(1)); }
@Test public void tokenizeQuotedTokens() { List<String> list = StringUtils.tokenize("\"foo bar\" baz"); Assert.assertEquals("List is not empty", 2, list.size()); Assert.assertEquals("Expecting foo bar", "foo bar", list.get(0)); Assert.assertEquals("Expecting baz", "baz", list.get(1)); }
@Test public void tokenizeQuotedNonClosedTokens() { List<String> list = StringUtils.tokenize("\"foo bar baz"); Assert.assertEquals("List is not empty", 3, list.size()); Assert.assertEquals("Expecting \"foo", "\"foo", list.get(0)); Assert.assertEquals("Expecting bar", "bar", list.get(1)); Assert.assertEquals("Expecting baz", "baz", list.get(2)); }