Search in sources :

Example 1 with TemplateManager

use of org.thymeleaf.engine.TemplateManager in project thymeleaf by thymeleaf.

the class AbstractStandardInliner method inlineSwitchTemplateMode.

private CharSequence inlineSwitchTemplateMode(final ITemplateContext context, final IComment comment) {
    final TemplateManager templateManager = context.getConfiguration().getTemplateManager();
    /*
         * Notice we are ONLY processing the contents of the Comment, because we know the target inlining
         * mode will not understand the Comment (it will be textual) and we don't want it to mess around with
         * the Comment's prefix and suffix.
         *
         * Note this will only be executed in markup modes (textual modes never fire "handleComment" events),
         * so we are safe assuming the sizes of Comment prefixes and suffixes in HTML/XML.
         */
    final TemplateModel templateModel = templateManager.parseString(context.getTemplateData(), comment.getContent(), // +4 because of the prefix
    comment.getLine(), // +4 because of the prefix
    comment.getCol() + 4, this.templateMode, true);
    final Writer stringWriter = new FastStringWriter(50);
    templateManager.process(templateModel, context, stringWriter);
    return stringWriter.toString();
}
Also used : FastStringWriter(org.thymeleaf.util.FastStringWriter) TemplateManager(org.thymeleaf.engine.TemplateManager) TemplateModel(org.thymeleaf.engine.TemplateModel) FastStringWriter(org.thymeleaf.util.FastStringWriter) Writer(java.io.Writer)

Example 2 with TemplateManager

use of org.thymeleaf.engine.TemplateManager in project thymeleaf by thymeleaf.

the class AbstractStandardInliner method inlineSwitchTemplateMode.

private CharSequence inlineSwitchTemplateMode(final ITemplateContext context, final IText text) {
    final TemplateManager templateManager = context.getConfiguration().getTemplateManager();
    final TemplateModel templateModel = templateManager.parseString(context.getTemplateData(), text.getText(), text.getLine(), text.getCol(), this.templateMode, true);
    if (!this.writeTextsToOutput) {
        final Writer stringWriter = new FastStringWriter(50);
        templateManager.process(templateModel, context, stringWriter);
        return stringWriter.toString();
    }
    // If we can directly write to output (and text is an IText), we will use a LazyProcessingCharSequence
    return new LazyProcessingCharSequence(context, templateModel);
}
Also used : LazyProcessingCharSequence(org.thymeleaf.util.LazyProcessingCharSequence) FastStringWriter(org.thymeleaf.util.FastStringWriter) TemplateManager(org.thymeleaf.engine.TemplateManager) TemplateModel(org.thymeleaf.engine.TemplateModel) FastStringWriter(org.thymeleaf.util.FastStringWriter) Writer(java.io.Writer)

Example 3 with TemplateManager

use of org.thymeleaf.engine.TemplateManager in project thymeleaf by thymeleaf.

the class TemplateEngine method processThrottled.

public final IThrottledTemplateProcessor processThrottled(final TemplateSpec templateSpec, final IContext context) {
    if (!this.initialized) {
        initialize();
    }
    final IThrottledTemplateProcessor throttledTemplateProcessor;
    try {
        Validate.notNull(templateSpec, "Template Specification cannot be null");
        Validate.notNull(context, "Context cannot be null");
        if (logger.isTraceEnabled()) {
            logger.trace("[THYMELEAF][{}] STARTING PREPARATION OF THROTTLED TEMPLATE \"{}\" WITH LOCALE {}", new Object[] { TemplateEngine.threadIndex(), templateSpec, context.getLocale() });
        }
        final long startNanos = System.nanoTime();
        final TemplateManager templateManager = this.configuration.getTemplateManager();
        throttledTemplateProcessor = templateManager.parseAndProcessThrottled(templateSpec, context);
        final long endNanos = System.nanoTime();
        if (logger.isTraceEnabled()) {
            logger.trace("[THYMELEAF][{}] FINISHED PREPARATION OF THROTTLED TEMPLATE \"{}\" WITH LOCALE {}", new Object[] { TemplateEngine.threadIndex(), templateSpec, context.getLocale() });
        }
        if (timerLogger.isTraceEnabled()) {
            final BigDecimal elapsed = BigDecimal.valueOf(endNanos - startNanos);
            final BigDecimal elapsedMs = elapsed.divide(BigDecimal.valueOf(NANOS_IN_SECOND), RoundingMode.HALF_UP);
            timerLogger.trace("[THYMELEAF][{}][{}][{}][{}][{}] TEMPLATE \"{}\" WITH LOCALE {} PREPARED FOR THROTTLED PROCESSING IN {} nanoseconds (approx. {}ms)", new Object[] { TemplateEngine.threadIndex(), LoggingUtils.loggifyTemplateName(templateSpec.getTemplate()), context.getLocale(), elapsed, elapsedMs, templateSpec, context.getLocale(), elapsed, elapsedMs });
        }
    } catch (final TemplateOutputException e) {
        // We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
        logger.error(String.format("[THYMELEAF][%s] Exception preparing throttled template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), templateSpec, e.getMessage() }), e);
        throw e;
    } catch (final TemplateEngineException e) {
        // We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
        logger.error(String.format("[THYMELEAF][%s] Exception preparing throttled template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), templateSpec, e.getMessage() }), e);
        throw e;
    } catch (final RuntimeException e) {
        // We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
        logger.error(String.format("[THYMELEAF][%s] Exception preparing throttled template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), templateSpec, e.getMessage() }), e);
        throw new TemplateProcessingException("Exception preparing throttled template", templateSpec.toString(), e);
    }
    return throttledTemplateProcessor;
}
Also used : TemplateManager(org.thymeleaf.engine.TemplateManager) TemplateOutputException(org.thymeleaf.exceptions.TemplateOutputException) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) TemplateEngineException(org.thymeleaf.exceptions.TemplateEngineException) BigDecimal(java.math.BigDecimal)

Example 4 with TemplateManager

use of org.thymeleaf.engine.TemplateManager in project thymeleaf by thymeleaf.

the class TemplateEngine method process.

public final void process(final TemplateSpec templateSpec, final IContext context, final Writer writer) {
    if (!this.initialized) {
        initialize();
    }
    try {
        Validate.notNull(templateSpec, "Template Specification cannot be null");
        Validate.notNull(context, "Context cannot be null");
        Validate.notNull(writer, "Writer cannot be null");
        if (logger.isTraceEnabled()) {
            logger.trace("[THYMELEAF][{}] STARTING PROCESS OF TEMPLATE \"{}\" WITH LOCALE {}", new Object[] { TemplateEngine.threadIndex(), templateSpec, context.getLocale() });
        }
        final long startNanos = System.nanoTime();
        final TemplateManager templateManager = this.configuration.getTemplateManager();
        templateManager.parseAndProcess(templateSpec, context, writer);
        final long endNanos = System.nanoTime();
        if (logger.isTraceEnabled()) {
            logger.trace("[THYMELEAF][{}] FINISHED PROCESS AND OUTPUT OF TEMPLATE \"{}\" WITH LOCALE {}", new Object[] { TemplateEngine.threadIndex(), templateSpec, context.getLocale() });
        }
        if (timerLogger.isTraceEnabled()) {
            final BigDecimal elapsed = BigDecimal.valueOf(endNanos - startNanos);
            final BigDecimal elapsedMs = elapsed.divide(BigDecimal.valueOf(NANOS_IN_SECOND), RoundingMode.HALF_UP);
            timerLogger.trace("[THYMELEAF][{}][{}][{}][{}][{}] TEMPLATE \"{}\" WITH LOCALE {} PROCESSED IN {} nanoseconds (approx. {}ms)", new Object[] { TemplateEngine.threadIndex(), LoggingUtils.loggifyTemplateName(templateSpec.getTemplate()), context.getLocale(), elapsed, elapsedMs, templateSpec, context.getLocale(), elapsed, elapsedMs });
        }
        /*
             * Finally, flush the writer in order to make sure that everything has been written to output
             */
        try {
            writer.flush();
        } catch (final IOException e) {
            throw new TemplateOutputException("An error happened while flushing output writer", templateSpec.getTemplate(), -1, -1, e);
        }
    } catch (final TemplateOutputException e) {
        // We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
        logger.error(String.format("[THYMELEAF][%s] Exception processing template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), templateSpec, e.getMessage() }), e);
        throw e;
    } catch (final TemplateEngineException e) {
        // We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
        logger.error(String.format("[THYMELEAF][%s] Exception processing template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), templateSpec, e.getMessage() }), e);
        throw e;
    } catch (final RuntimeException e) {
        // We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
        logger.error(String.format("[THYMELEAF][%s] Exception processing template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), templateSpec, e.getMessage() }), e);
        throw new TemplateProcessingException("Exception processing template", templateSpec.toString(), e);
    }
}
Also used : TemplateManager(org.thymeleaf.engine.TemplateManager) TemplateOutputException(org.thymeleaf.exceptions.TemplateOutputException) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) TemplateEngineException(org.thymeleaf.exceptions.TemplateEngineException) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal)

Example 5 with TemplateManager

use of org.thymeleaf.engine.TemplateManager in project thymeleaf by thymeleaf.

the class AbstractStandardInliner method inlineSwitchTemplateMode.

private CharSequence inlineSwitchTemplateMode(final ITemplateContext context, final ICDATASection cdataSection) {
    final TemplateManager templateManager = context.getConfiguration().getTemplateManager();
    /*
         * Notice we are ONLY processing the contents of the CDATA, because we know the target inlining
         * mode will not understand the CDATA (it will be textual) and we don't want it to mess around with
         * the CDATA's prefix and suffix.
         *
         * Note this will only be executed in markup modes (textual modes never fire "handleCDATASection" events),
         * so we are safe assuming the sizes of CDATA prefixes and suffixes in HTML/XML.
         */
    final TemplateModel templateModel = templateManager.parseString(context.getTemplateData(), cdataSection.getContent(), // +9 because of the prefix
    cdataSection.getLine(), // +9 because of the prefix
    cdataSection.getCol() + 9, this.templateMode, true);
    final Writer stringWriter = new FastStringWriter(50);
    templateManager.process(templateModel, context, stringWriter);
    return stringWriter.toString();
}
Also used : FastStringWriter(org.thymeleaf.util.FastStringWriter) TemplateManager(org.thymeleaf.engine.TemplateManager) TemplateModel(org.thymeleaf.engine.TemplateModel) FastStringWriter(org.thymeleaf.util.FastStringWriter) Writer(java.io.Writer)

Aggregations

TemplateManager (org.thymeleaf.engine.TemplateManager)6 TemplateModel (org.thymeleaf.engine.TemplateModel)4 FastStringWriter (org.thymeleaf.util.FastStringWriter)4 Writer (java.io.Writer)3 BigDecimal (java.math.BigDecimal)2 TemplateEngineException (org.thymeleaf.exceptions.TemplateEngineException)2 TemplateOutputException (org.thymeleaf.exceptions.TemplateOutputException)2 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)2 IOException (java.io.IOException)1 StandardConditionalCommentUtils (org.thymeleaf.standard.util.StandardConditionalCommentUtils)1 LazyProcessingCharSequence (org.thymeleaf.util.LazyProcessingCharSequence)1