use of org.attoparser.select.BlockSelectorMarkupHandler 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);
}
}
Aggregations