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 @ 45523
History | View | Annotate | Download (20.1 KB)
1 |
package org.gvsig.expressionevaluator; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.io.File; |
5 |
import java.util.List; |
6 |
import java.util.regex.Matcher; |
7 |
import java.util.regex.Pattern; |
8 |
import org.apache.commons.lang3.ArrayUtils; |
9 |
import org.apache.commons.lang3.StringUtils; |
10 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
11 |
import static org.gvsig.expressionevaluator.ExpressionEvaluatorManager.DYNAMICTEXT_ENDTAG; |
12 |
import static org.gvsig.expressionevaluator.ExpressionEvaluatorManager.DYNAMICTEXT_STARTTAG; |
13 |
import org.gvsig.tools.util.ListBuilder; |
14 |
|
15 |
/**
|
16 |
*
|
17 |
* @author gvSIG Team
|
18 |
*/
|
19 |
@SuppressWarnings("UseSpecificCatch") |
20 |
public class ExpressionUtils { |
21 |
|
22 |
public static boolean isEmpty(Expression expression) { |
23 |
return expression == null || expression.isEmpty(); |
24 |
} |
25 |
|
26 |
public static boolean isPhraseEmpty(Expression expression) { |
27 |
return expression == null || expression.isPhraseEmpty(); |
28 |
} |
29 |
|
30 |
public static Expression defaultIfEmpty(Expression expression, Expression defaultValue) { |
31 |
if( expression==null || expression.isEmpty() ) { |
32 |
return defaultValue;
|
33 |
} |
34 |
return expression;
|
35 |
} |
36 |
|
37 |
public static Expression defaultNullIfEmpty(Expression expression) { |
38 |
if( expression==null || expression.isEmpty() ) { |
39 |
return null; |
40 |
} |
41 |
return expression;
|
42 |
} |
43 |
|
44 |
public static Expression defaultIfPhraseEmpty(Expression expression, Expression defaultValue) { |
45 |
if( expression==null || expression.isPhraseEmpty() ) { |
46 |
return defaultValue;
|
47 |
} |
48 |
return expression;
|
49 |
} |
50 |
|
51 |
public static Expression defaultNullIfPhraseEmpty(Expression expression) { |
52 |
if( expression==null || expression.isPhraseEmpty() ) { |
53 |
return null; |
54 |
} |
55 |
return expression;
|
56 |
} |
57 |
|
58 |
public static Expression createExpression() { |
59 |
Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
60 |
return expression;
|
61 |
} |
62 |
|
63 |
public static Expression createExpression(String phrase) { |
64 |
if( StringUtils.isBlank(phrase) ) {
|
65 |
return null; |
66 |
} |
67 |
Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
68 |
expression.setPhrase(phrase); |
69 |
return expression;
|
70 |
} |
71 |
|
72 |
// public static Expression createExpression(String phrase, String code, Script... scripts) {
|
73 |
// Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
74 |
// expression.setPhrase(phrase);
|
75 |
// expression.setUserScript(code);
|
76 |
// for (Script script : scripts) {
|
77 |
// expression.addScript(script);
|
78 |
// }
|
79 |
// return expression;
|
80 |
// }
|
81 |
//
|
82 |
// public static Expression createExpression(String phrase, String code, String languaje, Script... scripts) {
|
83 |
// Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
84 |
// expression.setPhrase(phrase);
|
85 |
// expression.setUserScript(code, languaje);
|
86 |
// for (Script script : scripts) {
|
87 |
// expression.addScript(script);
|
88 |
// }
|
89 |
// return expression;
|
90 |
// }
|
91 |
|
92 |
public static ExpressionBuilder createExpressionBuilder() { |
93 |
ExpressionBuilder builder = ExpressionEvaluatorLocator.getManager().createExpressionBuilder(); |
94 |
return builder;
|
95 |
} |
96 |
|
97 |
public static Code compile(String expression) { |
98 |
if( StringUtils.isBlank(expression) ) {
|
99 |
return null; |
100 |
} |
101 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
102 |
Code code = manager.compile(expression); |
103 |
return code;
|
104 |
} |
105 |
|
106 |
public static Object evaluate(String expression) { |
107 |
return evaluate(null, expression); |
108 |
} |
109 |
|
110 |
public static Object evaluate(SymbolTable symbolTable, String expression) { |
111 |
if( StringUtils.isBlank(expression) ) {
|
112 |
return null; |
113 |
} |
114 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
115 |
Object x = manager.evaluate(symbolTable, expression);
|
116 |
return x;
|
117 |
} |
118 |
|
119 |
public static Object evaluate(SymbolTable symbolTable, Code code) { |
120 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
121 |
Object x = manager.evaluate(symbolTable, code);
|
122 |
return x;
|
123 |
} |
124 |
|
125 |
public static Code optimize(SymbolTable symbolTable, Code code) { |
126 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
127 |
code = manager.optimize(symbolTable, code); |
128 |
return code;
|
129 |
} |
130 |
|
131 |
public static String toString(Value value, Formatter formatter) { |
132 |
if( value == null ) { |
133 |
return null; |
134 |
} |
135 |
if( formatter==null ) { |
136 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
137 |
formatter = manager.getExpressionBuilderFormatter(); |
138 |
} |
139 |
return value.toString(formatter);
|
140 |
} |
141 |
|
142 |
public static String toString(Value value) { |
143 |
return toString(value, null); |
144 |
} |
145 |
|
146 |
public static String toString(Code code, Formatter formatter) { |
147 |
if( code == null ) { |
148 |
return null; |
149 |
} |
150 |
if( formatter==null ) { |
151 |
formatter = Code.EMPTY_FORMATTER; |
152 |
} |
153 |
return code.toString(formatter);
|
154 |
} |
155 |
|
156 |
public static String toString(Code code) { |
157 |
return toString(code, null); |
158 |
} |
159 |
|
160 |
public static Expression createExpressionFromJSON(String json) { |
161 |
Expression expression = ExpressionUtils.createExpression();
|
162 |
expression.fromJSON(json); |
163 |
return expression;
|
164 |
} |
165 |
|
166 |
public static MutableSymbolTable createSymbolTable() { |
167 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
168 |
MutableSymbolTable symbolTable = manager.createSymbolTable(); |
169 |
return symbolTable;
|
170 |
} |
171 |
|
172 |
public static String surroundByDynamicTextTag(String source) { |
173 |
return surroundByDynamicTextTag(source, true); |
174 |
} |
175 |
|
176 |
public static String surroundByDynamicTextTag(String source, boolean insert) { |
177 |
if( source==null ) { |
178 |
return null; |
179 |
} |
180 |
if( insert ) {
|
181 |
return DYNAMICTEXT_STARTTAG+ "=" + source + DYNAMICTEXT_ENDTAG; |
182 |
} |
183 |
return DYNAMICTEXT_STARTTAG + source + DYNAMICTEXT_ENDTAG;
|
184 |
} |
185 |
|
186 |
public static boolean isDynamicText(String source) { |
187 |
if( !source.contains(DYNAMICTEXT_STARTTAG) ) {
|
188 |
return false; |
189 |
} |
190 |
String[] sources = StringUtils.substringsBetween(source, DYNAMICTEXT_STARTTAG, DYNAMICTEXT_ENDTAG); |
191 |
if( ArrayUtils.isEmpty(sources) ) {
|
192 |
return false; |
193 |
} |
194 |
return true; |
195 |
} |
196 |
|
197 |
public static String evaluateDynamicText(String source) { |
198 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
199 |
return manager.evaluateDynamicText(source);
|
200 |
} |
201 |
|
202 |
public static String evaluateDynamicText(SymbolTable symbolTable, String source) { |
203 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
204 |
return manager.evaluateDynamicText(symbolTable, source);
|
205 |
} |
206 |
|
207 |
public static File evaluateFilename(File source) { |
208 |
return evaluateFilename(null, source); |
209 |
} |
210 |
|
211 |
public static boolean isDynamicFilename(File source) { |
212 |
if( source == null ) { |
213 |
return false; |
214 |
} |
215 |
return isDynamicText(source.getPath());
|
216 |
} |
217 |
|
218 |
public static File createDynamicFile(Value builder) { |
219 |
File f = new File(surroundByDynamicTextTag(builder.toString())); |
220 |
return f;
|
221 |
} |
222 |
|
223 |
public static File createDynamicFile(String expression) { |
224 |
File f = new File(surroundByDynamicTextTag(expression)); |
225 |
return f;
|
226 |
} |
227 |
|
228 |
@SuppressWarnings("StringEquality") |
229 |
public static File evaluateFilename(SymbolTable symbolTable, File source) { |
230 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
231 |
String src = source.getPath();
|
232 |
String r = manager.evaluateDynamicText(symbolTable, src);
|
233 |
if( r == src ) { // !!! I compare that it is the same pointer, it is what I want. |
234 |
return source;
|
235 |
} |
236 |
File f = new File(r); |
237 |
return f;
|
238 |
} |
239 |
|
240 |
public static int parseInt(String s) throws NumberFormatException { |
241 |
if( StringUtils.isBlank(s) ) {
|
242 |
throw new NumberFormatException("Can't get integer from a blank string."); |
243 |
} |
244 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
245 |
SymbolTable symbolTable = null; //manager.getInmutableSymbolTable(); |
246 |
Object x;
|
247 |
try {
|
248 |
x = manager.evaluate(symbolTable, s); |
249 |
if( x instanceof Number ) { |
250 |
return ((Number) x).intValue(); |
251 |
} |
252 |
} catch(Exception ex) { |
253 |
NumberFormatException ex1 = new NumberFormatException("Can't get integer from '"+s+"'."); |
254 |
ex1.initCause(ex); |
255 |
throw ex;
|
256 |
} |
257 |
if( x == null ) { |
258 |
throw new NumberFormatException("Can't get integer from '"+s+"' value is null."); |
259 |
} |
260 |
throw new NumberFormatException("Can't get integer from '"+s+"' value is a "+x.getClass().getSimpleName()+"."); |
261 |
} |
262 |
|
263 |
public static long parseLong(String s) throws NumberFormatException { |
264 |
if( StringUtils.isBlank(s) ) {
|
265 |
throw new NumberFormatException("Can't get long from a blank string."); |
266 |
} |
267 |
try {
|
268 |
int value = Integer.parseInt(s); |
269 |
return value;
|
270 |
} catch(Exception ex) { |
271 |
|
272 |
} |
273 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
274 |
SymbolTable symbolTable = null; //manager.getInmutableSymbolTable(); |
275 |
Object x;
|
276 |
try {
|
277 |
x = manager.evaluate(symbolTable, s); |
278 |
if( x instanceof Number ) { |
279 |
return ((Number) x).longValue(); |
280 |
} |
281 |
} catch(Exception ex) { |
282 |
NumberFormatException ex1 = new NumberFormatException("Can't get long from '"+s+"'."); |
283 |
ex1.initCause(ex); |
284 |
throw ex;
|
285 |
} |
286 |
if( x == null ) { |
287 |
throw new NumberFormatException("Can't get long from '"+s+"' value is null."); |
288 |
} |
289 |
throw new NumberFormatException("Can't get long from '"+s+"' value is a "+x.getClass().getSimpleName()+"."); |
290 |
} |
291 |
|
292 |
public static double parseDouble(String s) throws NumberFormatException { |
293 |
if( StringUtils.isBlank(s) ) {
|
294 |
throw new NumberFormatException("Can't get double from a blank string."); |
295 |
} |
296 |
try {
|
297 |
double value = Double.parseDouble(s); |
298 |
return value;
|
299 |
} catch(Exception ex) { |
300 |
|
301 |
} |
302 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
303 |
SymbolTable symbolTable = null; //manager.getInmutableSymbolTable(); |
304 |
Object x;
|
305 |
try {
|
306 |
x = manager.evaluate(symbolTable, s); |
307 |
if( x instanceof Number ) { |
308 |
return ((Number) x).doubleValue(); |
309 |
} |
310 |
} catch(Exception ex) { |
311 |
NumberFormatException ex1 = new NumberFormatException("Can't get double from '"+s+"'."); |
312 |
ex1.initCause(ex); |
313 |
throw ex;
|
314 |
} |
315 |
if( x == null ) { |
316 |
throw new NumberFormatException("Can't get double from '"+s+"' value is null."); |
317 |
} |
318 |
throw new NumberFormatException("Can't get double from '"+s+"' value is a "+x.getClass().getSimpleName()+"."); |
319 |
} |
320 |
|
321 |
public static Compiler createCompiler() { |
322 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
323 |
Compiler compiler = manager.createCompiler();
|
324 |
return compiler;
|
325 |
} |
326 |
|
327 |
public static Interpreter createInterpreter() { |
328 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
329 |
Interpreter interpreter = manager.createInterpreter(); |
330 |
return interpreter;
|
331 |
} |
332 |
|
333 |
public static Optimizer createOptimizer() { |
334 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
335 |
Optimizer optimizer = manager.createOptimizer(); |
336 |
return optimizer;
|
337 |
} |
338 |
|
339 |
public static String repr(Object value) { |
340 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
341 |
ReprMethod method = manager.getReprMethod(value); |
342 |
return method.repr(value);
|
343 |
} |
344 |
|
345 |
public static CodeBuilder createCodeBuilder() { |
346 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
347 |
return manager.createCodeBuilder();
|
348 |
} |
349 |
|
350 |
private static final List<String> TRUE_VALUES = ListBuilder.create("true","on","t", "1", "-1" ); |
351 |
private static final List<String> FALSE_VALUES = ListBuilder.create("false","off","f", "0" ); |
352 |
|
353 |
public static boolean parseBoolean(SymbolTable symbolTable, String expression, boolean defaultValue) { |
354 |
expression = StringUtils.trimToNull(expression); |
355 |
if( StringUtils.isBlank(expression) ) {
|
356 |
return defaultValue;
|
357 |
} |
358 |
if( TRUE_VALUES.contains(expression.toLowerCase())) {
|
359 |
return true; |
360 |
} |
361 |
if( FALSE_VALUES.contains(expression.toLowerCase())) {
|
362 |
return false; |
363 |
} |
364 |
try {
|
365 |
return (boolean) ExpressionUtils.evaluate(symbolTable, expression); |
366 |
} catch(Exception ex) { |
367 |
return defaultValue;
|
368 |
} |
369 |
} |
370 |
|
371 |
public static boolean parseBoolean(String expression) { |
372 |
expression = StringUtils.trimToNull(expression); |
373 |
if( StringUtils.isBlank(expression) ) {
|
374 |
throw new IllegalArgumentException("Can't get boolean from a blank string."); |
375 |
} |
376 |
if( TRUE_VALUES.contains(expression.toLowerCase())) {
|
377 |
return true; |
378 |
} |
379 |
if( FALSE_VALUES.contains(expression.toLowerCase())) {
|
380 |
return false; |
381 |
} |
382 |
Object x;
|
383 |
try {
|
384 |
x = ExpressionUtils.evaluate(null, expression);
|
385 |
return (boolean) x; |
386 |
} catch(Exception ex) { |
387 |
IllegalArgumentException ex1 = new IllegalArgumentException("Can't get boolean from '"+expression+"'."); |
388 |
ex1.initCause(ex); |
389 |
throw ex;
|
390 |
} |
391 |
} |
392 |
|
393 |
public static int parseInt(SymbolTable symbolTable, String expression, int defaultValue) { |
394 |
if( StringUtils.isBlank(expression) ) {
|
395 |
return defaultValue;
|
396 |
} |
397 |
try {
|
398 |
int value = Integer.parseInt(expression); |
399 |
return value;
|
400 |
} catch(Exception ex) { |
401 |
|
402 |
} |
403 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
404 |
Object x;
|
405 |
try {
|
406 |
x = manager.evaluate(symbolTable, expression); |
407 |
if( x instanceof Number ) { |
408 |
return ((Number) x).intValue(); |
409 |
} |
410 |
} catch(Exception ex) { |
411 |
} |
412 |
return defaultValue;
|
413 |
} |
414 |
|
415 |
public static long parseLong(SymbolTable symbolTable, String expression, long defaultValue) { |
416 |
if( StringUtils.isBlank(expression) ) {
|
417 |
return defaultValue;
|
418 |
} |
419 |
try {
|
420 |
int value = Integer.parseInt(expression); |
421 |
return value;
|
422 |
} catch(Exception ex) { |
423 |
|
424 |
} |
425 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
426 |
Object x;
|
427 |
try {
|
428 |
x = manager.evaluate(symbolTable, expression); |
429 |
if( x instanceof Number ) { |
430 |
return ((Number) x).longValue(); |
431 |
} |
432 |
} catch(Exception ex) { |
433 |
} |
434 |
return defaultValue;
|
435 |
} |
436 |
|
437 |
public static double parseDouble(SymbolTable symbolTable, String expression, double defaultValue) { |
438 |
if( StringUtils.isBlank(expression) ) {
|
439 |
return defaultValue;
|
440 |
} |
441 |
try {
|
442 |
double value = Double.parseDouble(expression); |
443 |
return value;
|
444 |
} catch(Exception ex) { |
445 |
|
446 |
} |
447 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
448 |
Object x;
|
449 |
try {
|
450 |
x = manager.evaluate(symbolTable, expression); |
451 |
if( x instanceof Number ) { |
452 |
return ((Number) x).doubleValue(); |
453 |
} |
454 |
} catch(Exception ex) { |
455 |
} |
456 |
return defaultValue;
|
457 |
} |
458 |
|
459 |
private static final Pattern COLOR_PATTERN3 = Pattern.compile("COLOR[(][ ]*(?<R>[0-9]{1,3})[ ]*,[ ]*(?<G>[0-9]{1,3})[ ]*,[ ]*(?<A>[0-9]{1,3})[ ]*[)]", Pattern.CASE_INSENSITIVE); |
460 |
private static final Pattern COLOR_PATTERN4 = Pattern.compile("COLOR[(][ ]*(?<R>[0-9]{1,3})[ ]*,[ ]*(?<G>[0-9]{1,3})[ ]*,[ ]*(?<B>[0-9]{1,3})[ ]*,[ ]*(?<A>[0-9]{1,3})[ ]*[)]", Pattern.CASE_INSENSITIVE); |
461 |
|
462 |
public static Color parseColor(SymbolTable symbolTable, String expression, Color defaultValue) { |
463 |
if( StringUtils.isBlank(expression) ) {
|
464 |
return defaultValue;
|
465 |
} |
466 |
try {
|
467 |
if( StringUtils.startsWithIgnoreCase(expression, "color(") && |
468 |
StringUtils.endsWithIgnoreCase(expression, ")")) {
|
469 |
Matcher m = COLOR_PATTERN4.matcher(expression);
|
470 |
if( m != null && m.matches()) { |
471 |
Color color = new Color( |
472 |
Integer.valueOf(m.group("R")), |
473 |
Integer.valueOf(m.group("G")), |
474 |
Integer.valueOf(m.group("B")), |
475 |
Integer.valueOf(m.group("A")) |
476 |
); |
477 |
return color;
|
478 |
} |
479 |
m = COLOR_PATTERN3.matcher(expression); |
480 |
if( m != null && m.matches()) { |
481 |
Color color = new Color( |
482 |
Integer.valueOf(m.group("R")), |
483 |
Integer.valueOf(m.group("G")), |
484 |
Integer.valueOf(m.group("B")), |
485 |
Integer.valueOf(m.group("A")) |
486 |
); |
487 |
return color;
|
488 |
} |
489 |
} |
490 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
491 |
Object x;
|
492 |
x = manager.evaluate(symbolTable, expression); |
493 |
if( x instanceof Color ) { |
494 |
return (Color) x; |
495 |
} |
496 |
if( x instanceof Number ) { |
497 |
return new Color(((Number) x).intValue()); |
498 |
} |
499 |
} catch(Exception ex) { |
500 |
} |
501 |
return defaultValue;
|
502 |
} |
503 |
|
504 |
public static Color parseColor(String expression) { |
505 |
if( StringUtils.isBlank(expression) ) {
|
506 |
throw new IllegalArgumentException("Can't get color from a blank string."); |
507 |
} |
508 |
Object x;
|
509 |
try {
|
510 |
if( StringUtils.startsWithIgnoreCase(expression, "color(") && |
511 |
StringUtils.endsWithIgnoreCase(expression, ")")) {
|
512 |
Matcher m = COLOR_PATTERN4.matcher(expression);
|
513 |
if( m != null && m.matches()) { |
514 |
Color color = new Color( |
515 |
Integer.valueOf(m.group("R")), |
516 |
Integer.valueOf(m.group("G")), |
517 |
Integer.valueOf(m.group("B")), |
518 |
Integer.valueOf(m.group("A")) |
519 |
); |
520 |
return color;
|
521 |
} |
522 |
m = COLOR_PATTERN3.matcher(expression); |
523 |
if( m != null && m.matches()) { |
524 |
Color color = new Color( |
525 |
Integer.valueOf(m.group("R")), |
526 |
Integer.valueOf(m.group("G")), |
527 |
Integer.valueOf(m.group("B")), |
528 |
Integer.valueOf(m.group("A")) |
529 |
); |
530 |
return color;
|
531 |
} |
532 |
} |
533 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
534 |
x = manager.evaluate(null, expression);
|
535 |
if( x instanceof Color ) { |
536 |
return (Color) x; |
537 |
} |
538 |
if( x instanceof Number ) { |
539 |
return new Color(((Number) x).intValue()); |
540 |
} |
541 |
} catch(Exception ex) { |
542 |
IllegalArgumentException ex1 = new IllegalArgumentException("Can't get color from '"+expression+"'."); |
543 |
ex1.initCause(ex); |
544 |
throw ex;
|
545 |
} |
546 |
if( x == null ) { |
547 |
throw new IllegalArgumentException("Can't get double from '"+expression+"' value is null."); |
548 |
} |
549 |
throw new IllegalArgumentException("Can't get double from '"+expression+"' value is a "+x.getClass().getSimpleName()+"."); |
550 |
} |
551 |
|
552 |
} |
553 |
|