Search in sources :

Example 21 with IModelFactory

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);
}
Also used : IProcessableElementTag(org.thymeleaf.model.IProcessableElementTag) IModelFactory(org.thymeleaf.model.IModelFactory)

Example 22 with IModelFactory

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);
    }
}
Also used : IProcessableElementTag(org.thymeleaf.model.IProcessableElementTag) IAttribute(org.thymeleaf.model.IAttribute) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) AttributeName(org.thymeleaf.engine.AttributeName) IModelFactory(org.thymeleaf.model.IModelFactory) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException)

Aggregations

IModelFactory (org.thymeleaf.model.IModelFactory)22 IModel (org.thymeleaf.model.IModel)15 IStandaloneElementTag (org.thymeleaf.model.IStandaloneElementTag)13 IProcessableElementTag (org.thymeleaf.model.IProcessableElementTag)6 AttributeName (org.thymeleaf.engine.AttributeName)1 StandardModelFactory (org.thymeleaf.engine.StandardModelFactory)1 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)1 IAttribute (org.thymeleaf.model.IAttribute)1 ICloseElementTag (org.thymeleaf.model.ICloseElementTag)1 IOpenElementTag (org.thymeleaf.model.IOpenElementTag)1 ITemplateEvent (org.thymeleaf.model.ITemplateEvent)1