CommonsJEXLサンプルの詳細



Commons Jexl Sample Details



Jexlは、ExpressionLanguageの解析エンジンです。これは、システムまたはプログラムフレームワークの埋め込みを容易にするように設計されています。 JSTLにELの拡張バージョンを実装します。ただし、Velocityのいくつかの概念も使用します。 Jexlを使用した例を次に示します。詳細なメモ付きで、

import java.util.HashMap import java.util.List import java.util.ArrayList import java.util.HashMap import java.util.Map import org.apache.commons.jexl.Expression import org.apache.commons.jexl.ExpressionFactory import org.apache.commons.jexl.JexlContext import org.apache.commons.jexl.JexlHelper ...... /* * Initialize a Cat object */ Cat cat = new Cat() { { this.setAge(2) this.setName('Tom') this.setOwner(new People() { { this.setAge(24) this.setName('yang') } }) } } /* * Initialize a List object, storing two elements in the list * The first element is a string * The second element is an integer */ List list = new ArrayList() list.add('Hello world !') list.add(11) Map map = new HashMap() map.put('cat', cat) map.put('people',cat.getOwner()) /* * Initialize a JexlContext object that represents a context that executes JEXL expressions */ JexlContext context = JexlHelper.createContext() /* * Deposit two variables into the variable dictionary of the context in which the JEXL expression is executed * key value 'tom' corresponding to the variable cat * key value 'array' corresponding variable list */ context.getVars().put('tom', cat) context.getVars().put('array', list) context.getVars().put('map', map) /* * Define all expressions to be evaluated */ String[] expressions = new String[]{ / / Nested properties 'tom.owner', / / Nested properties 'tom.owner.name', //The method call of the nested property, the result of the expression evaluation is the return value of the method 'tom.owner.name.length()', 'array[0].toUpperCase()', / / Built-in general method size (), return the length of String, Map and List 'size(tom.owner.name)', / / Return to the first element in the list 'array[0]', //+ operator can be used for string concatenation 'array[0].toUpperCase()+array[0]', / / Built-in general method empty (), return true if empty, otherwise return false 'empty(array[0])', 'empty(array[2])', / / Get the corresponding value in the dictionary by the key value 'cat' 'map['cat']', / / Nested properties 'map['people'].name.length()' } / / Evaluate all expressions in expressions for(String expression : expressions){ / / Create an Expression object with the static method createExpression of the ExpressionFactory class Expression e = ExpressionFactory.createExpression(expression) / / Evaluation of this Expression object, passing in the context object that executes the JEXL expression Object obj = e.evaluate(context) / / Output expression evaluation results System.out.println(e.getExpression()+' = '+obj) }

結果は次のとおりです。



tom.owner = root @ xxxxx

tom.owner.name = which



tom.owner.name.length()= 4

array [0] .toUpperCase()= HELLO WORLD!

size(tom.owner.name)= 4



array [0] = Hello world!

array [0] .toUpperCase()+ array [0] = HELLO WORLD!Hello world!

empty(array [0])= false

empty(array [2])= true

map ['cat'] = root @ xxxxx285

map ['people']。name.length()= 4