Search in sources :

Example 6 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 7 with Configuration

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

the class SpringResourceResourceResolverSpring3Test method testGetResourceAsStream.

@Test
public void testGetResourceAsStream() throws Exception {
    final String templateLocation = "spring321/view/test.html";
    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring321/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(SpringResourceResourceResolverSpring3Test.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 8 with Configuration

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

the class TemplateParserTest method initialize.

// -----
@Before
public void initialize() {
    this.templateResolver = new StringTemplateResolver("!!!");
    this.configuration = new Configuration();
    this.configuration.setDialect(new TestDialect());
    this.configuration.setTemplateResolver(this.templateResolver);
    this.configuration.setMessageResolver(new StandardMessageResolver());
    this.configuration.setTemplateModeHandlers(StandardTemplateModeHandlers.ALL_TEMPLATE_MODE_HANDLERS);
    this.configuration.initialize();
    this.xmlDomDocumentParser = new XhtmlAndHtml5NonValidatingDOMTemplateParser(2);
    this.xmlSaxDocumentParser = new XhtmlAndHtml5NonValidatingSAXTemplateParser(2);
}
Also used : XhtmlAndHtml5NonValidatingDOMTemplateParser(org.thymeleaf.templateparser.xmldom.XhtmlAndHtml5NonValidatingDOMTemplateParser) XhtmlAndHtml5NonValidatingSAXTemplateParser(org.thymeleaf.templateparser.xmlsax.XhtmlAndHtml5NonValidatingSAXTemplateParser) Configuration(org.thymeleaf.Configuration) StandardMessageResolver(org.thymeleaf.messageresolver.StandardMessageResolver) Before(org.junit.Before)

Example 9 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 10 with Configuration

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

the class SpringResourceTemplateResolverSpring4Test method testResolveTemplate.

@Test
public void testResolveTemplate() throws Exception {
    final String templateLocation = "spring421/view/test.html";
    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring421/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(SpringResourceTemplateResolverSpring4Test.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