use of org.thymeleaf.model.IModelFactory in project thymeleaf-tests by thymeleaf.
the class AttrModelAttributeTagProcessor method doProcess.
@Override
protected void doProcess(final ITemplateContext context, final IModel model, final AttributeName attributeName, final String attributeValue, final IElementModelStructureHandler structureHandler) {
final IModelFactory modelFactory = context.getModelFactory();
final IProcessableElementTag firstEvent = (IProcessableElementTag) model.get(0);
final IProcessableElementTag newFirstEvent = modelFactory.setAttribute(firstEvent, "var", (String) context.getVariable("var"));
model.replace(0, newFirstEvent);
}
use of org.thymeleaf.model.IModelFactory in project thymeleaf by thymeleaf.
the class AbstractAttributeModelProcessor method doProcess.
@Override
protected final void doProcess(final ITemplateContext context, final IModel model, final IElementModelStructureHandler structureHandler) {
AttributeName attributeName = null;
IProcessableElementTag firstEvent = null;
try {
attributeName = getMatchingAttributeName().getMatchingAttributeName();
firstEvent = (IProcessableElementTag) model.get(0);
final String attributeValue = EscapedAttributeUtils.unescapeAttribute(context.getTemplateMode(), firstEvent.getAttributeValue(attributeName));
doProcess(context, model, attributeName, attributeValue, structureHandler);
if (this.removeAttribute) {
if (model.size() > 0 && model.get(0) instanceof IProcessableElementTag) {
firstEvent = (IProcessableElementTag) model.get(0);
final IModelFactory modelFactory = context.getModelFactory();
final IProcessableElementTag newFirstEvent = modelFactory.removeAttribute(firstEvent, attributeName);
if (newFirstEvent != firstEvent) {
model.replace(0, newFirstEvent);
}
}
}
} catch (final TemplateProcessingException e) {
if (firstEvent != null) {
String attributeTemplateName = firstEvent.getTemplateName();
final IAttribute attribute = firstEvent.getAttribute(attributeName);
int attributeLine = (attribute != null ? attribute.getLine() : -1);
int attributeCol = (attribute != null ? attribute.getCol() : -1);
if (attributeTemplateName != null) {
if (!e.hasTemplateName()) {
e.setTemplateName(attributeTemplateName);
}
}
if (attributeLine != -1 && attributeCol != -1) {
if (!e.hasLineAndCol()) {
e.setLineAndCol(attributeLine, attributeCol);
}
}
}
throw e;
} catch (final Exception e) {
// We will try to add all information possible to the exception report (template name, line, col)
String attributeTemplateName = null;
int attributeLine = -1;
int attributeCol = -1;
if (firstEvent != null) {
attributeTemplateName = firstEvent.getTemplateName();
final IAttribute attribute = firstEvent.getAttribute(attributeName);
attributeLine = (attribute != null ? attribute.getLine() : -1);
attributeCol = (attribute != null ? attribute.getCol() : -1);
}
throw new TemplateProcessingException("Error during execution of processor '" + this.getClass().getName() + "'", attributeTemplateName, attributeLine, attributeCol, e);
}
}
Aggregations