Search in sources :

Example 6 with AttributeName

use of org.thymeleaf.engine.AttributeName 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)

Example 7 with AttributeName

use of org.thymeleaf.engine.AttributeName in project thymeleaf by thymeleaf.

the class StandardStyleappendTagProcessor method doProcess.

@Override
protected final void doProcess(final ITemplateContext context, final IProcessableElementTag tag, final AttributeName attributeName, final String attributeValue, final Object expressionResult, final IElementTagStructureHandler structureHandler) {
    String newAttributeValue = EscapedAttributeUtils.escapeAttribute(getTemplateMode(), expressionResult == null ? null : expressionResult.toString());
    // If we are not adding anything, we'll just leave it untouched
    if (newAttributeValue != null && newAttributeValue.length() > 0) {
        final AttributeName targetAttributeName = this.targetAttributeDefinition.getAttributeName();
        if (tag.hasAttribute(targetAttributeName)) {
            final String currentValue = tag.getAttributeValue(targetAttributeName);
            if (currentValue.length() > 0) {
                newAttributeValue = currentValue + ' ' + newAttributeValue;
            }
        }
        StandardProcessorUtils.setAttribute(structureHandler, this.targetAttributeDefinition, TARGET_ATTR_NAME, newAttributeValue);
    }
}
Also used : AttributeName(org.thymeleaf.engine.AttributeName)

Aggregations

AttributeName (org.thymeleaf.engine.AttributeName)7 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)3 IAttribute (org.thymeleaf.model.IAttribute)3 MatchingAttributeName (org.thymeleaf.processor.element.MatchingAttributeName)2 IModelFactory (org.thymeleaf.model.IModelFactory)1 IProcessableElementTag (org.thymeleaf.model.IProcessableElementTag)1 FragmentExpression (org.thymeleaf.standard.expression.FragmentExpression)1 IStandardExpression (org.thymeleaf.standard.expression.IStandardExpression)1 IStandardExpressionParser (org.thymeleaf.standard.expression.IStandardExpressionParser)1 TemplateMode (org.thymeleaf.templatemode.TemplateMode)1