Search in sources :

Example 21 with IEngineConfiguration

use of org.thymeleaf.IEngineConfiguration in project thymeleaf by thymeleaf.

the class AssignationUtils method parseAssignationSequence.

public static AssignationSequence parseAssignationSequence(final IExpressionContext context, final String input, final boolean allowParametersWithoutValue) {
    Validate.notNull(context, "Context cannot be null");
    Validate.notNull(input, "Input cannot be null");
    final String preprocessedInput = StandardExpressionPreprocessor.preprocess(context, input);
    final IEngineConfiguration configuration = context.getConfiguration();
    if (configuration != null) {
        final AssignationSequence cachedAssignationSequence = ExpressionCache.getAssignationSequenceFromCache(configuration, preprocessedInput);
        if (cachedAssignationSequence != null) {
            return cachedAssignationSequence;
        }
    }
    final AssignationSequence assignationSequence = internalParseAssignationSequence(preprocessedInput.trim(), allowParametersWithoutValue);
    if (assignationSequence == null) {
        throw new TemplateProcessingException("Could not parse as assignation sequence: \"" + input + "\"");
    }
    if (configuration != null) {
        ExpressionCache.putAssignationSequenceIntoCache(configuration, preprocessedInput, assignationSequence);
    }
    return assignationSequence;
}
Also used : IEngineConfiguration(org.thymeleaf.IEngineConfiguration) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException)

Example 22 with IEngineConfiguration

use of org.thymeleaf.IEngineConfiguration in project thymeleaf by thymeleaf.

the class StandardExpressionParser method parseExpression.

static IStandardExpression parseExpression(final IExpressionContext context, final String input, final boolean preprocess) {
    final IEngineConfiguration configuration = context.getConfiguration();
    final String preprocessedInput = (preprocess ? StandardExpressionPreprocessor.preprocess(context, input) : input);
    final IStandardExpression cachedExpression = ExpressionCache.getExpressionFromCache(configuration, preprocessedInput);
    if (cachedExpression != null) {
        return cachedExpression;
    }
    final Expression expression = Expression.parse(preprocessedInput.trim());
    if (expression == null) {
        throw new TemplateProcessingException("Could not parse as expression: \"" + input + "\"");
    }
    ExpressionCache.putExpressionIntoCache(configuration, preprocessedInput, expression);
    return expression;
}
Also used : IEngineConfiguration(org.thymeleaf.IEngineConfiguration) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException)

Example 23 with IEngineConfiguration

use of org.thymeleaf.IEngineConfiguration in project sling by apache.

the class SlingIncludeAttributeTagProcessor method doProcess.

@Override
protected void doProcess(final ITemplateContext templateContext, final IProcessableElementTag processableElementTag, final AttributeName attributeName, final String attributeValue, final IElementTagStructureHandler elementTagStructureHandler) {
    try {
        final SlingHttpServletRequest slingHttpServletRequest = (SlingHttpServletRequest) templateContext.getVariable(SlingBindings.REQUEST);
        final SlingHttpServletResponse slingHttpServletResponse = (SlingHttpServletResponse) templateContext.getVariable(SlingBindings.RESPONSE);
        final IEngineConfiguration configuration = templateContext.getConfiguration();
        final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(configuration);
        final IStandardExpression expression = expressionParser.parseExpression(templateContext, attributeValue);
        final Object include = expression.execute(templateContext);
        String path = null;
        if (include instanceof String) {
            path = (String) include;
        }
        Resource resource = null;
        if (include instanceof Resource) {
            resource = (Resource) include;
        }
        // request dispatcher options
        final RequestDispatcherOptions requestDispatcherOptions = prepareRequestDispatcherOptions(expressionParser, templateContext, processableElementTag, elementTagStructureHandler);
        // dispatch
        final String content = dispatch(resource, path, slingHttpServletRequest, slingHttpServletResponse, requestDispatcherOptions);
        // add output
        final Boolean unwrap = (Boolean) parseAttribute(expressionParser, templateContext, processableElementTag, elementTagStructureHandler, UNWRAP_ATTRIBUTE_NAME);
        if (unwrap != null && unwrap) {
            elementTagStructureHandler.replaceWith(content, false);
        } else {
            elementTagStructureHandler.setBody(content, false);
        }
    } catch (Exception e) {
        throw new RuntimeException("unable to process include attribute", e);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) IStandardExpression(org.thymeleaf.standard.expression.IStandardExpression) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) IStandardExpressionParser(org.thymeleaf.standard.expression.IStandardExpressionParser) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 24 with IEngineConfiguration

use of org.thymeleaf.IEngineConfiguration in project sling by apache.

the class TemplateManager method createTemplateProcessingHandlerChain.

private static ITemplateHandler createTemplateProcessingHandlerChain(final IEngineContext context, final boolean setPreProcessors, final boolean setPostProcessors, final ITemplateHandler handler, final Writer writer) {
    final IEngineConfiguration configuration = context.getConfiguration();
    /*
         * Declare the pair of pointers that will allow us to build the chain of template handlers
         */
    ITemplateHandler firstHandler = null;
    ITemplateHandler lastHandler = null;
    /*
         * First type of handlers to be added: pre-processors (if any)
         */
    if (setPreProcessors) {
        final Set<IPreProcessor> preProcessors = configuration.getPreProcessors(context.getTemplateMode());
        if (preProcessors != null && preProcessors.size() > 0) {
            for (final IPreProcessor preProcessor : preProcessors) {
                final Class<? extends ITemplateHandler> preProcessorClass = preProcessor.getHandlerClass();
                final ITemplateHandler preProcessorHandler;
                try {
                    preProcessorHandler = preProcessorClass.newInstance();
                } catch (final Exception e) {
                    // This should never happen - class was already checked during configuration to contain a zero-arg constructor
                    throw new TemplateProcessingException("An exception happened during the creation of a new instance of pre-processor " + preProcessorClass.getClass().getName(), e);
                }
                // Initialize the pre-processor
                preProcessorHandler.setContext(context);
                if (firstHandler == null) {
                    firstHandler = preProcessorHandler;
                    lastHandler = preProcessorHandler;
                } else {
                    lastHandler.setNext(preProcessorHandler);
                    lastHandler = preProcessorHandler;
                }
            }
        }
    }
    /*
         * Initialize and add to the chain te Processor Handler itself, the central piece of the chain
         */
    handler.setContext(context);
    if (firstHandler == null) {
        firstHandler = handler;
        lastHandler = handler;
    } else {
        lastHandler.setNext(handler);
        lastHandler = handler;
    }
    /*
         * After the Processor Handler, we now must add the post-processors (if any)
         */
    if (setPostProcessors) {
        final Set<IPostProcessor> postProcessors = configuration.getPostProcessors(context.getTemplateMode());
        if (postProcessors != null && postProcessors.size() > 0) {
            for (final IPostProcessor postProcessor : postProcessors) {
                final Class<? extends ITemplateHandler> postProcessorClass = postProcessor.getHandlerClass();
                final ITemplateHandler postProcessorHandler;
                try {
                    postProcessorHandler = postProcessorClass.newInstance();
                } catch (final Exception e) {
                    // This should never happen - class was already checked during configuration to contain a zero-arg constructor
                    throw new TemplateProcessingException("An exception happened during the creation of a new instance of post-processor " + postProcessorClass.getClass().getName(), e);
                }
                // Initialize the pre-processor
                postProcessorHandler.setContext(context);
                if (firstHandler == null) {
                    firstHandler = postProcessorHandler;
                    lastHandler = postProcessorHandler;
                } else {
                    lastHandler.setNext(postProcessorHandler);
                    lastHandler = postProcessorHandler;
                }
            }
        }
    }
    /*
         * Last step: the OUTPUT HANDLER
         */
    if (writer != null) {
        final OutputTemplateHandler outputHandler = new OutputTemplateHandler(writer);
        outputHandler.setContext(context);
        if (firstHandler == null) {
            firstHandler = outputHandler;
        } else {
            lastHandler.setNext(outputHandler);
        }
    }
    return firstHandler;
}
Also used : IEngineConfiguration(org.thymeleaf.IEngineConfiguration) IPostProcessor(org.thymeleaf.postprocessor.IPostProcessor) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) IPreProcessor(org.thymeleaf.preprocessor.IPreProcessor) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) TemplateInputException(org.thymeleaf.exceptions.TemplateInputException)

Example 25 with IEngineConfiguration

use of org.thymeleaf.IEngineConfiguration in project thymeleaf-tests by thymeleaf.

the class ParsingDecoupled01Test method testParsingDecoupled.

private static void testParsingDecoupled(final String decoupledTemplate, final TemplateMode templateMode, final String expectedResult) throws Exception {
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setPrefix("templateparser/markup/");
    templateResolver.setSuffix(templateMode == TemplateMode.HTML ? ".html" : ".xml");
    templateResolver.setTemplateMode(templateMode);
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    // We only to this in order to initialize the engine
    templateEngine.process("parsingdecoupled", new Context());
    final IEngineConfiguration configuration = templateEngine.getConfiguration();
    final TemplateResolution templateResolution = templateResolver.resolveTemplate(configuration, null, decoupledTemplate, null);
    final ITemplateResource templateResource = templateResolution.getTemplateResource();
    final DecoupledTemplateLogic decoupledTemplateLogic = DecoupledTemplateLogicUtils.computeDecoupledTemplateLogic(configuration, null, decoupledTemplate, null, templateResource, templateMode, (templateMode == TemplateMode.HTML ? htmlParser : xmlParser));
    Assert.assertEquals(expectedResult, decoupledTemplateLogic.toString());
}
Also used : Context(org.thymeleaf.context.Context) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) TemplateEngine(org.thymeleaf.TemplateEngine) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) DecoupledTemplateLogic(org.thymeleaf.templateparser.markup.decoupled.DecoupledTemplateLogic) TemplateResolution(org.thymeleaf.templateresolver.TemplateResolution) ITemplateResource(org.thymeleaf.templateresource.ITemplateResource)

Aggregations

IEngineConfiguration (org.thymeleaf.IEngineConfiguration)45 Test (org.junit.Test)26 LinkedHashMap (java.util.LinkedHashMap)15 ServletContext (javax.servlet.ServletContext)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)14 WebEngineContext (org.thymeleaf.context.WebEngineContext)14 EngineContext (org.thymeleaf.context.EngineContext)10 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)8 TemplateInputException (org.thymeleaf.exceptions.TemplateInputException)6 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 EngineConfiguration (org.thymeleaf.EngineConfiguration)3 TemplateModel (org.thymeleaf.engine.TemplateModel)3 IPostProcessor (org.thymeleaf.postprocessor.IPostProcessor)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2