/** * Split string value using given separator and return initialized pair instance. Pair values are trimmed for spaces * so that this method eliminates spaces around separator and around string value. For example, " john : doe " will * return Pair("john", "doe"). * * @param string string value, * @param separator pair components separator. * @return newly create pair instance. * @throws BugError if separator not found. */ private static Pair pair(String string, char separator) { int separatorIndex = string.indexOf(separator); if(separatorIndex == -1) { throw new BugError("Missing pair separator. Cannot initialize pair instance."); } final String first = string.substring(0, separatorIndex).trim(); final String second = string.substring(separatorIndex + 1).trim(); return new Pair(first, second); }
@Override protected Object doExec(Element element, Object scope, String expression, Object... arguments) throws TemplateException { if (expression.isEmpty()) { throw new TemplateException("Invalid CSS_CLASS operand. Expression is empty."); } CssClass cssClass = new CssClass(element); PairsList pairs = new PairsList(expression); for (Pair pair : pairs) { // accordingly CSS_CLASS operator syntax first pair value is a conditional expression and the second is the CSS // class name ConditionalExpression conditionalExpression = new ConditionalExpression(content, scope, pair.first()); String className = pair.second(); if (conditionalExpression.value()) { log.debug("True conditional expression |%s|. Add CSS class |%s| to element |%s|.", pair.first(), className, element.trace()); cssClass.add(className); } else { log.debug("False conditional expression |%s|. Remove CSS class |%s| from element |%s|.", pair.first(), className, element.trace()); cssClass.remove(className); } } return cssClass; } }
Pair pair = new Pair(firstValue, expression.substring(colonIndex, semicolonIndex)); if(pairs.contains(pair)) { throw new SyntaxException("Duplicated pairs in expression |%s|.", expression);