Search in sources :

Example 1 with NodeSelectorMarkupHandler

use of org.attoparser.select.NodeSelectorMarkupHandler in project thymeleaf by thymeleaf.

the class AbstractMarkupTemplateParser method parse.

private void parse(final IEngineConfiguration configuration, final String ownerTemplate, final String template, final Set<String> templateSelectors, final ITemplateResource resource, final int lineOffset, final int colOffset, final TemplateMode templateMode, final boolean useDecoupledLogic, final ITemplateHandler templateHandler) {
    if (templateMode == TemplateMode.HTML) {
        Validate.isTrue(this.html, "Parser is configured as XML, but HTML-mode template parsing is being requested");
    } else if (templateMode == TemplateMode.XML) {
        Validate.isTrue(!this.html, "Parser is configured as HTML, but XML-mode template parsing is being requested");
    } else {
        throw new IllegalArgumentException("Parser is configured as " + (this.html ? "HTML" : "XML") + " but an unsupported template mode " + "has been specified: " + templateMode);
    }
    // For a String template, we will use the ownerTemplate as templateName for its parsed events
    final String templateName = (resource != null ? template : ownerTemplate);
    try {
        // We might need to first check for the existence of decoupled logic in a separate resource
        final DecoupledTemplateLogic decoupledTemplateLogic = (useDecoupledLogic && resource != null ? DecoupledTemplateLogicUtils.computeDecoupledTemplateLogic(configuration, ownerTemplate, template, templateSelectors, resource, templateMode, this.parser) : null);
        // The final step of the handler chain will be the adapter that will convert attoparser's handler chain to thymeleaf's.
        IMarkupHandler handler = new TemplateHandlerAdapterMarkupHandler(templateName, templateHandler, configuration.getElementDefinitions(), configuration.getAttributeDefinitions(), templateMode, lineOffset, colOffset);
        // (which might see text blocks coming as several blocks instead of just one).
        if (configuration instanceof EngineConfiguration && ((EngineConfiguration) configuration).isModelReshapeable(templateMode)) {
            handler = new InlinedOutputExpressionMarkupHandler(configuration, templateMode, configuration.getStandardDialectPrefix(), handler);
        }
        // Precompute flags
        final boolean injectAttributes = decoupledTemplateLogic != null && decoupledTemplateLogic.hasInjectedAttributes();
        final boolean selectBlock = templateSelectors != null && !templateSelectors.isEmpty();
        // Pre-create reference resolver if needed, so that it can be used in both block and node selection
        final TemplateFragmentMarkupReferenceResolver referenceResolver;
        if (injectAttributes || selectBlock) {
            final String standardDialectPrefix = configuration.getStandardDialectPrefix();
            referenceResolver = (standardDialectPrefix != null ? TemplateFragmentMarkupReferenceResolver.forPrefix(this.html, standardDialectPrefix) : null);
        } else {
            referenceResolver = null;
        }
        // will be able to include in selectors code inside prototype-only comments.
        if (selectBlock) {
            handler = new BlockSelectorMarkupHandler(handler, templateSelectors.toArray(new String[templateSelectors.size()]), referenceResolver);
        }
        // comment block readers, so that we will be able to include in selectors code inside prototype-only comments.
        if (injectAttributes) {
            // This handler will be in charge of really injecting the attributes, reacting to the node-selection
            // signals sent by the NodeSelectorMarkupHandler configured below
            handler = new DecoupledTemplateLogicMarkupHandler(decoupledTemplateLogic, handler);
            // NOTE it is important that THIS IS THE FIRST NODE- OR BLOCK-SELECTION HANDLER TO BE APPLIED because
            // structures in the DecoupledTemplateLogicMarkupHandler will consider 0 (zero) as their injection
            // level of interest
            final Set<String> nodeSelectors = decoupledTemplateLogic.getAllInjectedAttributeSelectors();
            handler = new NodeSelectorMarkupHandler(handler, handler, nodeSelectors.toArray(new String[nodeSelectors.size()]), referenceResolver);
        }
        // Obtain the resource reader
        Reader templateReader = (resource != null ? resource.reader() : new StringReader(template));
        // Add the required reader wrappers in order to process parser-level and prototype-only comment blocks
        templateReader = new ParserLevelCommentMarkupReader(new PrototypeOnlyCommentMarkupReader(templateReader));
        this.parser.parse(templateReader, handler);
    } catch (final IOException e) {
        final String message = "An error happened during template parsing";
        throw new TemplateInputException(message, (resource != null ? resource.getDescription() : template), e);
    } catch (final ParseException e) {
        final String message = "An error happened during template parsing";
        if (e.getLine() != null && e.getCol() != null) {
            throw new TemplateInputException(message, (resource != null ? resource.getDescription() : template), e.getLine().intValue(), e.getCol().intValue(), e);
        }
        throw new TemplateInputException(message, (resource != null ? resource.getDescription() : template), e);
    }
}
Also used : DecoupledTemplateLogic(org.thymeleaf.templateparser.markup.decoupled.DecoupledTemplateLogic) NodeSelectorMarkupHandler(org.attoparser.select.NodeSelectorMarkupHandler) PrototypeOnlyCommentMarkupReader(org.thymeleaf.templateparser.reader.PrototypeOnlyCommentMarkupReader) Reader(java.io.Reader) StringReader(java.io.StringReader) ParserLevelCommentMarkupReader(org.thymeleaf.templateparser.reader.ParserLevelCommentMarkupReader) IOException(java.io.IOException) TemplateInputException(org.thymeleaf.exceptions.TemplateInputException) IMarkupHandler(org.attoparser.IMarkupHandler) TemplateHandlerAdapterMarkupHandler(org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) EngineConfiguration(org.thymeleaf.EngineConfiguration) StringReader(java.io.StringReader) ParserLevelCommentMarkupReader(org.thymeleaf.templateparser.reader.ParserLevelCommentMarkupReader) PrototypeOnlyCommentMarkupReader(org.thymeleaf.templateparser.reader.PrototypeOnlyCommentMarkupReader) ParseException(org.attoparser.ParseException) DecoupledTemplateLogicMarkupHandler(org.thymeleaf.templateparser.markup.decoupled.DecoupledTemplateLogicMarkupHandler) BlockSelectorMarkupHandler(org.attoparser.select.BlockSelectorMarkupHandler)

Aggregations

IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 IMarkupHandler (org.attoparser.IMarkupHandler)1 ParseException (org.attoparser.ParseException)1 BlockSelectorMarkupHandler (org.attoparser.select.BlockSelectorMarkupHandler)1 NodeSelectorMarkupHandler (org.attoparser.select.NodeSelectorMarkupHandler)1 EngineConfiguration (org.thymeleaf.EngineConfiguration)1 IEngineConfiguration (org.thymeleaf.IEngineConfiguration)1 TemplateHandlerAdapterMarkupHandler (org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler)1 TemplateInputException (org.thymeleaf.exceptions.TemplateInputException)1 DecoupledTemplateLogic (org.thymeleaf.templateparser.markup.decoupled.DecoupledTemplateLogic)1 DecoupledTemplateLogicMarkupHandler (org.thymeleaf.templateparser.markup.decoupled.DecoupledTemplateLogicMarkupHandler)1 ParserLevelCommentMarkupReader (org.thymeleaf.templateparser.reader.ParserLevelCommentMarkupReader)1 PrototypeOnlyCommentMarkupReader (org.thymeleaf.templateparser.reader.PrototypeOnlyCommentMarkupReader)1