Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / programming / CodeBlockWithExceptFunction.java @ 47017

History | View | Annotate | Download (2.98 KB)

1
package org.gvsig.expressionevaluator.impl.function.programming;
2

    
3
import java.util.Objects;
4
import org.apache.commons.lang3.Range;
5
import org.gvsig.expressionevaluator.Code;
6
import org.gvsig.expressionevaluator.Codes;
7
import org.gvsig.expressionevaluator.Function;
8
import org.gvsig.expressionevaluator.Interpreter;
9
import org.gvsig.expressionevaluator.impl.DefaultInterpreter;
10
import org.gvsig.expressionevaluator.impl.function.programming.BreakFunction.BreakException;
11
import org.gvsig.expressionevaluator.impl.function.programming.ReturnFunction.ReturnException;
12
import org.gvsig.expressionevaluator.spi.AbstractFunction;
13

    
14
public class CodeBlockWithExceptFunction extends AbstractFunction {
15

    
16
    public static final String NAME = "BLOCK_EXCEPT";
17
    
18
    public CodeBlockWithExceptFunction() {
19
        super(Function.GROUP_PROGRAMMING, 
20
                NAME, 
21
                Range.between(1,Integer.MAX_VALUE),
22
                "Evaluate each of the arguments sequentially.",
23
                NAME+"( {{arguments...}} )",
24
                null,
25
                "Object",
26
                false
27
        );
28
    }
29

    
30
    @Override
31
    public boolean isHidden() {
32
      return true;
33
    }
34
    
35
    @Override
36
    public boolean useArgumentsInsteadObjects() {
37
        return true;
38
    }
39

    
40
    @Override
41
    public boolean allowConstantFolding() {
42
        return false;
43
    }
44
    
45
    @Override
46
    public Object call(Interpreter interpreter, Object[] args) throws Exception {
47
        throw new UnsupportedOperationException("Not supported yet.");
48
    }
49
    
50
    @Override
51
    public Object call(Interpreter interpreter, Codes args) throws Exception {
52
        Object value = null;
53
        Code exceptionCode = args.get(args.size()-1);
54
        if( exceptionCode==null ) {
55
            for (int argn = 0; argn < args.size()-1; argn++) {
56
                Code statement = args.get(argn);
57
                // LLamamos a runCode para que no atrape los returns.
58
                try {
59
                    value = ((DefaultInterpreter)interpreter).runCode(statement);
60
                } catch(BreakException|ReturnException ex) {
61
                    throw ex;
62
                } catch(Exception ex) {
63
                    String stmt = "unknown";
64
                    try {
65
                        stmt = Objects.toString(statement);
66
                    } catch(Exception ex2) {
67

    
68
                    }
69
                    LOGGER.warn("Error in block function calling arg "+ argn + ", "+ stmt, ex);
70
                    throw ex;
71
                }
72
            }
73
        } else {
74
            for (int i = 0; i < args.size()-1; i++) {
75
                Code statement = args.get(i);
76
                try {
77
                    // LLamamos a runCode para que no atrape los returns.
78
                    value = ((DefaultInterpreter)interpreter).runCode(statement);
79
                } catch(Exception ex) {
80
                    value = ((DefaultInterpreter)interpreter).runCode(exceptionCode);
81
                    break;
82
                }
83
            }
84
        }
85
        return value;
86
    }
87

    
88
}