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;
}
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;
}
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);
}
}
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;
}
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());
}
Aggregations