protected final Document parseTemplateUsingPool(final Configuration configuration, final String documentName,
final Reader reader, final ResourcePool<DocumentBuilder> poolToBeUsed) {
final DocumentBuilder docBuilder = poolToBeUsed.allocate();
final TemplatePreprocessingReader templateReader = getTemplatePreprocessingReader(reader);
try {
docBuilder.setEntityResolver(new EntityResolver(configuration));
docBuilder.setErrorHandler(ErrorHandler.INSTANCE);
/*
* Really parse the document
*/
final org.w3c.dom.Document domDocument = docBuilder.parse(new InputSource(templateReader));
if (this.canResetParsers) {
try {
/*
* Reset the parser so that it can be used again.
*/
docBuilder.reset();
} catch (final UnsupportedOperationException ignored) {
if (this.logger.isWarnEnabled()) {
this.logger.warn(
"[THYMELEAF] The DOM Parser implementation being used (\"{}\") does not implement " +
"the \"reset\" operation. This will force Thymeleaf to re-create parser instances " +
"each time they are needed for parsing templates, which is more costly. Enabling template " +
"cache is recommended, and also using a parser library which implements \"reset\" such as " +
"xerces version 2.9.1 or newer.",
docBuilder.getClass().getName());
}
this.canResetParsers = false;
}
}
return StandardDOMTranslator.translateDocument(domDocument, documentName, templateReader.getDocTypeClause());
} catch (final SAXException e) {
if(e.getMessage() != null &&
e.getMessage().contains(SAXPARSEEXCEPTION_BAD_ELEMENT_CONTENT)) {
throw new TemplateInputException(
SAXPARSEEXCEPTION_BAD_ELEMENT_CONTENT_EXPLANATION, e);
}
throw new TemplateInputException("An exception happened during parsing", e);
} catch (final IOException e) {
throw new TemplateInputException("Exception parsing document", e);
} catch (final TemplateProcessingException e) {
throw e;
} catch (final Exception e) {
throw new TemplateInputException("Exception parsing document", e);
} finally {
if (templateReader != null) {
try {
templateReader.close();
} catch (final Exception ignored) {
// ignored
}
}