TemplateParser implements Parser { static List<String> splitAssign(String value) { List<String> list = new ArrayList<String>(); int i = value.indexOf('='); while ((i = value.indexOf('=', i + 1)) > 0) { if (i + 1 < value.length() && value.charAt(i + 1) == '=') { i++; } else if (value.charAt(i - 1) != '>' && value.charAt(i - 1) != '<' && value.charAt(i - 1) != '!' && isEndString(value.substring(0, i - 1))) { String sub = value.substring(0, i); int j = sub.lastIndexOf(','); int k = sub.lastIndexOf('>'); if (j > 0 && j < k) { int g = 0; for (int n = k; n >= 0; n--) { char c = sub.charAt(n); if (c == '>') { g++; } else if (c == '<') { g--; } if (g == 0) { sub = sub.substring(0, n); j = sub.lastIndexOf(','); break; } } } if (j > 0) { list.add(value.substring(0, j)); value = value.substring(j + 1); i = i - j - 1; } else { break; } } } list.add(value); return list; }  void setRemoveDirectiveBlankLine(boolean removeDirectiveBlankLine); void setSetDirective(String[] setDirective); void setIfDirective(String[] ifDirective); void setElseDirective(String[] elseDirective); void setForDirective(String[] forDirective); void setBreakDirective(String[] breakDirective); void setMacroDirective(String[] macroDirective); void setEndDirective(String[] endDirective); void setDefaultVariableType(String defaultVariableType); void setImportMacros(String[] importMacros); void setEngine(Engine engine); void setExpressionParser(Parser expressionParser); void setImportPackages(String[] importPackages); void setImportVariables(String[] importVariables); void setImportMethods(Object[] importMethods); String[] getImportDirective(); void init(); void inited(); Node parse(String source, int offset);  }
@Test public void testSplitAssign() throws Exception { List<String> list = TemplateParser.splitAssign("book = books.get(0)"); Assert.assertEquals(1, list.size()); Assert.assertEquals("book = books.get(0)", list.get(0)); list = TemplateParser.splitAssign("book1 = books.get(0), book2 = books.get(1)"); Assert.assertEquals(2, list.size()); Assert.assertEquals("book1 = books.get(0)", list.get(0)); Assert.assertEquals(" book2 = books.get(1)", list.get(1)); list = TemplateParser.splitAssign("book1 = books.get(0), book2 = books.get(1), book3 = books.get(2)"); Assert.assertEquals(3, list.size()); Assert.assertEquals("book1 = books.get(0)", list.get(0)); Assert.assertEquals(" book2 = books.get(1)", list.get(1)); Assert.assertEquals(" book3 = books.get(2)", list.get(2)); list = TemplateParser.splitAssign("Book book = books.get(0)"); Assert.assertEquals(1, list.size()); Assert.assertEquals("Book book = books.get(0)", list.get(0)); list = TemplateParser.splitAssign("Book book1 = books.get(0), Book book2 = books.get(1)"); Assert.assertEquals(2, list.size()); Assert.assertEquals("Book book1 = books.get(0)", list.get(0)); Assert.assertEquals(" Book book2 = books.get(1)", list.get(1)); list = TemplateParser.splitAssign("Book book1 = books.get(0), Book book2 = books.get(1), Book book3 = books.get(2)"); Assert.assertEquals(3, list.size()); Assert.assertEquals("Book book1 = books.get(0)", list.get(0)); Assert.assertEquals(" Book book2 = books.get(1)", list.get(1)); Assert.assertEquals(" Book book3 = books.get(2)", list.get(2)); list = TemplateParser.splitAssign("Map<String, Book> books = books.toMap()"); Assert.assertEquals(1, list.size()); Assert.assertEquals("Map<String, Book> books = books.toMap()", list.get(0)); list = TemplateParser.splitAssign("Map<String, Book> bookMap = books.toMap(), Map<String, Book> bookMap2 = books2.toMap()"); Assert.assertEquals(2, list.size()); Assert.assertEquals("Map<String, Book> bookMap = books.toMap()", list.get(0)); Assert.assertEquals(" Map<String, Book> bookMap2 = books2.toMap()", list.get(1)); list = TemplateParser.splitAssign("Map<String, Book> bookMap = books.toMap(), Map<String, Book> bookMap2 = books2.toMap(), Map<String, Book> bookMap3 = books3.toMap()"); Assert.assertEquals(3, list.size()); Assert.assertEquals("Map<String, Book> bookMap = books.toMap()", list.get(0)); Assert.assertEquals(" Map<String, Book> bookMap2 = books2.toMap()", list.get(1)); Assert.assertEquals(" Map<String, Book> bookMap3 = books3.toMap()", list.get(2)); list = TemplateParser.splitAssign("Map<String, Book> bookMap = books.toMap(), Book book = books.get(0)"); Assert.assertEquals(2, list.size()); Assert.assertEquals("Map<String, Book> bookMap = books.toMap()", list.get(0)); Assert.assertEquals(" Book book = books.get(0)", list.get(1)); list = TemplateParser.splitAssign("Map<String, Book> bookMap = books.toMap(), Book book1 = books.get(0), book2 = books.get(1)"); Assert.assertEquals(3, list.size()); Assert.assertEquals("Map<String, Book> bookMap = books.toMap()", list.get(0)); Assert.assertEquals(" Book book1 = books.get(0)", list.get(1)); Assert.assertEquals(" book2 = books.get(1)", list.get(2)); list = TemplateParser.splitAssign("pair = \"a = b\""); Assert.assertEquals(1, list.size()); Assert.assertEquals("pair = \"a = b\"", list.get(0)); list = TemplateParser.splitAssign("pair1 = \"a \\\" = b\\\", pair2 = \"a = b\""); Assert.assertEquals(2, list.size()); Assert.assertEquals("pair1 = \"a \\\" = b\\\"", list.get(0)); Assert.assertEquals(" pair2 = \"a = b\"", list.get(1)); list = TemplateParser.splitAssign("enable = user.role == 'member'"); Assert.assertEquals(1, list.size()); Assert.assertEquals("enable = user.role == 'member'", list.get(0)); list = TemplateParser.splitAssign("enable1 = user.role == 'member', enable2 = user.role == 'member'"); Assert.assertEquals(2, list.size()); Assert.assertEquals("enable1 = user.role == 'member'", list.get(0)); Assert.assertEquals(" enable2 = user.role == 'member'", list.get(1)); list = TemplateParser.splitAssign("enable1 = user.role == 'member', enable2 = user.role == 'member', enable3 = user.role == 'member'"); Assert.assertEquals(3, list.size()); Assert.assertEquals("enable1 = user.role == 'member'", list.get(0)); Assert.assertEquals(" enable2 = user.role == 'member'", list.get(1)); Assert.assertEquals(" enable3 = user.role == 'member'", list.get(2)); list = TemplateParser.splitAssign("access = user.age >= 18"); Assert.assertEquals(1, list.size()); Assert.assertEquals("access = user.age >= 18", list.get(0)); list = TemplateParser.splitAssign("access1 = user.age >= 18, access2 = user.age >= 18"); Assert.assertEquals(2, list.size()); Assert.assertEquals("access1 = user.age >= 18", list.get(0)); Assert.assertEquals(" access2 = user.age >= 18", list.get(1)); list = TemplateParser.splitAssign("access1 = user.age >= 18, access2 = user.age >= 18, access3 = user.age >= 18"); Assert.assertEquals(3, list.size()); Assert.assertEquals("access1 = user.age >= 18", list.get(0)); Assert.assertEquals(" access2 = user.age >= 18", list.get(1)); Assert.assertEquals(" access3 = user.age >= 18", list.get(2)); list = TemplateParser.splitAssign("enable = user.role == 'member', access = user.age >= 18"); Assert.assertEquals(2, list.size()); Assert.assertEquals("enable = user.role == 'member'", list.get(0)); Assert.assertEquals(" access = user.age >= 18", list.get(1)); list = TemplateParser.splitAssign("enable = user.role == 'member', disable = user.role != 'member', access = user.age >= 18, forbid = user.age <= 18"); Assert.assertEquals(4, list.size()); Assert.assertEquals("enable = user.role == 'member'", list.get(0)); Assert.assertEquals(" disable = user.role != 'member'", list.get(1)); Assert.assertEquals(" access = user.age >= 18", list.get(2)); Assert.assertEquals(" forbid = user.age <= 18", list.get(3)); }