Search in sources :

Example 1 with Configuration

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

the class SpringResourceResourceResolverSpring4Test method testGetResourceAsStream.

@Test
public void testGetResourceAsStream() throws Exception {
    final String templateLocation = "spring421/view/test.html";
    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring421/view/applicationContext.xml");
    final SpringResourceResourceResolver resolver = (SpringResourceResourceResolver) context.getBean("springResourceResourceResolver");
    final TemplateProcessingParameters parameters = new TemplateProcessingParameters(new Configuration(), "test", new Context());
    final InputStream is = resolver.getResourceAsStream(parameters, "classpath:" + templateLocation);
    final String testResource = ResourceUtils.normalize(ResourceUtils.read(is, "US-ASCII"));
    final String expected = ResourceUtils.read(ClassLoaderUtils.getClassLoader(SpringResourceResourceResolverSpring4Test.class).getResourceAsStream(templateLocation), "US-ASCII", true);
    Assert.assertEquals(expected, testResource);
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Context(org.thymeleaf.context.Context) TemplateProcessingParameters(org.thymeleaf.TemplateProcessingParameters) Configuration(org.thymeleaf.Configuration) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 2 with Configuration

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

the class ExpressionBenchmark method main.

public static void main(String[] args) throws Exception {
    final Map<String, String> expressionsMap = ExpressionBenchmarkDefinitions.createExpressionsMap();
    final Configuration configuration = new Configuration();
    final IProcessingContext processingContext = new ProcessingContext(new Context());
    final IStandardExpressionParser parser = new StandardExpressionParser();
    for (final Map.Entry<String, String> expressionEntry : expressionsMap.entrySet()) {
        final String expression = expressionEntry.getKey();
        final String expectedParsingResult = expressionEntry.getValue();
        final IStandardExpression parsedExpression = parser.parseExpression(configuration, processingContext, expression);
        Assert.assertNotNull(parsedExpression);
        final String exp = parsedExpression.getStringRepresentation();
        Assert.assertEquals(expectedParsingResult, exp);
    }
    final StopWatch sw = new StopWatch();
    sw.start();
    for (int x = 0; x < 1000; x++) for (final String expression : expressionsMap.keySet()) parser.parseExpression(configuration, processingContext, expression);
    sw.stop();
    System.out.println("First pass: " + sw.toString());
    sw.reset();
    sw.start();
    for (int x = 0; x < 1000; x++) for (final String expression : expressionsMap.keySet()) parser.parseExpression(configuration, processingContext, expression);
    sw.stop();
    System.out.println("Second pass: " + sw.toString());
}
Also used : IProcessingContext(org.thymeleaf.context.IProcessingContext) ProcessingContext(org.thymeleaf.context.ProcessingContext) Context(org.thymeleaf.context.Context) IProcessingContext(org.thymeleaf.context.IProcessingContext) ProcessingContext(org.thymeleaf.context.ProcessingContext) Configuration(org.thymeleaf.Configuration) IProcessingContext(org.thymeleaf.context.IProcessingContext) StopWatch(org.apache.commons.lang3.time.StopWatch) Map(java.util.Map)

Example 3 with Configuration

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

the class AddLocalVariableToResult method processAttribute.

@Override
protected ProcessorResult processAttribute(final Arguments arguments, final Element element, final String attributeName) {
    final String attributeValue = element.getAttributeValue(attributeName);
    final Configuration configuration = arguments.getConfiguration();
    final AssignationSequence assignationSequence = AssignationUtils.parseAssignationSequence(configuration, arguments, attributeValue, false);
    final Map<String, Object> localVariables = new HashMap<String, Object>();
    for (final Assignation assignation : assignationSequence.getAssignations()) {
        final IStandardExpression varNameExpr = assignation.getLeft();
        final IStandardExpression varValueExpr = assignation.getRight();
        final Object varName = varNameExpr.execute(configuration, arguments);
        final Object varValue = varValueExpr.execute(configuration, arguments);
        localVariables.put((varName == null ? null : varName.toString()), varValue);
    }
    element.removeAttribute(attributeName);
    return ProcessorResult.setLocalVariables(localVariables);
}
Also used : IStandardExpression(org.thymeleaf.standard.expression.IStandardExpression) Configuration(org.thymeleaf.Configuration) HashMap(java.util.HashMap) AssignationSequence(org.thymeleaf.standard.expression.AssignationSequence) Assignation(org.thymeleaf.standard.expression.Assignation)

Example 4 with Configuration

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

the class AddLocalVariableToNode method processAttribute.

@Override
protected ProcessorResult processAttribute(final Arguments arguments, final Element element, final String attributeName) {
    final String attributeValue = element.getAttributeValue(attributeName);
    final Configuration configuration = arguments.getConfiguration();
    final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(configuration);
    final AssignationSequence assignationSequence = AssignationUtils.parseAssignationSequence(configuration, arguments, attributeValue, false);
    for (final Assignation assignation : assignationSequence.getAssignations()) {
        final IStandardExpression varNameExpr = assignation.getLeft();
        final IStandardExpression varValueExpr = assignation.getRight();
        final Object varName = varNameExpr.execute(configuration, arguments);
        final Object varValue = varValueExpr.execute(configuration, arguments);
        element.setNodeLocalVariable((varName == null ? null : varName.toString()), varValue);
    }
    element.removeAttribute(attributeName);
    return ProcessorResult.OK;
}
Also used : IStandardExpression(org.thymeleaf.standard.expression.IStandardExpression) Configuration(org.thymeleaf.Configuration) IStandardExpressionParser(org.thymeleaf.standard.expression.IStandardExpressionParser) AssignationSequence(org.thymeleaf.standard.expression.AssignationSequence) Assignation(org.thymeleaf.standard.expression.Assignation)

Example 5 with Configuration

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

the class SpringResourceTemplateResolverSpring3Test method testResolveTemplate.

@Test
public void testResolveTemplate() throws Exception {
    final String templateLocation = "spring321/view/test.html";
    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring321/view/applicationContext.xml");
    final SpringResourceTemplateResolver resolver = (SpringResourceTemplateResolver) context.getBean("springResourceTemplateResolver");
    resolver.initialize();
    final String templateMode = resolver.getTemplateMode();
    Assert.assertEquals("HTML5", templateMode);
    final TemplateProcessingParameters parameters = new TemplateProcessingParameters(new Configuration(), "classpath:" + templateLocation, new Context());
    final TemplateResolution resolution = resolver.resolveTemplate(parameters);
    final IResourceResolver resourceResolver = resolution.getResourceResolver();
    final InputStream is = resourceResolver.getResourceAsStream(parameters, resolution.getResourceName());
    final String testResource = ResourceUtils.normalize(ResourceUtils.read(is, "US-ASCII"));
    final String expected = ResourceUtils.read(ClassLoaderUtils.getClassLoader(SpringResourceTemplateResolverSpring3Test.class).getResourceAsStream(templateLocation), "US-ASCII", true);
    Assert.assertEquals(expected, testResource);
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Context(org.thymeleaf.context.Context) IResourceResolver(org.thymeleaf.resourceresolver.IResourceResolver) TemplateProcessingParameters(org.thymeleaf.TemplateProcessingParameters) Configuration(org.thymeleaf.Configuration) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) TemplateResolution(org.thymeleaf.templateresolver.TemplateResolution) InputStream(java.io.InputStream) Test(org.junit.Test)

Aggregations

Configuration (org.thymeleaf.Configuration)10 Context (org.thymeleaf.context.Context)5 InputStream (java.io.InputStream)4 Test (org.junit.Test)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 TemplateProcessingParameters (org.thymeleaf.TemplateProcessingParameters)4 Assignation (org.thymeleaf.standard.expression.Assignation)4 AssignationSequence (org.thymeleaf.standard.expression.AssignationSequence)4 IStandardExpression (org.thymeleaf.standard.expression.IStandardExpression)4 HashMap (java.util.HashMap)2 IResourceResolver (org.thymeleaf.resourceresolver.IResourceResolver)2 IStandardExpressionParser (org.thymeleaf.standard.expression.IStandardExpressionParser)2 TemplateResolution (org.thymeleaf.templateresolver.TemplateResolution)2 Map (java.util.Map)1 StopWatch (org.apache.commons.lang3.time.StopWatch)1 Before (org.junit.Before)1 IProcessingContext (org.thymeleaf.context.IProcessingContext)1 ProcessingContext (org.thymeleaf.context.ProcessingContext)1 StandardMessageResolver (org.thymeleaf.messageresolver.StandardMessageResolver)1 XhtmlAndHtml5NonValidatingDOMTemplateParser (org.thymeleaf.templateparser.xmldom.XhtmlAndHtml5NonValidatingDOMTemplateParser)1