I have an expression that contains a single & and spring spel evaluator keeps throwing the error missing expected character '&'
I have tried using backslash and double backslash to escape it but then it gives illegal escape character error.
This is my expression that I need to parse
(#PaymentType!=null&&!(#PaymentType).toString().isEmpty()?'PaymentType:'+((#PaymentType)+' ')+'$':'')+(#ProductSearchState!=null&&!(#ProductSearchState).toString().isEmpty()?'ProductSearchState:'+((#ProductSearchState)+' ')+'$':'')+(#Manufacturer_s39s_s32Name_s32_s38_s32Address!=null&&!(#Manufacturer_s39s_s32Name_s32_s38_s32Address).toString().isEmpty()?'Manufacturer's Name & Address:'+((#Manufacturer_s39s_s32Name_s32_s38_s32Address)+' '):'')
Following is the code I am using to parse the expression .
public static void main (String args[]) {
String expression="(#PaymentType!=null&&!(#PaymentType).toString().isEmpty()?'PaymentType:'+((#PaymentType)+' ')+'$':'')+(#ProductSearchState!=null&&!(#ProductSearchState).toString().isEmpty()?'ProductSearchState:'+((#ProductSearchState)+' ')+'$':'')+(#Manufacturer_s39s_s32Name_s32_s38_s32Address!=null&&!(#Manufacturer_s39s_s32Name_s32_s38_s32Address).toString().isEmpty()?'Manufacturer's Name & Address:'+((#Manufacturer_s39s_s32Name_s32_s38_s32Address)+' '):'')";
String g = expression;
System.out.println(g);
final Set<String> dependent = new HashSet<>();
try {
ExpressionParser parser = new SpelExpressionParser();
// log.info("Extracting dependent attributes for {} ", expression);
new SplExpressionVisitor<Void>() {
public void visit(SpelExpression expression) {
visitNode(expression.getAST());
}
@Override
protected Void visit(VariableReference node) {
final String variableReferenceName = getVariableReferenceName(node);
dependent.add(variableReferenceName);
return super.visit(node);
}
}.visit((SpelExpression) parser.parseExpression(expression));
}catch (Exception e){
System.out.println(e.toString());
}
}
Your string 'Manufacturer's Name & Address:'
looks like the string 'Manufacturer'
followed by some nonsense, as you can't use single quotation marks both to delimit the string as well as inside the string.
You need to use two single quote characters inside the string to escape it, like this:
'Manufacturer''s Name & Address:'
Refer to the "Literal Expressions" section of the SpEL languauge reference documentation for more information about including literals in your expressions.