Search in sources :

Example 1 with AttributeName

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

the class StandardClassappendTagProcessor 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)

Example 2 with AttributeName

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

the class StandardDefaultAttributesTagProcessor method processDefaultAttribute.

private static void processDefaultAttribute(final TemplateMode templateMode, final ITemplateContext context, final IProcessableElementTag tag, final IAttribute attribute, final IElementTagStructureHandler structureHandler) {
    try {
        final AttributeName attributeName = attribute.getAttributeDefinition().getAttributeName();
        final String attributeValue = EscapedAttributeUtils.unescapeAttribute(context.getTemplateMode(), attribute.getValue());
        /*
             * Compute the new attribute name (i.e. the same, without the prefix)
             */
        final String originalCompleteAttributeName = attribute.getAttributeCompleteName();
        final String canonicalAttributeName = attributeName.getAttributeName();
        final String newAttributeName;
        if (TextUtil.endsWith(true, originalCompleteAttributeName, canonicalAttributeName)) {
            // We avoid creating a new String instance
            newAttributeName = canonicalAttributeName;
        } else {
            newAttributeName = originalCompleteAttributeName.substring(originalCompleteAttributeName.length() - canonicalAttributeName.length());
        }
        /*
             * Obtain the parser
             */
        final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(context.getConfiguration());
        /*
             * Execute the expression, handling nulls in a way consistent with the rest of the Standard Dialect
             */
        final Object expressionResult;
        if (attributeValue != null) {
            final IStandardExpression expression = expressionParser.parseExpression(context, attributeValue);
            if (expression != null && expression instanceof FragmentExpression) {
                // This is merely a FragmentExpression (not complex, not combined with anything), so we can apply a shortcut
                // so that we don't require a "null" result for this expression if the template does not exist. That will
                // save a call to resource.exists() which might be costly.
                final FragmentExpression.ExecutedFragmentExpression executedFragmentExpression = FragmentExpression.createExecutedFragmentExpression(context, (FragmentExpression) expression);
                expressionResult = FragmentExpression.resolveExecutedFragmentExpression(context, executedFragmentExpression, true);
            } else {
                // Default attributes will ALWAYS be executed in RESTRICTED mode, for safety reasons (they might
                // create attributes involved in code execution)
                expressionResult = expression.execute(context, StandardExpressionExecutionContext.RESTRICTED);
            }
        } else {
            expressionResult = null;
        }
        /*
             * If the result of this expression is NO-OP, there is nothing to execute
             */
        if (expressionResult == NoOpToken.VALUE) {
            structureHandler.removeAttribute(attributeName);
            return;
        }
        /*
             * Compute the new attribute value
             */
        final String newAttributeValue = EscapedAttributeUtils.escapeAttribute(templateMode, expressionResult == null ? null : expressionResult.toString());
        /*
             * Set the new value, removing the attribute completely if the expression evaluated to null
             */
        if (newAttributeValue == null || newAttributeValue.length() == 0) {
            // We are removing the equivalent attribute name, without the prefix...
            structureHandler.removeAttribute(newAttributeName);
            structureHandler.removeAttribute(attributeName);
        } else {
            // We are setting the equivalent attribute name, without the prefix...
            structureHandler.replaceAttribute(attributeName, newAttributeName, (newAttributeValue == null ? "" : newAttributeValue));
        }
    } catch (final TemplateProcessingException e) {
        // specific because we know exactly what attribute was being executed and caused the error
        if (!e.hasTemplateName()) {
            e.setTemplateName(tag.getTemplateName());
        }
        if (!e.hasLineAndCol()) {
            e.setLineAndCol(attribute.getLine(), attribute.getCol());
        }
        throw e;
    } catch (final Exception e) {
        throw new TemplateProcessingException("Error during execution of processor '" + StandardDefaultAttributesTagProcessor.class.getName() + "'", tag.getTemplateName(), attribute.getLine(), attribute.getCol(), e);
    }
}
Also used : IStandardExpression(org.thymeleaf.standard.expression.IStandardExpression) FragmentExpression(org.thymeleaf.standard.expression.FragmentExpression) IStandardExpressionParser(org.thymeleaf.standard.expression.IStandardExpressionParser) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) MatchingAttributeName(org.thymeleaf.processor.element.MatchingAttributeName) AttributeName(org.thymeleaf.engine.AttributeName) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException)

Example 3 with AttributeName

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

the class StandardDefaultAttributesTagProcessor method process.

// Default implementation - meant to be overridden by subclasses if needed
public void process(final ITemplateContext context, final IProcessableElementTag tag, final IElementTagStructureHandler structureHandler) {
    final TemplateMode templateMode = getTemplateMode();
    final IAttribute[] attributes = tag.getAllAttributes();
    // should not be affected by modifications on the original tag attribute set
    for (final IAttribute attribute : attributes) {
        final AttributeName attributeName = attribute.getAttributeDefinition().getAttributeName();
        if (attributeName.isPrefixed()) {
            if (TextUtil.equals(templateMode.isCaseSensitive(), attributeName.getPrefix(), this.dialectPrefix)) {
                // We will process each 'default' attribute separately
                processDefaultAttribute(getTemplateMode(), context, tag, attribute, structureHandler);
            }
        }
    }
}
Also used : TemplateMode(org.thymeleaf.templatemode.TemplateMode) IAttribute(org.thymeleaf.model.IAttribute) MatchingAttributeName(org.thymeleaf.processor.element.MatchingAttributeName) AttributeName(org.thymeleaf.engine.AttributeName)

Example 4 with AttributeName

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

the class StandardFragmentTagProcessor method doProcess.

@Override
protected void doProcess(final ITemplateContext context, final IProcessableElementTag tag, final IElementTagStructureHandler structureHandler) {
    // Nothing to do, this processor is just a marker. Simply remove the attribute
    final AttributeName attributeName = getMatchingAttributeName().getMatchingAttributeName();
    structureHandler.removeAttribute(attributeName);
}
Also used : AttributeName(org.thymeleaf.engine.AttributeName)

Example 5 with AttributeName

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

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