svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.api / src / main / java / org / gvsig / expressionevaluator / ExpressionUtils.java @ 44369
History | View | Annotate | Download (5.44 KB)
1 | 44163 | jjdelcerro | package org.gvsig.expressionevaluator; |
---|---|---|---|
2 | |||
3 | 44215 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
4 | 44198 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
5 | 44163 | jjdelcerro | import org.gvsig.tools.script.Script; |
6 | |||
7 | /**
|
||
8 | *
|
||
9 | * @author jjdelcerro
|
||
10 | */
|
||
11 | public class ExpressionUtils { |
||
12 | |||
13 | public static boolean isEmpty(Expression expression) { |
||
14 | return expression == null || expression.isEmpty(); |
||
15 | } |
||
16 | |||
17 | public static boolean isPhraseEmpty(Expression expression) { |
||
18 | return expression == null || expression.isPhraseEmpty(); |
||
19 | } |
||
20 | |||
21 | public static Expression defaultIfEmpty(Expression expression, Expression defaultValue) { |
||
22 | if( expression==null || expression.isEmpty() ) { |
||
23 | return defaultValue;
|
||
24 | } |
||
25 | return expression;
|
||
26 | } |
||
27 | |||
28 | public static Expression defaultNullIfEmpty(Expression expression) { |
||
29 | if( expression==null || expression.isEmpty() ) { |
||
30 | return null; |
||
31 | } |
||
32 | return expression;
|
||
33 | } |
||
34 | |||
35 | public static Expression defaultIfPhraseEmpty(Expression expression, Expression defaultValue) { |
||
36 | if( expression==null || expression.isPhraseEmpty() ) { |
||
37 | return defaultValue;
|
||
38 | } |
||
39 | return expression;
|
||
40 | } |
||
41 | |||
42 | public static Expression defaultNullIfPhraseEmpty(Expression expression) { |
||
43 | if( expression==null || expression.isPhraseEmpty() ) { |
||
44 | return null; |
||
45 | } |
||
46 | return expression;
|
||
47 | } |
||
48 | |||
49 | public static Expression createExpression() { |
||
50 | Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
||
51 | return expression;
|
||
52 | } |
||
53 | |||
54 | public static Expression createExpression(String phrase) { |
||
55 | 44215 | jjdelcerro | if( StringUtils.isBlank(phrase) ) {
|
56 | return null; |
||
57 | } |
||
58 | 44163 | jjdelcerro | Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
59 | expression.setPhrase(phrase); |
||
60 | return expression;
|
||
61 | } |
||
62 | |||
63 | public static Expression createExpression(String phrase, String code, Script... scripts) { |
||
64 | Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
||
65 | expression.setPhrase(phrase); |
||
66 | expression.setUserScript(code); |
||
67 | for (Script script : scripts) {
|
||
68 | expression.addScript(script); |
||
69 | } |
||
70 | return expression;
|
||
71 | } |
||
72 | |||
73 | public static Expression createExpression(String phrase, String code, String languaje, Script... scripts) { |
||
74 | Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
||
75 | expression.setPhrase(phrase); |
||
76 | expression.setUserScript(code, languaje); |
||
77 | for (Script script : scripts) {
|
||
78 | expression.addScript(script); |
||
79 | } |
||
80 | return expression;
|
||
81 | } |
||
82 | 44198 | jjdelcerro | |
83 | public static ExpressionBuilder createExpressionBuilder() { |
||
84 | ExpressionBuilder builder = ExpressionEvaluatorLocator.getManager().createExpressionBuilder(); |
||
85 | return builder;
|
||
86 | } |
||
87 | |||
88 | public static Code compile(String expression) { |
||
89 | 44215 | jjdelcerro | if( StringUtils.isBlank(expression) ) {
|
90 | return null; |
||
91 | } |
||
92 | 44198 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
93 | Code code = manager.compile(expression); |
||
94 | return code;
|
||
95 | } |
||
96 | |||
97 | 44346 | jjdelcerro | public static Object evaluate(String expression) { |
98 | return evaluate(null, expression); |
||
99 | } |
||
100 | |||
101 | 44198 | jjdelcerro | public static Object evaluate(SymbolTable symbolTable, String expression) { |
102 | 44215 | jjdelcerro | if( StringUtils.isBlank(expression) ) {
|
103 | return null; |
||
104 | } |
||
105 | 44346 | jjdelcerro | if( symbolTable==null ) { |
106 | symbolTable = createSymbolTable(); |
||
107 | } |
||
108 | 44198 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
109 | Object x = manager.evaluate(symbolTable, expression);
|
||
110 | return x;
|
||
111 | } |
||
112 | |||
113 | public static Object evaluate(SymbolTable symbolTable, Code code) { |
||
114 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
115 | Object x = manager.evaluate(symbolTable, code);
|
||
116 | return x;
|
||
117 | } |
||
118 | |||
119 | public static Code optimize(SymbolTable symbolTable, Code code) { |
||
120 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
121 | code = manager.optimize(symbolTable, code); |
||
122 | return code;
|
||
123 | } |
||
124 | |||
125 | public static String toString(Value value, Formatter formatter) { |
||
126 | if( value == null ) { |
||
127 | return null; |
||
128 | } |
||
129 | if( formatter==null ) { |
||
130 | formatter = ExpressionBuilder.EMPTY_FORMATTER; |
||
131 | } |
||
132 | return value.toString(formatter);
|
||
133 | } |
||
134 | |||
135 | public static String toString(Value value) { |
||
136 | if( value == null ) { |
||
137 | return null; |
||
138 | } |
||
139 | return value.toString(ExpressionBuilder.EMPTY_FORMATTER);
|
||
140 | } |
||
141 | |||
142 | public static String toString(Code code, Formatter formatter) { |
||
143 | if( code == null ) { |
||
144 | return null; |
||
145 | } |
||
146 | if( formatter==null ) { |
||
147 | formatter = Code.EMPTY_FORMATTER; |
||
148 | } |
||
149 | return code.toString(formatter);
|
||
150 | } |
||
151 | |||
152 | public static String toString(Code code) { |
||
153 | if( code == null ) { |
||
154 | return null; |
||
155 | } |
||
156 | return code.toString(Code.EMPTY_FORMATTER);
|
||
157 | } |
||
158 | |||
159 | 44215 | jjdelcerro | public static Expression createExpressionFromJSON(String json) { |
160 | Expression expression = ExpressionUtils.createExpression();
|
||
161 | expression.fromJSON(json); |
||
162 | return expression;
|
||
163 | } |
||
164 | |||
165 | public static MutableSymbolTable createSymbolTable() { |
||
166 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
167 | MutableSymbolTable symbolTable = manager.createSymbolTable(); |
||
168 | return symbolTable;
|
||
169 | } |
||
170 | |||
171 | 44163 | jjdelcerro | } |