/** * Returns an instance of Filter by name. During template parsing/compilation * phase, an instance of Filter is created for each time it is used in a template. * The same Filter instance gets re-used throughout template document lifecycle. * * @param name Name of the filter to be returned. * @param line Line number on the template which tries to instantiate a filter instance * @param col Column number on the template which tries to instantiate a filter instance * @return Returns an instance of the Filter * @throws TemplateParsingException Might be thrown if the filter is unknown or can not be instantiated. */ public Filter getFilter(String name, int line, int col) throws TemplateParsingException { try { String key = name.toLowerCase(); Class<? extends Filter> filterClass = filters.get(key); if (filterClass == null) { throw new TemplateParsingException("Unknown filter: " + name, line, col); } return filterClass.newInstance(); } catch (InstantiationException e) { throw new TemplateParsingException("Could not instantiate filter: " + name, e, line, col); } catch (IllegalAccessException e) { throw new TemplateParsingException("Could not instantiate filter: " + name, e, line, col); } }
public String toString() { return getMessage() + " (" + "Line: " + line + ", Col: " + col + ")"; } }
private TemplateNode parseExpressionLanguageDirective(ParserDirectiveToken tok) { String args = tok.getArgs(); if (args == null || args.trim().equals("")) { throw new TemplateParsingException("Invalid expression language directive", currentToken.getLineNo(), currentToken.getColumn()); } expressionLanguage = Expressions.getExpressionLanguageByName(args.trim()); return new ExpressionLanguageDirective(args.trim()); }
private TemplateNode parseNamespaceDirective(ParserDirectiveToken tok) { String args = tok.getArgs(); if (args == null) { throw new TemplateParsingException("Invalid namespace directive", currentToken.getLineNo(), currentToken.getColumn()); } Matcher matcher = namespaceDirectivePattern.matcher(args); if (!matcher.find()) { throw new TemplateParsingException("Invalid namespace directive", currentToken.getLineNo(), currentToken.getColumn()); } String name = matcher.group(1); String uri = matcher.group(2); putNamespaceMapping(name, uri); return new NamespaceDirective(name, uri); }
private TemplateNode parseSetDirective(ParserDirectiveToken tok) { String args = tok.getArgs(); if (args == null) { throw new TemplateParsingException("Invalid set directive", currentToken.getLineNo(), currentToken.getColumn()); } Matcher matcher = setDirectivePattern.matcher(args); if (!matcher.find()) { throw new TemplateParsingException("Invalid set directive", currentToken.getLineNo(), currentToken.getColumn()); } String varName = matcher.group(1); String expression = matcher.group(2); Expression ex = expressionLanguage.parse(expression, currentToken.getLineNo(), currentToken.getColumn()); return new SetDirective(varName, ex); }
public Tag addBehavior(TagBehavior behavior) { dynamic = true; if (behavior instanceof ConditionalTagBehavior) { if (conditionalBehavior != null) { throw new TemplateParsingException("Only one conditional behaviour be assigned to a tag. Tag: " + toString(), getBeginLine(), getBeginColumn()); } conditionalBehavior = (ConditionalTagBehavior) behavior; } else if (behavior instanceof LoopingTagBehavior) { if (looping != null) { throw new TemplateParsingException("Only one looping behaviour be assigned to a tag. Tag: " + toString(), getBeginLine(), getBeginColumn()); } looping = (LoopingTagBehavior) behavior; } else if (behavior instanceof ModifyingTagBehavior) { if (modifyingBehaviors == null) { modifyingBehaviors = new ArrayList<ModifyingTagBehavior>(); } modifyingBehaviors.add((ModifyingTagBehavior) behavior); } return this; }
private TemplateNode parseIncludeNode(ParserDirectiveToken tok) { String args = tok.getArgs(); if (args == null) { throw new TemplateParsingException("Invalid include directive", currentToken.getLineNo(), currentToken.getColumn()); throw new TemplateParsingException("Invalid extend directive", currentToken.getLineNo(), currentToken.getColumn()); return new IncludeNode(templateLoader, fileName, expressionLanguage, selector); } catch (TemplateLoadingException e) { throw new TemplateParsingException("Could not load the include", e, currentToken.getLineNo(), currentToken.getColumn()); } catch (BehaviorInstantiationException e) { throw new TemplateParsingException("Could not load the include", e, currentToken.getLineNo(), currentToken.getColumn());
@Override public void init() throws TemplateParsingException { Attribute a = getAttribute(nameSpace, "form"); if (!(a instanceof DynamicAttribute)) { throw new TemplateParsingException("The required attribute form for form element is not specified", getBeginLine(), getBeginColumn()); } DynamicAttribute form = (DynamicAttribute) a; try { formExpression = form.getExpression(); } catch (ExpressionParsingException e) { throw new TemplateParsingException("Error parsing expression for form tag", e, getBeginLine(), getBeginColumn()); } }
private TemplateNode parseExtendsDirective(ParserDirectiveToken tok) { String args = tok.getArgs(); if (args == null) { throw new TemplateParsingException("Invalid extend directive", currentToken.getLineNo(), currentToken.getColumn()); throw new TemplateParsingException("Invalid extend directive", currentToken.getLineNo(), currentToken.getColumn()); return new ExtendsDirective(templateLoader, fileName, selector, expressionLanguage); } catch (TemplateLoadingException e) { throw new TemplateParsingException("Could not load the extended template", e, currentToken.getLineNo(), currentToken.getColumn()); } catch (BehaviorInstantiationException e) { throw new TemplateParsingException("Could not load the extended template", e, currentToken.getLineNo(), currentToken.getColumn());
private TemplateNode parserDirective() throws TemplateParsingException { ParserDirectiveToken tok = (ParserDirectiveToken) currentToken; if ("include".equalsIgnoreCase(tok.getDirective()) && templateLoader != null) { return parseIncludeNode(tok); } if ("set".equalsIgnoreCase(tok.getDirective())) { return parseSetDirective(tok); } if ("namespace".equalsIgnoreCase(tok.getDirective())) { return parseNamespaceDirective(tok); } if ("expressionLanguage".equalsIgnoreCase(tok.getDirective())) { return parseExpressionLanguageDirective(tok); } if ("debug".equalsIgnoreCase(tok.getDirective())) { return new DebugDirective(); } if ("extend".equalsIgnoreCase(tok.getDirective()) || "extends".equalsIgnoreCase(tok.getDirective())) { if (template.hasChildren()) { throw new TemplateParsingException("extend directive should be the first element of the template", tok.getLineNo(), tok.getColumn()); } return parseExtendsDirective(tok); } return null; }
throw new TemplateParsingException(behavior.getValidationError(), nextTag.getBeginLine(), nextTag.getBeginColumn());
private ExpressionNode expression() throws TemplateParsingException { try { ExpressionToken tok = (ExpressionToken) currentToken; ExpressionNode node = new ExpressionNode(currentToken.value, expressionLanguage.parse(currentToken.value, currentToken.getLineNo(), currentToken.getColumn()), tok.isRawExpression()); node.setBeginLine(currentToken.getLineNo()); node.setBeginColumn(currentToken.getColumn()); String[] lines = node.getSource().split("\n"); node.setEndLine(currentToken.getLineNo() + lines.length); if (lines.length == 0) { node.setEndColumn(currentToken.getColumn() + node.getSource().length()); } else { node.setEndColumn(currentToken.getColumn() + lines[lines.length - 1].replaceAll("\r", "").length()); } if (tok.getFilters() != null) { node.setFilters(tok.getFilters()); } return node; } catch (ExpressionParsingException e) { throw new TemplateParsingException("Error parsing expression", e, currentToken.getLineNo(), currentToken.getColumn()); } }
throw new TemplateParsingException("Error parsing expression", e1, currentToken.getLineNo(), currentToken.getColumn()); element = new SimpleAttribute(tok.getAttributeName(), tok.getNameSpace(), tok.getLineNo(), tok.getLineNo()); } else { throw new TemplateParsingException("Error parsing template file. Unterminated tag?", currentToken.getLineNo(), currentToken.getColumn()); throw new TemplateParsingException("Error parsing expression", e1, currentToken.getLineNo(), currentToken.getColumn());