org.thymeleaf.templateparser.html.AbstractHtmlTemplateParser Maven / Gradle / Ivy
/*
* =============================================================================
*
* Copyright (c) 2011-2012, The THYMELEAF team (http://www.thymeleaf.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/
package org.thymeleaf.templateparser.html;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.thymeleaf.Configuration;
import org.thymeleaf.dom.Document;
import org.thymeleaf.dom.Node;
import org.thymeleaf.exceptions.ConfigurationException;
import org.thymeleaf.exceptions.TemplateInputException;
import org.thymeleaf.exceptions.TemplateProcessingException;
import org.thymeleaf.templateparser.EntityResolver;
import org.thymeleaf.templateparser.ErrorHandler;
import org.thymeleaf.templateparser.ITemplateParser;
import org.thymeleaf.templateparser.TemplatePreprocessingReader;
import org.thymeleaf.util.ClassLoaderUtils;
import org.thymeleaf.util.ResourcePool;
import org.thymeleaf.util.StandardDOMTranslator;
import org.xml.sax.InputSource;
/**
*
* Document parser implementation for non-XML HTML documents.
*
*
* @since 2.0.0
*
* @author Daniel Fernández
*/
public abstract class AbstractHtmlTemplateParser implements ITemplateParser {
private static final int BUFFER_SIZE = 8192;
private final String templateModeName;
private final boolean nekoInClasspath;
private final NekoBasedHtmlParser parser;
protected AbstractHtmlTemplateParser(final String templateModeName, int poolSize) {
super();
boolean nekoFound = true;
try {
ClassLoaderUtils.getClassLoader(AbstractHtmlTemplateParser.class).
loadClass("org.cyberneko.html.parsers.DOMParser");
} catch (final ClassNotFoundException ignored) {
nekoFound = false;
} catch (final NoClassDefFoundError ignored) {
nekoFound = false;
}
this.nekoInClasspath = nekoFound;
this.templateModeName = templateModeName;
if (this.nekoInClasspath) {
this.parser = new NekoBasedHtmlParser(poolSize);
} else {
this.parser = null;
}
}
public final Document parseTemplate(final Configuration configuration, final String documentName, final Reader reader) {
if (!this.nekoInClasspath) {
throw new ConfigurationException(
"Cannot perform conversion to XML from legacy HTML: The nekoHTML library " +
"is not in classpath. nekoHTML 1.9.15 or newer is required for processing templates in " +
"\"" + this.templateModeName + "\" mode [http://nekohtml.sourceforge.net]. Maven spec: " +
"\"net.sourceforge.nekohtml::nekohtml::1.9.15\". IMPORTANT: DO NOT use versions of " +
"nekoHTML older than 1.9.15.");
}
return this.parser.parseTemplate(configuration, documentName, getTemplatePreprocessingReader(reader));
}
public final List parseFragment(final Configuration configuration, final String fragment) {
final String wrappedFragment = wrapFragment(fragment);
final Document document =
parseTemplate(
configuration,
null, // documentName
new StringReader(wrappedFragment));
return unwrapFragment(document);
}
protected abstract String wrapFragment(final String fragment);
protected abstract List unwrapFragment(final Document document);
/*
* This is defined in a class apart so that the classloader does not always try to load
* neko and xerces classes that might not be in the classpath.
*/
private static class NekoBasedHtmlParser {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
// The org.apache.xerces.parsers.DOMParser is not used here as a type
// parameter to avoid the class loader to try to load this xerces class
// (and fail) before we control the error at the constructor.
private final ResourcePool
© 2015 - 2025 Weber Informatics LLC | Privacy Policy