Search in sources :

Example 1 with MatchingElementName

use of org.thymeleaf.processor.element.MatchingElementName 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 MatchingElementName

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

the class AbstractProcessableElementTag method computeProcessors.

private IElementProcessor[] computeProcessors() {
    final int associatedProcessorCount = (this.attributes != null ? this.attributes.getAssociatedProcessorCount() : 0);
    // If there are no processors associated with attributes, this is much easier
    if (this.attributes == null || associatedProcessorCount == 0) {
        return (this.elementDefinition.hasAssociatedProcessors ? this.elementDefinition.associatedProcessors : EMPTY_ASSOCIATED_PROCESSORS);
    }
    // At this point we know for sure there are processors associated with attributes
    final int elementProcessorCount = (this.elementDefinition.hasAssociatedProcessors ? this.elementDefinition.associatedProcessors.length : 0);
    IElementProcessor[] processors = new IElementProcessor[elementProcessorCount + associatedProcessorCount];
    if (elementProcessorCount > 0) {
        System.arraycopy(this.elementDefinition.associatedProcessors, 0, processors, 0, elementProcessorCount);
    }
    int idx = elementProcessorCount;
    int n = this.attributes.attributes.length;
    while (n-- != 0) {
        if (!this.attributes.attributes[n].definition.hasAssociatedProcessors) {
            continue;
        }
        final IElementProcessor[] attributeAssociatedProcessors = this.attributes.attributes[n].definition.associatedProcessors;
        for (int i = 0; i < attributeAssociatedProcessors.length; i++) {
            // We should never have duplicates. The same attribute can never appear twice in an element (parser
            // restrictions + the way this class's 'setAttribute' works), plus a specific processor instance can
            // never appear in more than one dialect, nor be applied to more than one attribute name.
            // Now for each processor, before adding it to the list, we must first determine whether it requires
            // a specific element name and, if so, confirm that it is the same as the name of the element these
            // attributes live at.
            final MatchingElementName matchingElementName = attributeAssociatedProcessors[i].getMatchingElementName();
            if (matchingElementName != null && !matchingElementName.matches(this.elementDefinition.elementName)) {
                continue;
            }
            // Just add the processor to the list
            processors[idx++] = attributeAssociatedProcessors[i];
        }
    }
    // due to the element name not matching. In such cases, we need to readjust the array size.
    if (idx < processors.length) {
        processors = Arrays.copyOf(processors, idx);
    }
    if (processors.length > 1) {
        Arrays.sort(processors, ProcessorComparators.PROCESSOR_COMPARATOR);
    }
    return processors;
}
Also used : IElementProcessor(org.thymeleaf.processor.element.IElementProcessor) MatchingElementName(org.thymeleaf.processor.element.MatchingElementName)

Example 3 with MatchingElementName

use of org.thymeleaf.processor.element.MatchingElementName 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 4 with MatchingElementName

use of org.thymeleaf.processor.element.MatchingElementName 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 5 with MatchingElementName

use of org.thymeleaf.processor.element.MatchingElementName 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)

Aggregations

MatchingElementName (org.thymeleaf.processor.element.MatchingElementName)8 IElementProcessor (org.thymeleaf.processor.element.IElementProcessor)7 MatchingAttributeName (org.thymeleaf.processor.element.MatchingAttributeName)7 LinkedHashSet (java.util.LinkedHashSet)6 ConfigurationException (org.thymeleaf.exceptions.ConfigurationException)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