Expression parser and pre-processing syntax in a custom dialect

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Expression parser and pre-processing syntax in a custom dialect

Thibault Duchateau
Hi,

Currently in the Dandelion dialects, I'm using an utility method that detect whether the provided argument is an expression or not. I'm not particularly proud of it.

Here is a snippet of the method:

private static <T> T processStandardExpression(Arguments arguments, String value, Class<T> clazz) {

   Object result = null;

   if (value.contains("${") || value.contains("@{") || value.contains("#{")) {
      Configuration configuration = arguments.getConfiguration();
      IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(configuration);
      IStandardExpression standardExpression = expressionParser.parseExpression(configuration, arguments, value);
      result = standardExpression.execute(configuration, arguments);
   }
   ...
}

1) Is there any better way (than hard coding the above characters) to know if it's a Thymeleaf expression?

2) Unfortunately, the above snippet doesn't handle the pre-processing syntax. Do I have to parse the expression myself (looking for __${...}__) and use the standard expression parser or do I have to use something particular to handle this situation?

Thanks!

Regards,
Thibault
Reply | Threaded
Open this post in threaded view
|

Re: Expression parser and pre-processing syntax in a custom dialect

danielfernandez
Administrator
In fact, your method not only does not support preprocessing, but neither does support text literals ('...'), literal substitutions (|...|) or literal tokens (hello).

The most correct way of determining if something is an expression or not is by actually parsing it. A non-null result will mean the original String was a valid expression, and null will mean it wasn't.

I'm afraid there is no more elegant option, though the null-result thing was actually done on purpose (instead of raising exceptions) in order to make it usable without a big performance loss...

Regards,
Daniel.
Reply | Threaded
Open this post in threaded view
|

Re: Expression parser and pre-processing syntax in a custom dialect

Thibault Duchateau
Hi Daniel,

Sorry for the late reply :-/

I ran some tests (Thymeleaf 2.1.3.RELEASE and 2.1.4.RELEASE) and getting an unexpected result.

Everything was going well until I use "fadein,1500" as an attribute value.
I was expecting the return value from expressionParser.parseExpression to be null and so consider the attribute's value as a simple String.

Usage example:
<table ... dt:appear="fadein,1500">
...
</table>

The attribute processor applied on "dt:appear" internally uses the function below, slighly modified as you suggested:

private static <T> T processStandardExpression(Arguments arguments, String value, Class<T> clazz) {

   Object result = null;

   Configuration configuration = arguments.getConfiguration();
   IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(configuration);
   IStandardExpression standardExpression = expressionParser.parseExpression(configuration, arguments, value);
		
   if(standardExpression == null){
      result = value;
   }
   else {
      result = standardExpression.execute(configuration, arguments);
   }
   ...
}

Here is the exception:

1:59:42.164 ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][http-bio-8080-exec-5] Exception processing template "styling/effect": Could not parse as expression: "fadein,1500" (styling/effect:25)
5 janv. 2015 01:59:42 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [spring] in context with path [/datatables-thymeleaf-starter] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "fadein,1500" (styling/effect:25)] with root cause
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "fadein,1500" (styling/effect:25)
	at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:186)
	at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:70)
	at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:40)
	at com.github.dandelion.datatables.thymeleaf.util.AttributeUtils.processStandardExpression(AttributeUtils.java:230)

Any idea?

Thanks!

PS: happy new year! ;-)
Reply | Threaded
Open this post in threaded view
|

Re: Expression parser and pre-processing syntax in a custom dialect

Thibault Duchateau
Sorry but up :-/

Please let me know if you need anything else.

Regards,
Thibault.