Search in sources :

Example 6 with IAttribute

use of org.thymeleaf.model.IAttribute in project thymeleaf by thymeleaf.

the class AbstractAttributeTagProcessor method doProcess.

@Override
protected final void doProcess(final ITemplateContext context, final IProcessableElementTag tag, final IElementTagStructureHandler structureHandler) {
    AttributeName attributeName = null;
    try {
        attributeName = getMatchingAttributeName().getMatchingAttributeName();
        final String attributeValue = EscapedAttributeUtils.unescapeAttribute(context.getTemplateMode(), tag.getAttributeValue(attributeName));
        doProcess(context, tag, attributeName, attributeValue, structureHandler);
        if (this.removeAttribute) {
            structureHandler.removeAttribute(attributeName);
        }
    } catch (final TemplateProcessingException e) {
        // specific because we know exactly what attribute was being executed and caused the error
        if (tag.hasLocation()) {
            if (!e.hasTemplateName()) {
                e.setTemplateName(tag.getTemplateName());
            }
            if (!e.hasLineAndCol()) {
                if (attributeName == null) {
                    // We don't have info about the specific attribute provoking the error
                    e.setLineAndCol(tag.getLine(), tag.getCol());
                } else {
                    final IAttribute attribute = tag.getAttribute(attributeName);
                    if (attribute != null) {
                        e.setLineAndCol(attribute.getLine(), attribute.getCol());
                    }
                }
            }
        }
        throw e;
    } catch (final Exception e) {
        int line = tag.getLine();
        int col = tag.getCol();
        if (attributeName != null) {
            // We don't have info about the specific attribute provoking the error
            final IAttribute attribute = tag.getAttribute(attributeName);
            if (attribute != null) {
                line = attribute.getLine();
                col = attribute.getCol();
            }
        }
        throw new TemplateProcessingException("Error during execution of processor '" + this.getClass().getName() + "'", tag.getTemplateName(), line, col, e);
    }
}
Also used : IAttribute(org.thymeleaf.model.IAttribute) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) AttributeName(org.thymeleaf.engine.AttributeName) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException)

Example 7 with IAttribute

use of org.thymeleaf.model.IAttribute 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

IAttribute (org.thymeleaf.model.IAttribute)7 AttributeName (org.thymeleaf.engine.AttributeName)3 IProcessableElementTag (org.thymeleaf.model.IProcessableElementTag)3 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)2 IModelFactory (org.thymeleaf.model.IModelFactory)1 IOpenElementTag (org.thymeleaf.model.IOpenElementTag)1 IStandaloneElementTag (org.thymeleaf.model.IStandaloneElementTag)1 MatchingAttributeName (org.thymeleaf.processor.element.MatchingAttributeName)1 TemplateMode (org.thymeleaf.templatemode.TemplateMode)1