Search in sources :

Example 1 with MatchingAttributeName

use of org.thymeleaf.processor.element.MatchingAttributeName in project thymeleaf by thymeleaf.

the class ElementDefinitions method buildTextElementDefinition.

private static TextElementDefinition buildTextElementDefinition(final TemplateMode templateMode, final TextElementName name, final Set<IElementProcessor> elementProcessors) {
    // No need to use a list for sorting - the elementProcessors set has already been ordered
    final Set<IElementProcessor> associatedProcessors = new LinkedHashSet<IElementProcessor>(2);
    if (elementProcessors != null) {
        for (final IElementProcessor processor : elementProcessors) {
            if (processor.getTemplateMode() != templateMode) {
                // We are creating an element definition for a specific template mode
                continue;
            }
            final MatchingElementName matchingElementName = processor.getMatchingElementName();
            final MatchingAttributeName matchingAttributeName = processor.getMatchingAttributeName();
            if ((matchingElementName != null && matchingElementName.getTemplateMode() != templateMode) || (matchingAttributeName != null && matchingAttributeName.getTemplateMode() != templateMode)) {
                throw new ConfigurationException(templateMode + " processors must return " + templateMode + "element names and " + templateMode + " attribute names (processor: " + processor.getClass().getName() + ")");
            }
            if (matchingAttributeName != null && !matchingAttributeName.isMatchingAllAttributes()) {
                // (will be instead associated with the attribute).
                continue;
            }
            if (matchingElementName != null && !matchingElementName.matches(name)) {
                // Note that elementName == null means "apply to all processors"
                continue;
            }
            associatedProcessors.add(processor);
        }
    }
    // Build the final instance
    return new TextElementDefinition(name, associatedProcessors);
}
Also used : IElementProcessor(org.thymeleaf.processor.element.IElementProcessor) LinkedHashSet(java.util.LinkedHashSet) MatchingElementName(org.thymeleaf.processor.element.MatchingElementName) MatchingAttributeName(org.thymeleaf.processor.element.MatchingAttributeName) ConfigurationException(org.thymeleaf.exceptions.ConfigurationException)

Example 2 with MatchingAttributeName

use of org.thymeleaf.processor.element.MatchingAttributeName in project thymeleaf by thymeleaf.

the class ConfigurationPrinterHelper method printProcessorsForTemplateMode.

private static void printProcessorsForTemplateMode(final ConfigLogBuilder logBuilder, final Set<IProcessor> processors, final TemplateMode templateMode) {
    if (processors == null || processors.isEmpty()) {
        return;
    }
    final List<ICDATASectionProcessor> cdataSectionProcessors = new ArrayList<ICDATASectionProcessor>();
    final List<ICommentProcessor> commentProcessors = new ArrayList<ICommentProcessor>();
    final List<IDocTypeProcessor> docTypeProcessors = new ArrayList<IDocTypeProcessor>();
    final List<IElementTagProcessor> elementTagProcessors = new ArrayList<IElementTagProcessor>();
    final List<IElementModelProcessor> elementModelProcessors = new ArrayList<IElementModelProcessor>();
    final List<IProcessingInstructionProcessor> processingInstructionProcessors = new ArrayList<IProcessingInstructionProcessor>();
    final List<ITextProcessor> textProcessors = new ArrayList<ITextProcessor>();
    final List<IXMLDeclarationProcessor> xmlDeclarationProcessors = new ArrayList<IXMLDeclarationProcessor>();
    boolean processorsForTemplateModeExist = false;
    for (final IProcessor processor : processors) {
        if (!templateMode.equals(processor.getTemplateMode())) {
            continue;
        }
        processorsForTemplateModeExist = true;
        if (processor instanceof ICDATASectionProcessor) {
            cdataSectionProcessors.add((ICDATASectionProcessor) processor);
        } else if (processor instanceof ICommentProcessor) {
            commentProcessors.add((ICommentProcessor) processor);
        } else if (processor instanceof IDocTypeProcessor) {
            docTypeProcessors.add((IDocTypeProcessor) processor);
        } else if (processor instanceof IElementTagProcessor) {
            elementTagProcessors.add((IElementTagProcessor) processor);
        } else if (processor instanceof IElementModelProcessor) {
            elementModelProcessors.add((IElementModelProcessor) processor);
        } else if (processor instanceof IProcessingInstructionProcessor) {
            processingInstructionProcessors.add((IProcessingInstructionProcessor) processor);
        } else if (processor instanceof ITextProcessor) {
            textProcessors.add((ITextProcessor) processor);
        } else if (processor instanceof IXMLDeclarationProcessor) {
            xmlDeclarationProcessors.add((IXMLDeclarationProcessor) processor);
        }
    }
    if (!processorsForTemplateModeExist) {
        // Nothing to show, there are no processors for this template mode
        return;
    }
    logBuilder.line("[THYMELEAF]     * Processors for Template Mode: {}", templateMode);
    Collections.sort(cdataSectionProcessors, ProcessorComparators.PROCESSOR_COMPARATOR);
    Collections.sort(commentProcessors, ProcessorComparators.PROCESSOR_COMPARATOR);
    Collections.sort(docTypeProcessors, ProcessorComparators.PROCESSOR_COMPARATOR);
    Collections.sort(elementTagProcessors, ProcessorComparators.PROCESSOR_COMPARATOR);
    Collections.sort(elementModelProcessors, ProcessorComparators.PROCESSOR_COMPARATOR);
    Collections.sort(processingInstructionProcessors, ProcessorComparators.PROCESSOR_COMPARATOR);
    Collections.sort(textProcessors, ProcessorComparators.PROCESSOR_COMPARATOR);
    Collections.sort(xmlDeclarationProcessors, ProcessorComparators.PROCESSOR_COMPARATOR);
    if (!elementTagProcessors.isEmpty()) {
        logBuilder.line("[THYMELEAF]         * Element Tag Processors by [matching element and attribute name] [precedence]:");
        for (final IElementTagProcessor processor : elementTagProcessors) {
            final MatchingElementName matchingElementName = processor.getMatchingElementName();
            final MatchingAttributeName matchingAttributeName = processor.getMatchingAttributeName();
            final String elementName = (matchingElementName == null ? "*" : matchingElementName.toString());
            final String attributeName = (matchingAttributeName == null ? "*" : matchingAttributeName.toString());
            logBuilder.line("[THYMELEAF]             * [{} {}] [{}]: {}", new Object[] { elementName, attributeName, Integer.valueOf(processor.getPrecedence()), processor.getClass().getName() });
        }
    }
    if (!elementModelProcessors.isEmpty()) {
        logBuilder.line("[THYMELEAF]         * Element Model Processors by [matching element and attribute name] [precedence]:");
        for (final IElementModelProcessor processor : elementModelProcessors) {
            final MatchingElementName matchingElementName = processor.getMatchingElementName();
            final MatchingAttributeName matchingAttributeName = processor.getMatchingAttributeName();
            final String elementName = (matchingElementName == null ? "*" : matchingElementName.toString());
            final String attributeName = (matchingAttributeName == null ? "*" : matchingAttributeName.toString());
            logBuilder.line("[THYMELEAF]             * [{} {}] [{}]: {}", new Object[] { elementName, attributeName, Integer.valueOf(processor.getPrecedence()), processor.getClass().getName() });
        }
    }
    if (!textProcessors.isEmpty()) {
        logBuilder.line("[THYMELEAF]         * Text Processors by [precedence]:");
        for (final ITextProcessor processor : textProcessors) {
            logBuilder.line("[THYMELEAF]             * [{}]: {}", new Object[] { Integer.valueOf(processor.getPrecedence()), processor.getClass().getName() });
        }
    }
    if (!docTypeProcessors.isEmpty()) {
        logBuilder.line("[THYMELEAF]         * DOCTYPE Processors by [precedence]:");
        for (final IDocTypeProcessor processor : docTypeProcessors) {
            logBuilder.line("[THYMELEAF]             * [{}]: {}", new Object[] { Integer.valueOf(processor.getPrecedence()), processor.getClass().getName() });
        }
    }
    if (!cdataSectionProcessors.isEmpty()) {
        logBuilder.line("[THYMELEAF]         * CDATA Section Processors by [precedence]:");
        for (final ICDATASectionProcessor processor : cdataSectionProcessors) {
            logBuilder.line("[THYMELEAF]             * [{}]: {}", new Object[] { Integer.valueOf(processor.getPrecedence()), processor.getClass().getName() });
        }
    }
    if (!commentProcessors.isEmpty()) {
        logBuilder.line("[THYMELEAF]         * Comment Processors by [precedence]:");
        for (final ICommentProcessor processor : commentProcessors) {
            logBuilder.line("[THYMELEAF]             * [{}]: {}", new Object[] { Integer.valueOf(processor.getPrecedence()), processor.getClass().getName() });
        }
    }
    if (!xmlDeclarationProcessors.isEmpty()) {
        logBuilder.line("[THYMELEAF]         * XML Declaration Processors by [precedence]:");
        for (final IXMLDeclarationProcessor processor : xmlDeclarationProcessors) {
            logBuilder.line("[THYMELEAF]             * [{}]: {}", new Object[] { Integer.valueOf(processor.getPrecedence()), processor.getClass().getName() });
        }
    }
    if (!processingInstructionProcessors.isEmpty()) {
        logBuilder.line("[THYMELEAF]         * Processing Instruction Processors by [precedence]:");
        for (final IProcessingInstructionProcessor processor : processingInstructionProcessors) {
            logBuilder.line("[THYMELEAF]             * [{}]: {}", new Object[] { Integer.valueOf(processor.getPrecedence()), processor.getClass().getName() });
        }
    }
}
Also used : IElementTagProcessor(org.thymeleaf.processor.element.IElementTagProcessor) ITextProcessor(org.thymeleaf.processor.text.ITextProcessor) MatchingAttributeName(org.thymeleaf.processor.element.MatchingAttributeName) IProcessingInstructionProcessor(org.thymeleaf.processor.processinginstruction.IProcessingInstructionProcessor) ArrayList(java.util.ArrayList) IProcessor(org.thymeleaf.processor.IProcessor) IDocTypeProcessor(org.thymeleaf.processor.doctype.IDocTypeProcessor) ICommentProcessor(org.thymeleaf.processor.comment.ICommentProcessor) MatchingElementName(org.thymeleaf.processor.element.MatchingElementName) IElementModelProcessor(org.thymeleaf.processor.element.IElementModelProcessor) IXMLDeclarationProcessor(org.thymeleaf.processor.xmldeclaration.IXMLDeclarationProcessor) ICDATASectionProcessor(org.thymeleaf.processor.cdatasection.ICDATASectionProcessor)

Example 3 with MatchingAttributeName

use of org.thymeleaf.processor.element.MatchingAttributeName in project thymeleaf by thymeleaf.

the class AttributeDefinitions method buildTextAttributeDefinition.

private static TextAttributeDefinition buildTextAttributeDefinition(final TemplateMode templateMode, final TextAttributeName name, final Set<IElementProcessor> elementProcessors) {
    // No need to use a list for sorting - the elementProcessors set has already been ordered
    final Set<IElementProcessor> associatedProcessors = new LinkedHashSet<IElementProcessor>(2);
    if (elementProcessors != null) {
        for (final IElementProcessor processor : elementProcessors) {
            if (processor.getTemplateMode() != templateMode) {
                // We are creating a text element definition, therefore we are only interested on XML processors
                continue;
            }
            final MatchingElementName matchingElementName = processor.getMatchingElementName();
            final MatchingAttributeName matchingAttributeName = processor.getMatchingAttributeName();
            if ((matchingElementName != null && matchingElementName.getTemplateMode() != templateMode) || (matchingAttributeName != null && matchingAttributeName.getTemplateMode() != templateMode)) {
                throw new ConfigurationException(templateMode + " processors must return " + templateMode + "element names and " + templateMode + " attribute names (processor: " + processor.getClass().getName() + ")");
            }
            if (matchingAttributeName == null || matchingAttributeName.isMatchingAllAttributes()) {
                // This processor does not relate to a specific attribute - surely an element processor
                continue;
            }
            if (!matchingAttributeName.matches(name)) {
                // Doesn't match. This processor is not associated with this attribute
                continue;
            }
            associatedProcessors.add(processor);
        }
    }
    // Build the final instance
    return new TextAttributeDefinition(name, associatedProcessors);
}
Also used : IElementProcessor(org.thymeleaf.processor.element.IElementProcessor) LinkedHashSet(java.util.LinkedHashSet) MatchingElementName(org.thymeleaf.processor.element.MatchingElementName) MatchingAttributeName(org.thymeleaf.processor.element.MatchingAttributeName) ConfigurationException(org.thymeleaf.exceptions.ConfigurationException)

Example 4 with MatchingAttributeName

use of org.thymeleaf.processor.element.MatchingAttributeName in project thymeleaf by thymeleaf.

the class AttributeDefinitions method buildHTMLAttributeDefinition.

private static HTMLAttributeDefinition buildHTMLAttributeDefinition(final HTMLAttributeName name, final Set<IElementProcessor> elementProcessors) {
    // No need to use a list for sorting - the elementProcessors set has already been ordered
    final Set<IElementProcessor> associatedProcessors = new LinkedHashSet<IElementProcessor>(2);
    if (elementProcessors != null) {
        for (final IElementProcessor processor : elementProcessors) {
            // Cannot be null -- has been previously validated
            final TemplateMode templateMode = processor.getTemplateMode();
            if (templateMode != TemplateMode.HTML) {
                // We are creating an HTML element definition, therefore we are only interested on HTML processors
                continue;
            }
            final MatchingElementName matchingElementName = processor.getMatchingElementName();
            final MatchingAttributeName matchingAttributeName = processor.getMatchingAttributeName();
            if ((matchingElementName != null && matchingElementName.getTemplateMode() != TemplateMode.HTML) || (matchingAttributeName != null && matchingAttributeName.getTemplateMode() != TemplateMode.HTML)) {
                throw new ConfigurationException("HTML processors must return HTML element names and HTML attribute names (processor: " + processor.getClass().getName() + ")");
            }
            if (matchingAttributeName == null || matchingAttributeName.isMatchingAllAttributes()) {
                // This processor does not relate to a specific attribute - surely an element processor
                continue;
            }
            if (!matchingAttributeName.matches(name)) {
                // Doesn't match. This processor is not associated with this attribute
                continue;
            }
            associatedProcessors.add(processor);
        }
    }
    // Compute whether this attribute is to be considered boolean or not
    boolean booleanAttribute = false;
    for (final String completeAttributeName : name.getCompleteAttributeNames()) {
        if (ALL_STANDARD_BOOLEAN_HTML_ATTRIBUTE_NAMES.contains(completeAttributeName)) {
            booleanAttribute = true;
        }
    }
    // Build the final instance
    return new HTMLAttributeDefinition(name, booleanAttribute, associatedProcessors);
}
Also used : IElementProcessor(org.thymeleaf.processor.element.IElementProcessor) LinkedHashSet(java.util.LinkedHashSet) MatchingElementName(org.thymeleaf.processor.element.MatchingElementName) TemplateMode(org.thymeleaf.templatemode.TemplateMode) MatchingAttributeName(org.thymeleaf.processor.element.MatchingAttributeName) ConfigurationException(org.thymeleaf.exceptions.ConfigurationException)

Example 5 with MatchingAttributeName

use of org.thymeleaf.processor.element.MatchingAttributeName in project thymeleaf by thymeleaf.

the class AttributeDefinitions method buildXMLAttributeDefinition.

private static XMLAttributeDefinition buildXMLAttributeDefinition(final XMLAttributeName name, final Set<IElementProcessor> elementProcessors) {
    // No need to use a list for sorting - the elementProcessors set has already been ordered
    final Set<IElementProcessor> associatedProcessors = new LinkedHashSet<IElementProcessor>(2);
    if (elementProcessors != null) {
        for (final IElementProcessor processor : elementProcessors) {
            // Cannot be null -- has been previously validated
            final TemplateMode templateMode = processor.getTemplateMode();
            if (templateMode != TemplateMode.XML) {
                // We are creating an XML element definition, therefore we are only interested on XML processors
                continue;
            }
            final MatchingElementName matchingElementName = processor.getMatchingElementName();
            final MatchingAttributeName matchingAttributeName = processor.getMatchingAttributeName();
            if ((matchingElementName != null && matchingElementName.getTemplateMode() != TemplateMode.XML) || (matchingAttributeName != null && matchingAttributeName.getTemplateMode() != TemplateMode.XML)) {
                throw new ConfigurationException("XML processors must return XML element names and XML attribute names (processor: " + processor.getClass().getName() + ")");
            }
            if (matchingAttributeName == null || matchingAttributeName.isMatchingAllAttributes()) {
                // This processor does not relate to a specific attribute - surely an element processor
                continue;
            }
            if (!matchingAttributeName.matches(name)) {
                // Doesn't match. This processor is not associated with this attribute
                continue;
            }
            associatedProcessors.add(processor);
        }
    }
    // Build the final instance
    return new XMLAttributeDefinition(name, associatedProcessors);
}
Also used : IElementProcessor(org.thymeleaf.processor.element.IElementProcessor) LinkedHashSet(java.util.LinkedHashSet) MatchingElementName(org.thymeleaf.processor.element.MatchingElementName) TemplateMode(org.thymeleaf.templatemode.TemplateMode) MatchingAttributeName(org.thymeleaf.processor.element.MatchingAttributeName) ConfigurationException(org.thymeleaf.exceptions.ConfigurationException)

Aggregations

MatchingAttributeName (org.thymeleaf.processor.element.MatchingAttributeName)7 MatchingElementName (org.thymeleaf.processor.element.MatchingElementName)7 LinkedHashSet (java.util.LinkedHashSet)6 ConfigurationException (org.thymeleaf.exceptions.ConfigurationException)6 IElementProcessor (org.thymeleaf.processor.element.IElementProcessor)6 TemplateMode (org.thymeleaf.templatemode.TemplateMode)4 ArrayList (java.util.ArrayList)1 IProcessor (org.thymeleaf.processor.IProcessor)1 ICDATASectionProcessor (org.thymeleaf.processor.cdatasection.ICDATASectionProcessor)1 ICommentProcessor (org.thymeleaf.processor.comment.ICommentProcessor)1 IDocTypeProcessor (org.thymeleaf.processor.doctype.IDocTypeProcessor)1 IElementModelProcessor (org.thymeleaf.processor.element.IElementModelProcessor)1 IElementTagProcessor (org.thymeleaf.processor.element.IElementTagProcessor)1 IProcessingInstructionProcessor (org.thymeleaf.processor.processinginstruction.IProcessingInstructionProcessor)1 ITextProcessor (org.thymeleaf.processor.text.ITextProcessor)1 IXMLDeclarationProcessor (org.thymeleaf.processor.xmldeclaration.IXMLDeclarationProcessor)1