final Template template = this.templateRepository.getTemplate(templateProcessingParameters);
final TemplateResolution templateResolution = template.getTemplateResolution();
final String templateMode = templateResolution.getTemplateMode();
Document document = template.getDocument();
if (fragmentSpec != null) {
// Apply the fragment specification and filter the parsed template.
final List<Node> processingRootNodes =
fragmentSpec.extractFragment(this.configuration, Collections.singletonList((Node)document));
if (processingRootNodes == null || processingRootNodes.size() == 0) {
// If the result is null, there will be no processing to do
document = null;
} else {
final Node firstProcessingRootNode = processingRootNodes.get(0);
if (processingRootNodes.size() == 1 &&
firstProcessingRootNode != null &&
firstProcessingRootNode instanceof Document) {
// If it is a document, just process it as it is output from the filter
document = (Document) firstProcessingRootNode;
} else {
// Fragment exists and it is not a Document. We will therefore lose DOCTYPE
final String documentName = document.getDocumentName();
document = new Document(documentName);
for (final Node processingRootNode : processingRootNodes) {
if (processingRootNode != null) {
final Node clonedProcessingRootNode =
processingRootNode.cloneNode(document, false);
document.addChild(clonedProcessingRootNode);
}
}
document.precompute(this.configuration);
}
}
}
final Arguments arguments =
new Arguments(this,
templateProcessingParameters, templateResolution,
this.templateRepository, document);
if (logger.isDebugEnabled()) {
logger.debug("[THYMELEAF][{}] Starting process on template \"{}\" using mode \"{}\"",
new Object[] { TemplateEngine.threadIndex(), templateName, templateMode });
}
if (document != null) {
document.process(arguments);
}
if (logger.isDebugEnabled()) {
logger.debug("[THYMELEAF][{}] Finished process on template \"{}\" using mode \"{}\"",
new Object[] { TemplateEngine.threadIndex(), templateName, templateMode });