Search in sources :

Example 1 with PrototypeOnlyCommentTextReader

use of org.thymeleaf.templateparser.reader.PrototypeOnlyCommentTextReader in project thymeleaf by thymeleaf.

the class AbstractTextTemplateParser 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 ITemplateHandler templateHandler) {
    // For a String template, we will use the ownerTemplate as templateName for its parsed events
    final String templateName = (resource != null ? template : ownerTemplate);
    try {
        // The final step of the handler chain will be the adapter that will convert the text parser's handler chain to thymeleaf's.
        ITextHandler handler = new TemplateHandlerAdapterTextHandler(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 InlinedOutputExpressionTextHandler(configuration, templateMode, configuration.getStandardDialectPrefix(), handler);
        }
        // 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
        if (templateMode == TemplateMode.TEXT) {
            // There are no /*[+...+]*/ blocks in TEXT mode (it makes no sense)
            templateReader = new ParserLevelCommentTextReader(templateReader);
        } else {
            // TemplateMode.JAVASCRIPT || TemplateMode.CSS
            templateReader = new ParserLevelCommentTextReader(new PrototypeOnlyCommentTextReader(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 TextParseException 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 : ParserLevelCommentTextReader(org.thymeleaf.templateparser.reader.ParserLevelCommentTextReader) PrototypeOnlyCommentTextReader(org.thymeleaf.templateparser.reader.PrototypeOnlyCommentTextReader) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) EngineConfiguration(org.thymeleaf.EngineConfiguration) StringReader(java.io.StringReader) ParserLevelCommentTextReader(org.thymeleaf.templateparser.reader.ParserLevelCommentTextReader) Reader(java.io.Reader) StringReader(java.io.StringReader) PrototypeOnlyCommentTextReader(org.thymeleaf.templateparser.reader.PrototypeOnlyCommentTextReader) IOException(java.io.IOException) TemplateHandlerAdapterTextHandler(org.thymeleaf.engine.TemplateHandlerAdapterTextHandler) TemplateInputException(org.thymeleaf.exceptions.TemplateInputException)

Aggregations

IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 EngineConfiguration (org.thymeleaf.EngineConfiguration)1 IEngineConfiguration (org.thymeleaf.IEngineConfiguration)1 TemplateHandlerAdapterTextHandler (org.thymeleaf.engine.TemplateHandlerAdapterTextHandler)1 TemplateInputException (org.thymeleaf.exceptions.TemplateInputException)1 ParserLevelCommentTextReader (org.thymeleaf.templateparser.reader.ParserLevelCommentTextReader)1 PrototypeOnlyCommentTextReader (org.thymeleaf.templateparser.reader.PrototypeOnlyCommentTextReader)1