Revision 45523 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

View differences:

ExpressionUtils.java
1 1
package org.gvsig.expressionevaluator;
2 2

  
3
import java.awt.Color;
3 4
import java.io.File;
5
import java.util.List;
6
import java.util.regex.Matcher;
7
import java.util.regex.Pattern;
4 8
import org.apache.commons.lang3.ArrayUtils;
5 9
import org.apache.commons.lang3.StringUtils;
6 10
import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
7 11
import static org.gvsig.expressionevaluator.ExpressionEvaluatorManager.DYNAMICTEXT_ENDTAG;
8 12
import static org.gvsig.expressionevaluator.ExpressionEvaluatorManager.DYNAMICTEXT_STARTTAG;
13
import org.gvsig.tools.util.ListBuilder;
9 14

  
10 15
/**
11 16
 *
12
 * @author jjdelcerro
17
 * @author gvSIG Team
13 18
 */
19
@SuppressWarnings("UseSpecificCatch")
14 20
public class ExpressionUtils {
15 21

  
16 22
    public static boolean isEmpty(Expression expression) {
......
258 264
        if( StringUtils.isBlank(s) ) {
259 265
            throw new NumberFormatException("Can't get long from a blank string.");
260 266
        }
267
        try {
268
            int value = Integer.parseInt(s);
269
            return value;
270
        } catch(Exception ex) {
271
            
272
        }
261 273
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
262 274
        SymbolTable symbolTable = null; //manager.getInmutableSymbolTable();
263 275
        Object x;
......
281 293
        if( StringUtils.isBlank(s) ) {
282 294
            throw new NumberFormatException("Can't get double from a blank string.");
283 295
        }
296
        try {
297
            double value = Double.parseDouble(s);
298
            return value;
299
        } catch(Exception ex) {
300
            
301
        }
284 302
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
285 303
        SymbolTable symbolTable = null; //manager.getInmutableSymbolTable();
286 304
        Object x;
......
328 346
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
329 347
        return manager.createCodeBuilder();
330 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
    
331 552
}
553

  

Also available in: Unified diff