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 @ 44769
History | View | Annotate | Download (11.2 KB)
1 | 44163 | jjdelcerro | package org.gvsig.expressionevaluator; |
---|---|---|---|
2 | |||
3 | 44389 | jjdelcerro | import java.io.File; |
4 | 44397 | jjdelcerro | import org.apache.commons.lang3.ArrayUtils; |
5 | 44215 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
6 | 44198 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
7 | 44163 | jjdelcerro | |
8 | /**
|
||
9 | *
|
||
10 | * @author jjdelcerro
|
||
11 | */
|
||
12 | public class ExpressionUtils { |
||
13 | |||
14 | public static boolean isEmpty(Expression expression) { |
||
15 | return expression == null || expression.isEmpty(); |
||
16 | } |
||
17 | |||
18 | public static boolean isPhraseEmpty(Expression expression) { |
||
19 | return expression == null || expression.isPhraseEmpty(); |
||
20 | } |
||
21 | |||
22 | public static Expression defaultIfEmpty(Expression expression, Expression defaultValue) { |
||
23 | if( expression==null || expression.isEmpty() ) { |
||
24 | return defaultValue;
|
||
25 | } |
||
26 | return expression;
|
||
27 | } |
||
28 | |||
29 | public static Expression defaultNullIfEmpty(Expression expression) { |
||
30 | if( expression==null || expression.isEmpty() ) { |
||
31 | return null; |
||
32 | } |
||
33 | return expression;
|
||
34 | } |
||
35 | |||
36 | public static Expression defaultIfPhraseEmpty(Expression expression, Expression defaultValue) { |
||
37 | if( expression==null || expression.isPhraseEmpty() ) { |
||
38 | return defaultValue;
|
||
39 | } |
||
40 | return expression;
|
||
41 | } |
||
42 | |||
43 | public static Expression defaultNullIfPhraseEmpty(Expression expression) { |
||
44 | if( expression==null || expression.isPhraseEmpty() ) { |
||
45 | return null; |
||
46 | } |
||
47 | return expression;
|
||
48 | } |
||
49 | |||
50 | public static Expression createExpression() { |
||
51 | Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
||
52 | return expression;
|
||
53 | } |
||
54 | |||
55 | public static Expression createExpression(String phrase) { |
||
56 | 44215 | jjdelcerro | if( StringUtils.isBlank(phrase) ) {
|
57 | return null; |
||
58 | } |
||
59 | 44163 | jjdelcerro | Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
60 | expression.setPhrase(phrase); |
||
61 | return expression;
|
||
62 | } |
||
63 | |||
64 | 44533 | jjdelcerro | // public static Expression createExpression(String phrase, String code, Script... scripts) {
|
65 | // Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
||
66 | // expression.setPhrase(phrase);
|
||
67 | // expression.setUserScript(code);
|
||
68 | // for (Script script : scripts) {
|
||
69 | // expression.addScript(script);
|
||
70 | // }
|
||
71 | // return expression;
|
||
72 | // }
|
||
73 | //
|
||
74 | // public static Expression createExpression(String phrase, String code, String languaje, Script... scripts) {
|
||
75 | // Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
||
76 | // expression.setPhrase(phrase);
|
||
77 | // expression.setUserScript(code, languaje);
|
||
78 | // for (Script script : scripts) {
|
||
79 | // expression.addScript(script);
|
||
80 | // }
|
||
81 | // return expression;
|
||
82 | // }
|
||
83 | 44163 | jjdelcerro | |
84 | 44198 | jjdelcerro | public static ExpressionBuilder createExpressionBuilder() { |
85 | ExpressionBuilder builder = ExpressionEvaluatorLocator.getManager().createExpressionBuilder(); |
||
86 | return builder;
|
||
87 | } |
||
88 | |||
89 | public static Code compile(String expression) { |
||
90 | 44215 | jjdelcerro | if( StringUtils.isBlank(expression) ) {
|
91 | return null; |
||
92 | } |
||
93 | 44198 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
94 | Code code = manager.compile(expression); |
||
95 | return code;
|
||
96 | } |
||
97 | |||
98 | 44346 | jjdelcerro | public static Object evaluate(String expression) { |
99 | return evaluate(null, expression); |
||
100 | } |
||
101 | |||
102 | 44198 | jjdelcerro | public static Object evaluate(SymbolTable symbolTable, String expression) { |
103 | 44215 | jjdelcerro | if( StringUtils.isBlank(expression) ) {
|
104 | return null; |
||
105 | } |
||
106 | 44198 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
107 | Object x = manager.evaluate(symbolTable, expression);
|
||
108 | return x;
|
||
109 | } |
||
110 | |||
111 | public static Object evaluate(SymbolTable symbolTable, Code code) { |
||
112 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
113 | Object x = manager.evaluate(symbolTable, code);
|
||
114 | return x;
|
||
115 | } |
||
116 | |||
117 | public static Code optimize(SymbolTable symbolTable, Code code) { |
||
118 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
119 | code = manager.optimize(symbolTable, code); |
||
120 | return code;
|
||
121 | } |
||
122 | |||
123 | public static String toString(Value value, Formatter formatter) { |
||
124 | if( value == null ) { |
||
125 | return null; |
||
126 | } |
||
127 | if( formatter==null ) { |
||
128 | 44769 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
129 | formatter = manager.getExpressionBuilderFormatter(); |
||
130 | 44198 | jjdelcerro | } |
131 | return value.toString(formatter);
|
||
132 | } |
||
133 | |||
134 | public static String toString(Value value) { |
||
135 | 44769 | jjdelcerro | return toString(value, null); |
136 | 44198 | jjdelcerro | } |
137 | |||
138 | public static String toString(Code code, Formatter formatter) { |
||
139 | if( code == null ) { |
||
140 | return null; |
||
141 | } |
||
142 | if( formatter==null ) { |
||
143 | formatter = Code.EMPTY_FORMATTER; |
||
144 | } |
||
145 | return code.toString(formatter);
|
||
146 | } |
||
147 | |||
148 | public static String toString(Code code) { |
||
149 | 44769 | jjdelcerro | return toString(code, null); |
150 | 44198 | jjdelcerro | } |
151 | |||
152 | 44215 | jjdelcerro | public static Expression createExpressionFromJSON(String json) { |
153 | Expression expression = ExpressionUtils.createExpression();
|
||
154 | expression.fromJSON(json); |
||
155 | return expression;
|
||
156 | } |
||
157 | |||
158 | public static MutableSymbolTable createSymbolTable() { |
||
159 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
160 | MutableSymbolTable symbolTable = manager.createSymbolTable(); |
||
161 | return symbolTable;
|
||
162 | } |
||
163 | 44397 | jjdelcerro | |
164 | 44408 | jjdelcerro | public static String surroundByDynamicTextTag(String source) { |
165 | return surroundByDynamicTextTag(source, true); |
||
166 | } |
||
167 | |||
168 | public static String surroundByDynamicTextTag(String source, boolean insert) { |
||
169 | if( source==null ) { |
||
170 | return null; |
||
171 | } |
||
172 | if( insert ) {
|
||
173 | return "<%=" + source + "%>"; |
||
174 | } |
||
175 | return "<%" + source + "%>"; |
||
176 | } |
||
177 | |||
178 | 44397 | jjdelcerro | public static boolean isDynamicText(String source) { |
179 | String[] sources = StringUtils.substringsBetween(source, "<%", "%>"); |
||
180 | if( ArrayUtils.isEmpty(sources) ) {
|
||
181 | return false; |
||
182 | } |
||
183 | return true; |
||
184 | } |
||
185 | 44215 | jjdelcerro | |
186 | 44397 | jjdelcerro | public static String evaluateDynamicText(String source) { |
187 | 44389 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
188 | 44397 | jjdelcerro | return manager.evaluateDynamicText(source);
|
189 | 44389 | jjdelcerro | } |
190 | |||
191 | 44397 | jjdelcerro | public static String evaluateDynamicText(SymbolTable symbolTable, String source) { |
192 | 44389 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
193 | 44397 | jjdelcerro | return manager.evaluateDynamicText(symbolTable, source);
|
194 | 44389 | jjdelcerro | } |
195 | |||
196 | 44390 | jjdelcerro | public static File evaluateFilename(File source) { |
197 | 44389 | jjdelcerro | return evaluateFilename(null, source); |
198 | } |
||
199 | 44397 | jjdelcerro | |
200 | public static boolean isDynamicFilename(File source) { |
||
201 | if( source == null ) { |
||
202 | return false; |
||
203 | } |
||
204 | return isDynamicText(source.getPath());
|
||
205 | } |
||
206 | 44389 | jjdelcerro | |
207 | @SuppressWarnings("StringEquality") |
||
208 | 44390 | jjdelcerro | public static File evaluateFilename(SymbolTable symbolTable, File source) { |
209 | 44389 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
210 | String src = source.getPath();
|
||
211 | 44397 | jjdelcerro | String r = manager.evaluateDynamicText(symbolTable, src);
|
212 | 44389 | jjdelcerro | if( r == src ) { // !!! I compare that it is the same pointer, it is what I want. |
213 | return source;
|
||
214 | } |
||
215 | File f = new File(r); |
||
216 | return f;
|
||
217 | } |
||
218 | 44444 | jjdelcerro | |
219 | public static int parseInt(String s) throws NumberFormatException { |
||
220 | if( StringUtils.isBlank(s) ) {
|
||
221 | throw new NumberFormatException("Can't get integer from a blank string."); |
||
222 | } |
||
223 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
224 | 44446 | jjdelcerro | SymbolTable symbolTable = null; //manager.getInmutableSymbolTable(); |
225 | 44444 | jjdelcerro | Object x;
|
226 | try {
|
||
227 | x = manager.evaluate(symbolTable, s); |
||
228 | if( x instanceof Number ) { |
||
229 | return ((Number) x).intValue(); |
||
230 | } |
||
231 | } catch(Exception ex) { |
||
232 | NumberFormatException ex1 = new NumberFormatException("Can't get integer from '"+s+"'."); |
||
233 | ex1.initCause(ex); |
||
234 | throw ex;
|
||
235 | } |
||
236 | if( x == null ) { |
||
237 | throw new NumberFormatException("Can't get integer from '"+s+"' value is null."); |
||
238 | } |
||
239 | throw new NumberFormatException("Can't get integer from '"+s+"' value is a "+x.getClass().getSimpleName()+"."); |
||
240 | } |
||
241 | |||
242 | public static long parseLong(String s) throws NumberFormatException { |
||
243 | if( StringUtils.isBlank(s) ) {
|
||
244 | throw new NumberFormatException("Can't get long from a blank string."); |
||
245 | } |
||
246 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
247 | 44446 | jjdelcerro | SymbolTable symbolTable = null; //manager.getInmutableSymbolTable(); |
248 | 44444 | jjdelcerro | Object x;
|
249 | try {
|
||
250 | x = manager.evaluate(symbolTable, s); |
||
251 | if( x instanceof Number ) { |
||
252 | return ((Number) x).longValue(); |
||
253 | } |
||
254 | } catch(Exception ex) { |
||
255 | NumberFormatException ex1 = new NumberFormatException("Can't get long from '"+s+"'."); |
||
256 | ex1.initCause(ex); |
||
257 | throw ex;
|
||
258 | } |
||
259 | if( x == null ) { |
||
260 | throw new NumberFormatException("Can't get long from '"+s+"' value is null."); |
||
261 | } |
||
262 | throw new NumberFormatException("Can't get long from '"+s+"' value is a "+x.getClass().getSimpleName()+"."); |
||
263 | } |
||
264 | |||
265 | public static double parseDouble(String s) throws NumberFormatException { |
||
266 | if( StringUtils.isBlank(s) ) {
|
||
267 | throw new NumberFormatException("Can't get double from a blank string."); |
||
268 | } |
||
269 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
270 | 44446 | jjdelcerro | SymbolTable symbolTable = null; //manager.getInmutableSymbolTable(); |
271 | 44444 | jjdelcerro | Object x;
|
272 | try {
|
||
273 | x = manager.evaluate(symbolTable, s); |
||
274 | if( x instanceof Number ) { |
||
275 | return ((Number) x).doubleValue(); |
||
276 | } |
||
277 | } catch(Exception ex) { |
||
278 | NumberFormatException ex1 = new NumberFormatException("Can't get double from '"+s+"'."); |
||
279 | ex1.initCause(ex); |
||
280 | throw ex;
|
||
281 | } |
||
282 | if( x == null ) { |
||
283 | throw new NumberFormatException("Can't get double from '"+s+"' value is null."); |
||
284 | } |
||
285 | throw new NumberFormatException("Can't get double from '"+s+"' value is a "+x.getClass().getSimpleName()+"."); |
||
286 | } |
||
287 | 44644 | jjdelcerro | |
288 | public static Compiler createCompiler() { |
||
289 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
290 | Compiler compiler = manager.createCompiler();
|
||
291 | return compiler;
|
||
292 | } |
||
293 | |||
294 | public static Interpreter createInterpreter() { |
||
295 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
296 | Interpreter interpreter = manager.createInterpreter(); |
||
297 | return interpreter;
|
||
298 | } |
||
299 | |||
300 | public static Optimizer createOptimizer() { |
||
301 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
302 | Optimizer optimizer = manager.createOptimizer(); |
||
303 | return optimizer;
|
||
304 | } |
||
305 | |||
306 | public static String repr(Object value) { |
||
307 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
308 | ReprMethod method = manager.getReprMethod(value); |
||
309 | return method.repr(value);
|
||
310 | } |
||
311 | 44750 | jjdelcerro | |
312 | public static CodeBuilder createCodeBuilder() { |
||
313 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
314 | return manager.createCodeBuilder();
|
||
315 | } |
||
316 | 44163 | jjdelcerro | } |