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 @ 46854
History | View | Annotate | Download (25.9 KB)
1 |
package org.gvsig.expressionevaluator; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.io.File; |
5 |
import java.net.MalformedURLException; |
6 |
import java.net.URL; |
7 |
import java.nio.charset.StandardCharsets; |
8 |
import java.util.List; |
9 |
import java.util.regex.Matcher; |
10 |
import java.util.regex.Pattern; |
11 |
import org.apache.commons.io.IOUtils; |
12 |
import org.apache.commons.lang3.ArrayUtils; |
13 |
import org.apache.commons.lang3.StringUtils; |
14 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
15 |
import static org.gvsig.expressionevaluator.ExpressionEvaluatorManager.DYNAMICTEXT_ENDTAG; |
16 |
import static org.gvsig.expressionevaluator.ExpressionEvaluatorManager.DYNAMICTEXT_STARTTAG; |
17 |
import org.gvsig.expressionevaluator.spi.DynObjectSymbolTable; |
18 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
19 |
import org.gvsig.tools.dispose.DisposeUtils; |
20 |
import org.gvsig.tools.dynobject.DynObject; |
21 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
22 |
import org.gvsig.tools.script.Script; |
23 |
import org.gvsig.tools.util.ListBuilder; |
24 |
import org.slf4j.Logger; |
25 |
import org.slf4j.LoggerFactory; |
26 |
|
27 |
/**
|
28 |
*
|
29 |
* @author gvSIG Team
|
30 |
*/
|
31 |
@SuppressWarnings("UseSpecificCatch") |
32 |
public class ExpressionUtils { |
33 |
|
34 |
private static final Logger LOGGER = LoggerFactory.getLogger(ExpressionUtils.class); |
35 |
|
36 |
public static boolean isEmpty(Expression expression) { |
37 |
return expression == null || expression.isEmpty(); |
38 |
} |
39 |
|
40 |
public static boolean isPhraseEmpty(Expression expression) { |
41 |
return expression == null || expression.isPhraseEmpty(); |
42 |
} |
43 |
|
44 |
public static Expression defaultIfEmpty(Expression expression, Expression defaultValue) { |
45 |
if( expression==null || expression.isEmpty() ) { |
46 |
return defaultValue;
|
47 |
} |
48 |
return expression;
|
49 |
} |
50 |
|
51 |
public static Expression defaultNullIfEmpty(Expression expression) { |
52 |
if( expression==null || expression.isEmpty() ) { |
53 |
return null; |
54 |
} |
55 |
return expression;
|
56 |
} |
57 |
|
58 |
public static Expression defaultIfPhraseEmpty(Expression expression, Expression defaultValue) { |
59 |
if( expression==null || expression.isPhraseEmpty() ) { |
60 |
return defaultValue;
|
61 |
} |
62 |
return expression;
|
63 |
} |
64 |
|
65 |
public static Expression defaultNullIfPhraseEmpty(Expression expression) { |
66 |
if( expression==null || expression.isPhraseEmpty() ) { |
67 |
return null; |
68 |
} |
69 |
return expression;
|
70 |
} |
71 |
|
72 |
public static Expression createExpression() { |
73 |
Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
74 |
return expression;
|
75 |
} |
76 |
|
77 |
public static Expression createExpression(String phrase) { |
78 |
if( StringUtils.isBlank(phrase) ) {
|
79 |
return null; |
80 |
} |
81 |
Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
82 |
expression.setPhrase(phrase); |
83 |
return expression;
|
84 |
} |
85 |
|
86 |
public static Expression createExpression(Expression expression) { |
87 |
return expression;
|
88 |
} |
89 |
|
90 |
// public static Expression createExpression(String phrase, String code, Script... scripts) {
|
91 |
// Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
92 |
// expression.setPhrase(phrase);
|
93 |
// expression.setUserScript(code);
|
94 |
// for (Script script : scripts) {
|
95 |
// expression.addScript(script);
|
96 |
// }
|
97 |
// return expression;
|
98 |
// }
|
99 |
//
|
100 |
// public static Expression createExpression(String phrase, String code, String languaje, Script... scripts) {
|
101 |
// Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
102 |
// expression.setPhrase(phrase);
|
103 |
// expression.setUserScript(code, languaje);
|
104 |
// for (Script script : scripts) {
|
105 |
// expression.addScript(script);
|
106 |
// }
|
107 |
// return expression;
|
108 |
// }
|
109 |
|
110 |
public static ExpressionBuilder createExpressionBuilder() { |
111 |
ExpressionBuilder builder = ExpressionEvaluatorLocator.getManager().createExpressionBuilder(); |
112 |
return builder;
|
113 |
} |
114 |
|
115 |
public static Code compile(String expression) { |
116 |
if( StringUtils.isBlank(expression) ) {
|
117 |
return null; |
118 |
} |
119 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
120 |
Code code = manager.compile(expression); |
121 |
return code;
|
122 |
} |
123 |
|
124 |
public static Object evaluate(String expression) { |
125 |
return evaluate(null, expression); |
126 |
} |
127 |
|
128 |
public static Object evaluate(SymbolTable symbolTable, String expression) { |
129 |
if( StringUtils.isBlank(expression) ) {
|
130 |
return null; |
131 |
} |
132 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
133 |
Object x = manager.evaluate(symbolTable, expression);
|
134 |
return x;
|
135 |
} |
136 |
|
137 |
public static Object evaluate(SymbolTable symbolTable, Code code) { |
138 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
139 |
Object x = manager.evaluate(symbolTable, code);
|
140 |
return x;
|
141 |
} |
142 |
|
143 |
public static Code optimize(SymbolTable symbolTable, Code code) { |
144 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
145 |
code = manager.optimize(symbolTable, code); |
146 |
return code;
|
147 |
} |
148 |
|
149 |
public static String toString(Value value, Formatter formatter) { |
150 |
if( value == null ) { |
151 |
return null; |
152 |
} |
153 |
if( formatter==null ) { |
154 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
155 |
formatter = manager.getExpressionBuilderFormatter(); |
156 |
} |
157 |
return value.toString(formatter);
|
158 |
} |
159 |
|
160 |
public static String toString(Value value) { |
161 |
return toString(value, null); |
162 |
} |
163 |
|
164 |
public static String toString(Code code, Formatter formatter) { |
165 |
if( code == null ) { |
166 |
return null; |
167 |
} |
168 |
if( formatter==null ) { |
169 |
formatter = Code.EMPTY_FORMATTER; |
170 |
} |
171 |
return code.toString(formatter);
|
172 |
} |
173 |
|
174 |
public static String toString(Code code) { |
175 |
return toString(code, null); |
176 |
} |
177 |
|
178 |
public static Expression createExpressionFromJSON(String json) { |
179 |
Expression expression = ExpressionUtils.createExpression();
|
180 |
expression.fromJSON(json); |
181 |
return expression;
|
182 |
} |
183 |
|
184 |
public static MutableSymbolTable createSymbolTable() { |
185 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
186 |
MutableSymbolTable symbolTable = manager.createSymbolTable(); |
187 |
return symbolTable;
|
188 |
} |
189 |
|
190 |
public static MutableSymbolTable createSymbolTable(DynObject obj) { |
191 |
DynObjectSymbolTable symbolTable = new DynObjectSymbolTable("object",obj); |
192 |
return symbolTable;
|
193 |
} |
194 |
|
195 |
public static MutableSymbolTable createSymbolTable(String name, DynObject obj) { |
196 |
DynObjectSymbolTable symbolTable = new DynObjectSymbolTable(name, obj);
|
197 |
return symbolTable;
|
198 |
} |
199 |
|
200 |
public static String surroundByDynamicTextTag(String source) { |
201 |
return surroundByDynamicTextTag(source, true); |
202 |
} |
203 |
|
204 |
public static String surroundByDynamicTextTag(String source, boolean insert) { |
205 |
if( source==null ) { |
206 |
return null; |
207 |
} |
208 |
if( insert ) {
|
209 |
return DYNAMICTEXT_STARTTAG+ "=" + source + DYNAMICTEXT_ENDTAG; |
210 |
} |
211 |
return DYNAMICTEXT_STARTTAG + source + DYNAMICTEXT_ENDTAG;
|
212 |
} |
213 |
|
214 |
public static boolean isDynamicText(String source) { |
215 |
if( StringUtils.isBlank(source) || !source.contains(DYNAMICTEXT_STARTTAG) ) {
|
216 |
return false; |
217 |
} |
218 |
String[] sources = StringUtils.substringsBetween(source, DYNAMICTEXT_STARTTAG, DYNAMICTEXT_ENDTAG); |
219 |
if( ArrayUtils.isEmpty(sources) ) {
|
220 |
return false; |
221 |
} |
222 |
return true; |
223 |
} |
224 |
|
225 |
public static String evaluateDynamicText(String source) { |
226 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
227 |
return manager.evaluateDynamicText(source);
|
228 |
} |
229 |
|
230 |
public static String evaluateDynamicText(SymbolTable symbolTable, String source) { |
231 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
232 |
return manager.evaluateDynamicText(symbolTable, source);
|
233 |
} |
234 |
|
235 |
public static File evaluateFilename(File source) { |
236 |
return evaluateFilename(null, source); |
237 |
} |
238 |
|
239 |
public static URL evaluateURL(URL source) { |
240 |
return evaluateURL(null, source); |
241 |
} |
242 |
|
243 |
public static boolean isDynamicFilename(File source) { |
244 |
if( source == null ) { |
245 |
return false; |
246 |
} |
247 |
return isDynamicText(source.getPath());
|
248 |
} |
249 |
|
250 |
public static boolean isDynamicURL(URL source) { |
251 |
if( source == null ) { |
252 |
return false; |
253 |
} |
254 |
return isDynamicText(source.toString());
|
255 |
} |
256 |
|
257 |
public static File createDynamicFile(Value builder) { |
258 |
File f = new File(surroundByDynamicTextTag(builder.toString())); |
259 |
return f;
|
260 |
} |
261 |
|
262 |
public static File createDynamicFile(String expression) { |
263 |
File f = new File(surroundByDynamicTextTag(expression)); |
264 |
return f;
|
265 |
} |
266 |
|
267 |
@SuppressWarnings("StringEquality") |
268 |
public static File evaluateFilename(SymbolTable symbolTable, File source) { |
269 |
String src = source.getPath();
|
270 |
if( !isDynamicText(src) ) {
|
271 |
return source;
|
272 |
} |
273 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
274 |
String r = manager.evaluateDynamicText(symbolTable, src);
|
275 |
if( r == src ) { // !!! I compare that it is the same pointer, it is what I want. |
276 |
return source;
|
277 |
} |
278 |
File f = new File(r); |
279 |
return f;
|
280 |
} |
281 |
|
282 |
@SuppressWarnings("StringEquality") |
283 |
public static URL evaluateURL(SymbolTable symbolTable, URL source) { |
284 |
String src = source.toString();
|
285 |
if( !isDynamicText(src) ) {
|
286 |
return source;
|
287 |
} |
288 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
289 |
String r = manager.evaluateDynamicText(symbolTable, src);
|
290 |
if( r == src ) { // !!! I compare that it is the same pointer, it is what I want. |
291 |
return source;
|
292 |
} |
293 |
try {
|
294 |
URL url = new URL(r); |
295 |
return url;
|
296 |
} catch (MalformedURLException ex) { |
297 |
return source;
|
298 |
} |
299 |
} |
300 |
|
301 |
public static int parseInt(String s) throws NumberFormatException { |
302 |
if( StringUtils.isBlank(s) ) {
|
303 |
throw new NumberFormatException("Can't get integer from a blank string."); |
304 |
} |
305 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
306 |
SymbolTable symbolTable = null; //manager.getInmutableSymbolTable(); |
307 |
Object x;
|
308 |
try {
|
309 |
x = manager.evaluate(symbolTable, s); |
310 |
if( x instanceof Number ) { |
311 |
return ((Number) x).intValue(); |
312 |
} |
313 |
} catch(Exception ex) { |
314 |
NumberFormatException ex1 = new NumberFormatException("Can't get integer from '"+s+"'."); |
315 |
ex1.initCause(ex); |
316 |
throw ex;
|
317 |
} |
318 |
if( x == null ) { |
319 |
throw new NumberFormatException("Can't get integer from '"+s+"' value is null."); |
320 |
} |
321 |
throw new NumberFormatException("Can't get integer from '"+s+"' value is a "+x.getClass().getSimpleName()+"."); |
322 |
} |
323 |
|
324 |
public static long parseLong(String s) throws NumberFormatException { |
325 |
if( StringUtils.isBlank(s) ) {
|
326 |
throw new NumberFormatException("Can't get long from a blank string."); |
327 |
} |
328 |
try {
|
329 |
int value = Integer.parseInt(s); |
330 |
return value;
|
331 |
} catch(Exception ex) { |
332 |
|
333 |
} |
334 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
335 |
SymbolTable symbolTable = null; //manager.getInmutableSymbolTable(); |
336 |
Object x;
|
337 |
try {
|
338 |
x = manager.evaluate(symbolTable, s); |
339 |
if( x instanceof Number ) { |
340 |
return ((Number) x).longValue(); |
341 |
} |
342 |
} catch(Exception ex) { |
343 |
NumberFormatException ex1 = new NumberFormatException("Can't get long from '"+s+"'."); |
344 |
ex1.initCause(ex); |
345 |
throw ex;
|
346 |
} |
347 |
if( x == null ) { |
348 |
throw new NumberFormatException("Can't get long from '"+s+"' value is null."); |
349 |
} |
350 |
throw new NumberFormatException("Can't get long from '"+s+"' value is a "+x.getClass().getSimpleName()+"."); |
351 |
} |
352 |
|
353 |
public static double parseDouble(String s) throws NumberFormatException { |
354 |
if( StringUtils.isBlank(s) ) {
|
355 |
throw new NumberFormatException("Can't get double from a blank string."); |
356 |
} |
357 |
try {
|
358 |
double value = Double.parseDouble(s); |
359 |
return value;
|
360 |
} catch(Exception ex) { |
361 |
|
362 |
} |
363 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
364 |
SymbolTable symbolTable = null; //manager.getInmutableSymbolTable(); |
365 |
Object x;
|
366 |
try {
|
367 |
x = manager.evaluate(symbolTable, s); |
368 |
if( x instanceof Number ) { |
369 |
return ((Number) x).doubleValue(); |
370 |
} |
371 |
} catch(Exception ex) { |
372 |
NumberFormatException ex1 = new NumberFormatException("Can't get double from '"+s+"'."); |
373 |
ex1.initCause(ex); |
374 |
throw ex;
|
375 |
} |
376 |
if( x == null ) { |
377 |
throw new NumberFormatException("Can't get double from '"+s+"' value is null."); |
378 |
} |
379 |
throw new NumberFormatException("Can't get double from '"+s+"' value is a "+x.getClass().getSimpleName()+"."); |
380 |
} |
381 |
|
382 |
public static Compiler createCompiler() { |
383 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
384 |
Compiler compiler = manager.createCompiler();
|
385 |
return compiler;
|
386 |
} |
387 |
|
388 |
public static Interpreter createInterpreter() { |
389 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
390 |
Interpreter interpreter = manager.createInterpreter(); |
391 |
return interpreter;
|
392 |
} |
393 |
|
394 |
public static Optimizer createOptimizer() { |
395 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
396 |
Optimizer optimizer = manager.createOptimizer(); |
397 |
return optimizer;
|
398 |
} |
399 |
|
400 |
public static String repr(Object value) { |
401 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
402 |
ReprMethod method = manager.getReprMethod(value); |
403 |
return method.repr(value);
|
404 |
} |
405 |
|
406 |
public static CodeBuilder createCodeBuilder() { |
407 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
408 |
return manager.createCodeBuilder();
|
409 |
} |
410 |
|
411 |
private static final List<String> TRUE_VALUES = ListBuilder.create("true","on","t", "1", "-1" ); |
412 |
private static final List<String> FALSE_VALUES = ListBuilder.create("false","off","f", "0" ); |
413 |
|
414 |
public static boolean parseBoolean(SymbolTable symbolTable, String expression, boolean defaultValue) { |
415 |
expression = StringUtils.trimToNull(expression); |
416 |
if( StringUtils.isBlank(expression) ) {
|
417 |
return defaultValue;
|
418 |
} |
419 |
if( TRUE_VALUES.contains(expression.toLowerCase())) {
|
420 |
return true; |
421 |
} |
422 |
if( FALSE_VALUES.contains(expression.toLowerCase())) {
|
423 |
return false; |
424 |
} |
425 |
try {
|
426 |
return (boolean) ExpressionUtils.evaluate(symbolTable, expression); |
427 |
} catch(Exception ex) { |
428 |
return defaultValue;
|
429 |
} |
430 |
} |
431 |
|
432 |
public static boolean parseBoolean(String expression) { |
433 |
expression = StringUtils.trimToNull(expression); |
434 |
if( StringUtils.isBlank(expression) ) {
|
435 |
throw new IllegalArgumentException("Can't get boolean from a blank string."); |
436 |
} |
437 |
if( TRUE_VALUES.contains(expression.toLowerCase())) {
|
438 |
return true; |
439 |
} |
440 |
if( FALSE_VALUES.contains(expression.toLowerCase())) {
|
441 |
return false; |
442 |
} |
443 |
Object x;
|
444 |
try {
|
445 |
x = ExpressionUtils.evaluate(null, expression);
|
446 |
return (boolean) x; |
447 |
} catch(Exception ex) { |
448 |
IllegalArgumentException ex1 = new IllegalArgumentException("Can't get boolean from '"+expression+"'."); |
449 |
ex1.initCause(ex); |
450 |
throw ex;
|
451 |
} |
452 |
} |
453 |
|
454 |
public static int parseInt(SymbolTable symbolTable, String expression, int defaultValue) { |
455 |
if( StringUtils.isBlank(expression) ) {
|
456 |
return defaultValue;
|
457 |
} |
458 |
try {
|
459 |
int value = Integer.parseInt(expression); |
460 |
return value;
|
461 |
} catch(Exception ex) { |
462 |
|
463 |
} |
464 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
465 |
Object x;
|
466 |
try {
|
467 |
x = manager.evaluate(symbolTable, expression); |
468 |
if( x instanceof Number ) { |
469 |
return ((Number) x).intValue(); |
470 |
} |
471 |
} catch(Exception ex) { |
472 |
} |
473 |
return defaultValue;
|
474 |
} |
475 |
|
476 |
public static long parseLong(SymbolTable symbolTable, String expression, long defaultValue) { |
477 |
if( StringUtils.isBlank(expression) ) {
|
478 |
return defaultValue;
|
479 |
} |
480 |
try {
|
481 |
int value = Integer.parseInt(expression); |
482 |
return value;
|
483 |
} catch(Exception ex) { |
484 |
|
485 |
} |
486 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
487 |
Object x;
|
488 |
try {
|
489 |
x = manager.evaluate(symbolTable, expression); |
490 |
if( x instanceof Number ) { |
491 |
return ((Number) x).longValue(); |
492 |
} |
493 |
} catch(Exception ex) { |
494 |
} |
495 |
return defaultValue;
|
496 |
} |
497 |
|
498 |
public static double parseDouble(SymbolTable symbolTable, String expression, double defaultValue) { |
499 |
if( StringUtils.isBlank(expression) ) {
|
500 |
return defaultValue;
|
501 |
} |
502 |
try {
|
503 |
double value = Double.parseDouble(expression); |
504 |
return value;
|
505 |
} catch(Exception ex) { |
506 |
|
507 |
} |
508 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
509 |
Object x;
|
510 |
try {
|
511 |
x = manager.evaluate(symbolTable, expression); |
512 |
if( x instanceof Number ) { |
513 |
return ((Number) x).doubleValue(); |
514 |
} |
515 |
} catch(Exception ex) { |
516 |
} |
517 |
return defaultValue;
|
518 |
} |
519 |
|
520 |
private static final Pattern COLOR_PATTERN3 = Pattern.compile("COLOR[(][ ]*(?<R>[0-9]{1,3})[ ]*,[ ]*(?<G>[0-9]{1,3})[ ]*,[ ]*(?<B>[0-9]{1,3})[ ]*[)]", Pattern.CASE_INSENSITIVE); |
521 |
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); |
522 |
|
523 |
public static Color parseColor(SymbolTable symbolTable, String expression, Color defaultValue) { |
524 |
if( StringUtils.isBlank(expression) ) {
|
525 |
return defaultValue;
|
526 |
} |
527 |
try {
|
528 |
Color color;
|
529 |
if( StringUtils.startsWithIgnoreCase(expression, "color(") && |
530 |
StringUtils.endsWithIgnoreCase(expression, ")")) {
|
531 |
Matcher m = COLOR_PATTERN4.matcher(expression);
|
532 |
if( m != null && m.matches()) { |
533 |
color = new Color( |
534 |
Integer.valueOf(m.group("R")), |
535 |
Integer.valueOf(m.group("G")), |
536 |
Integer.valueOf(m.group("B")), |
537 |
Integer.valueOf(m.group("A")) |
538 |
); |
539 |
return color;
|
540 |
} |
541 |
m = COLOR_PATTERN3.matcher(expression); |
542 |
if( m != null && m.matches()) { |
543 |
color = new Color( |
544 |
Integer.valueOf(m.group("R")), |
545 |
Integer.valueOf(m.group("G")), |
546 |
Integer.valueOf(m.group("B")) |
547 |
); |
548 |
return color;
|
549 |
} |
550 |
} |
551 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
552 |
Object x;
|
553 |
x = manager.evaluate(symbolTable, expression); |
554 |
if( x instanceof Color ) { |
555 |
return (Color) x; |
556 |
} |
557 |
color = (Color) DataTypeUtils.coerce(DataTypes.COLOR, expression, null); |
558 |
if( color != null ) { |
559 |
return color;
|
560 |
} |
561 |
} catch(Exception ex) { |
562 |
} |
563 |
return defaultValue;
|
564 |
} |
565 |
|
566 |
public static Color parseColor(String expression) { |
567 |
if( StringUtils.isBlank(expression) ) {
|
568 |
throw new IllegalArgumentException("Can't get color from a blank string."); |
569 |
} |
570 |
Object x;
|
571 |
try {
|
572 |
if( StringUtils.startsWithIgnoreCase(expression, "color(") && |
573 |
StringUtils.endsWithIgnoreCase(expression, ")")) {
|
574 |
Matcher m = COLOR_PATTERN4.matcher(expression);
|
575 |
if( m != null && m.matches()) { |
576 |
Color color = new Color( |
577 |
Integer.valueOf(m.group("R")), |
578 |
Integer.valueOf(m.group("G")), |
579 |
Integer.valueOf(m.group("B")), |
580 |
Integer.valueOf(m.group("A")) |
581 |
); |
582 |
return color;
|
583 |
} |
584 |
m = COLOR_PATTERN3.matcher(expression); |
585 |
if( m != null && m.matches()) { |
586 |
Color color = new Color( |
587 |
Integer.valueOf(m.group("R")), |
588 |
Integer.valueOf(m.group("G")), |
589 |
Integer.valueOf(m.group("B")), |
590 |
Integer.valueOf(m.group("A")) |
591 |
); |
592 |
return color;
|
593 |
} |
594 |
} |
595 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
596 |
x = manager.evaluate(null, expression);
|
597 |
if( x instanceof Color ) { |
598 |
return (Color) x; |
599 |
} |
600 |
if( x instanceof Number ) { |
601 |
return new Color(((Number) x).intValue()); |
602 |
} |
603 |
} catch(Exception ex) { |
604 |
IllegalArgumentException ex1 = new IllegalArgumentException("Can't get color from '"+expression+"'."); |
605 |
ex1.initCause(ex); |
606 |
throw ex;
|
607 |
} |
608 |
if( x == null ) { |
609 |
throw new IllegalArgumentException("Can't get double from '"+expression+"' value is null."); |
610 |
} |
611 |
throw new IllegalArgumentException("Can't get double from '"+expression+"' value is a "+x.getClass().getSimpleName()+"."); |
612 |
} |
613 |
|
614 |
public static boolean isValidIdentifier(String name){ |
615 |
String s = StringUtils.strip(name);
|
616 |
if(StringUtils.isEmpty(s)){
|
617 |
return false; |
618 |
} |
619 |
if(s.charAt(0) == '"' && s.charAt(s.length()-1) == '"') { |
620 |
s = StringUtils.unwrap(s, '"');
|
621 |
return !StringUtils.contains(s, '"'); |
622 |
} |
623 |
char ch = s.charAt(0); |
624 |
if(Character.isDigit(ch)){ |
625 |
return false; |
626 |
} |
627 |
|
628 |
for (int i = 0; i < s.length(); i++) { |
629 |
ch = s.charAt(i); |
630 |
if(!Character.isJavaIdentifierPart(ch)){ |
631 |
return false; |
632 |
} |
633 |
} |
634 |
return true; |
635 |
|
636 |
} |
637 |
|
638 |
public static Expression resolveHostExpressions(Expression expression, SymbolTable symbolTable) { |
639 |
Expression expression1 = ExpressionEvaluatorLocator.getManager().resolveHostExpressions(expression, symbolTable);
|
640 |
return expression1;
|
641 |
} |
642 |
|
643 |
public static ExpressionBuilder.Value resolveHostExpressions(ExpressionBuilder.Value statement, SymbolTable symbolTable) { |
644 |
Value statement1 = ExpressionEvaluatorLocator.getManager().resolveHostExpressions(statement, symbolTable); |
645 |
return statement1;
|
646 |
} |
647 |
|
648 |
public static ExpressionBuilder.Value getHostExpressionValue(ExpressionBuilder.Function hostExpression, ExpressionBuilder expbuilder) { |
649 |
Value statement1 = ExpressionEvaluatorLocator.getManager().getHostExpressionValue(hostExpression, expbuilder); |
650 |
return statement1;
|
651 |
} |
652 |
|
653 |
public static ExpressionBuilder.Value getHostExpressionValue(ExpressionBuilder.Function hostExpression, ExpressionBuilder expbuilder, SymbolTable symboltable) { |
654 |
Value statement1 = ExpressionEvaluatorLocator.getManager().getHostExpressionValue(hostExpression, expbuilder, symboltable); |
655 |
return statement1;
|
656 |
} |
657 |
|
658 |
public static String getPhrase(Expression expression) { |
659 |
if( expression == null ) { |
660 |
return null; |
661 |
} |
662 |
String s = expression.getPhrase();
|
663 |
return StringUtils.defaultIfBlank(s, null); |
664 |
} |
665 |
|
666 |
public static Script createScript(byte[] bytes, String name) { |
667 |
try {
|
668 |
if( StringUtils.isBlank(name)) {
|
669 |
return null; |
670 |
} |
671 |
String source = ""; |
672 |
if( !ArrayUtils.isEmpty(bytes) ) {
|
673 |
source = IOUtils.toString(bytes, StandardCharsets.UTF_8.name()); |
674 |
} |
675 |
ExpressionEvaluatorManager scriptManager = ExpressionEvaluatorLocator.getManager(); |
676 |
Script script = scriptManager.createScript(name, source, null);
|
677 |
return script;
|
678 |
} catch (Exception ex) { |
679 |
LOGGER.warn("Can't create script "+name+".", ex); |
680 |
return null; |
681 |
} |
682 |
} |
683 |
|
684 |
public static Script getScript(ResourcesStorage resources, String resourceName) { |
685 |
ResourcesStorage.Resource res = null;
|
686 |
try {
|
687 |
if( resources==null || StringUtils.isBlank(resourceName)) { |
688 |
return null; |
689 |
} |
690 |
res = resources.getResource(resourceName); |
691 |
if( res == null || !res.exists() ) { |
692 |
return null; |
693 |
} |
694 |
String source = IOUtils.toString(res.asInputStream(), StandardCharsets.UTF_8);
|
695 |
if( StringUtils.isEmpty(source) ) {
|
696 |
return null; |
697 |
} |
698 |
ExpressionEvaluatorManager scriptManager = ExpressionEvaluatorLocator.getManager(); |
699 |
Script script = scriptManager.createScript(resourceName, source, null);
|
700 |
return script;
|
701 |
} catch (Exception ex) { |
702 |
LOGGER.warn("Can't load script from resource "+resourceName+".", ex); |
703 |
return null; |
704 |
} finally {
|
705 |
DisposeUtils.disposeQuietly(res); |
706 |
} |
707 |
} |
708 |
} |
709 |
|