diff --git a/spring-framework-reference/src/expressions.xml b/spring-framework-reference/src/expressions.xml index 2a537a68d11..faacbd465ce 100644 --- a/spring-framework-reference/src/expressions.xml +++ b/spring-framework-reference/src/expressions.xml @@ -72,7 +72,7 @@ - Operators + Relational operators @@ -83,6 +83,10 @@ Calling constructors + + Ternary operator + + Variables @@ -429,49 +433,189 @@ String city = (String) parser.parseExpression("placeOfBirth.City").getValue(cont names. The contents of arrays and lists are obtained using square bracket notation. - + ExpressionParser parser = new SpelAntlrExpressionParser(); + +// Inventions Array +StandardEvaluationContext teslaContext = new StandardEvaluationContext(); +teslaContext.setRootObject(tesla); + +// evaluates to "Induction motor" +String invention = parser.parseExpression("inventions[3]").getValue(teslaContext, String.class); + + +// Members List +StandardEvaluationContext societyContext = new StandardEvaluationContext(); +societyContext.setRootObject(ieee); + +// evaluates to "Nikola Tesla" +String name = parser.parseExpression("Members[0].Name").getValue(societyContext, String.class); + +// List and Array navigation +// evaluates to "Wireless communication" +String invention = parser.parseExpression("Members[0].Inventions[6]").getValue(societyContext, String.class); + + + The contents of dictionaries are obtained by specifying the + literal key value within the brackets. In this case, because keys for + the Officers dictionary are strings, we can specify string + literal. + + // Officer's Dictionary + +Inventor pupin = parser.parseExpression("Officers['president']").getValue(societyContext, Inventor.class); + +// evaluates to "Idvor" +String city = parser.parseExpression("Officers['president'].PlaceOfBirth.City").getValue(societyContext, String.class); + +// setting values +parser.parseExpression("Officers['advisors'][0].PlaceOfBirth.Country").setValue(societyContext, "Croatia"); + +
Methods - blah blah varargs + Methods are invoked using typical Java programming syntax. You may + also invoke methods on literals. Varargs are also supported. + + // string literal, evaluates to "bc" +String c = parser.parseExpression("'abc'.substring(2, 3)").getValue(String.class); + +// evaluates to true +boolean isMember = parser.parseExpression("isMember('Mihajlo Pupin')").getValue(societyContext, Boolean.class);
Operators - blah blah -
Relational operators - blah blah + The relational operators; equal, not equal, less than, less than + or equal, greater than, and greater than or equal are supported using + standard operator notation. Support is not yet implemented for objects + that implement the Comparable interface. - + // evaluats to true +boolean isEqual = parser.parseExpression("2 == 2").getValue(Boolean.class); + +// evaluates to false +boolean isEqual = parser.parseExpression("2 < -5.0").getValue(Boolean.class); + +// evaluates to true +boolean isEqual = parser.parseExpression("'black' < 'block'").getValue(Boolean.class);In + addition to standard relational operators SpEL supports the + 'instanceof' and regular expression based 'matches' operator. + + // evaluates to false +boolean falseValue = parser.parseExpression("'xyz' instanceof T(int)").getValue(Boolean.class); + +// evaluates to true +boolean trueValue = parser.parseExpression("'5.00' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class); + +//evaluates to false +boolean falseValue = parser.parseExpression("'5.0067' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class); + +
Logical operators - + The logical operators that are supported are and, or, and not. + Their use is demonstrated below - + // -- AND -- + +// evaluates to false +boolean falseValue = parser.parseExpression("true and false").getValue(Boolean.class); + +// evaluates to true +String expression = "isMember('Nikola Tesla') and isMember('Mihajlo Pupin')"; +boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class); + +// -- OR -- + +// evaluates to false +boolean falseValue = parser.parseExpression("true or false").getValue(Boolean.class); + +// evaluates to true +String expression = "isMember('Nikola Tesla') or isMember('Albert Einstien')"; +boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class); + +// -- NOT -- + +// evaluates to false +boolean falseValue = parser.parseExpression("!true").getValue(Boolean.class); + + +// -- AND and NOT -- +String expression = "isMember('Nikola Tesla') and !isMember('Mihajlo Pupin')"; +boolean falseValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);
Mathematical operators - blah blah + The addition operator can be used on numbers, strings and dates. + Subtraction can be used on numbers and dates. Multiplication and + division can be used only on numbers. Other mathematical operators + supported are modulus (%) and exponential power (^). Standard operator + precedence is enforced. These operators are demonstrated below - + // Addition +int two = parser.parseExpression("1 + 1").getValue(Integer.class); // 2 + +String testString = parser.parseExpression("'test' + ' ' + 'string'").getValue(String.class); // 'test string' + +// Subtraction +int four = parser.parseExpression("1 - -3").getValue(Integer.class); // 4 + +double d = parser.parseExpression("1000.00 - 1e4").getValue(Double.class); // -9000 + +// Multiplication +int six = parser.parseExpression("-2 * -3").getValue(Integer.class); // 6 + +double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(Double.class); // 24.0 + +// Division +int minusTwo = parser.parseExpression("6 / -3").getValue(Integer.class); // -2 + +double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(Double.class); // 1.0 + +// Modulus +int three = parser.parseExpression("7 % 4").getValue(Integer.class); // 3 + +int one = parser.parseExpression("8 / 5 % 2").getValue(Integer.class); // 1 + +// Operator precedence +int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class); // -21 + + + +
Assignment - blah blah + Setting of a property is done by using the assignment operator. + This would typically be done within a call to SetValue but can also be + done inside a call to GetValue + + Inventor inventor = new Inventor(); +StandardEvaluationContext inventorContext = new StandardEvaluationContext(); +inventorContext.setRootObject(inventor); + +parser.parseExpression("Name").setValue(inventorContext, "Alexander Seovic2"); + +// alternatively + +String aleks = parser.parseExpression("Name = 'Alexandar Seovic'").getValue(inventorContext, String.class); + +
@@ -479,19 +623,47 @@ String city = (String) parser.parseExpression("placeOfBirth.City").getValue(cont
Types - blah blah + The specic 'T' operator can be used to specify an instance of + java.lang.Class (the 'type'). Static methods are invoked using this + operator as well + + Class dateClass = parser.parseExpression("T(java.util.Date)").getValue(Class.class); + + +boolean isEqual = parser.parseExpression("T(java.math.RoundingMode).CEILING < T(java.math.RoundingMode).FLOOR").getValue(Boolean.class); +
Constructors - blah blah + Constructors can be invoked using the new operator. The fully + qualified classname should be used for all but the primitive type and + String (where int, float, etc, can be used). + + Inventor einstein = + parser.parseExpression("new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German')").getValue(Inventor.class); + +//create new inventor instance within add method of List +parser.parseExpression("Members.add(new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German'))").getValue(societyContext); +
Variables - blah blah + Variables can referenced in the expression using the syntax + #variableName. Variables are set using the method setVariable on the + StandardEvaluationContext. + + Inventor tesla = new Inventor("Nikola Tesla", "Serbian"); +StandardEvaluationContext context = new StandardEvaluationContext(); +context.setVariable("newName", "Mike Tesla"); +context.setRootObject(tesla); + +parser.parseExpression("Name = #newName").getValue(context); + +System.out.println(tesla.getName()) // "Mike Tesla"
The #this and #root variables @@ -506,6 +678,42 @@ String city = (String) parser.parseExpression("placeOfBirth.City").getValue(cont blah blah
+
+ Ternary Operator (If-Then-Else) + + You can use the ternary operator for performing if-then-else + conditional logic inside the expression. A minimal example is; + + String falseString = parser.parseExpression("false ? 'trueExp' : 'falseExp'").getValue(String.class); + + In this case, the boolean false results in returning the string + value 'falseExp'. A less artificial example is shown below. + + parser.parseExpression("Name").setValue(societyContext, "IEEE"); +societyContext.setVariable("queryName", "Nikola Tesla"); + +expression = "isMember(#queryName)? #queryName + ' is a member of the ' " + + "+ Name + ' Society' : #queryName + ' is not a member of the ' + Name + ' Society'"; + +String queryResultString = parser.parseExpression(expression).getValue(societyContext, String.class); +
+ +
+ List Selection + + List selection is a powerful expression language feature that + allow you to transform the source list into another list by selecting + from its "rows". In other words, selection is comparable to using SQL + with a WHERE clause. + + Selection uses the syntax ?{projectionExpression}. This will + filter the list and return a new list containing a subset of the + original element list. For example, selection would allow us to easily + get a list of Serbian inventors: + + List<Inventor> list = (List<Inventor>) parser.parseExpression("Members.?{Nationality == 'Serbian'}").getValue(societyContext); +
+
Expression templating