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 @ 47698
History | View | Annotate | Download (6.19 KB)
1 |
package org.gvsig.expressionevaluator.impl.function.programming; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Collection; |
5 |
import java.util.Collections; |
6 |
import java.util.List; |
7 |
import java.util.Objects; |
8 |
import org.apache.commons.lang3.Range; |
9 |
import org.apache.commons.lang3.StringUtils; |
10 |
import org.gvsig.expressionevaluator.Code; |
11 |
import org.gvsig.expressionevaluator.Codes; |
12 |
import org.gvsig.expressionevaluator.Formatter; |
13 |
import org.gvsig.expressionevaluator.Function; |
14 |
import org.gvsig.expressionevaluator.Interpreter; |
15 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
16 |
import org.gvsig.expressionevaluator.SymbolTable; |
17 |
import org.gvsig.expressionevaluator.impl.DefaultInterpreter; |
18 |
import org.gvsig.expressionevaluator.impl.HostExpressionUtils; |
19 |
import org.gvsig.expressionevaluator.impl.function.programming.BreakFunction.BreakException; |
20 |
import org.gvsig.expressionevaluator.impl.function.programming.ReturnFunction.ReturnException; |
21 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
22 |
|
23 |
public class CodeBlockWithExceptFunction extends AbstractFunction { |
24 |
|
25 |
public static final String NAME = "BLOCK_EXCEPT"; |
26 |
|
27 |
public CodeBlockWithExceptFunction() {
|
28 |
super(Function.GROUP_PROGRAMMING,
|
29 |
NAME, |
30 |
Range.between(1,Integer.MAX_VALUE), |
31 |
"Evaluate each of the arguments sequentially.",
|
32 |
NAME+"( {{arguments...}} )",
|
33 |
null,
|
34 |
"Object",
|
35 |
false
|
36 |
); |
37 |
} |
38 |
|
39 |
@Override
|
40 |
public boolean isHidden() { |
41 |
return true; |
42 |
} |
43 |
|
44 |
@Override
|
45 |
public boolean useArgumentsInsteadObjects() { |
46 |
return true; |
47 |
} |
48 |
|
49 |
@Override
|
50 |
public boolean allowConstantFolding() { |
51 |
return false; |
52 |
} |
53 |
|
54 |
@Override
|
55 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
56 |
throw new UnsupportedOperationException("Not supported yet."); |
57 |
} |
58 |
|
59 |
@Override
|
60 |
public Object call(Interpreter interpreter, Codes args) throws Exception { |
61 |
Object value = null; |
62 |
Code exceptionCode = args.get(args.size()-2);
|
63 |
Code declareCode = args.get(args.size()-1);
|
64 |
if( declareCode != null ) { |
65 |
try {
|
66 |
value = ((DefaultInterpreter)interpreter).runCode(declareCode); |
67 |
// } catch(BreakException|ReturnException ex) {
|
68 |
// throw ex;
|
69 |
} catch(Exception ex) { |
70 |
if( exceptionCode==null ) { |
71 |
LOGGER.warn("Error in DECLARE calling "+ code2string(declareCode), ex);
|
72 |
throw ex;
|
73 |
} else {
|
74 |
value = ((DefaultInterpreter)interpreter).runCode(exceptionCode); |
75 |
} |
76 |
} |
77 |
} |
78 |
for (int argn = 0; argn < args.size()-2; argn++) { |
79 |
Code statement = args.get(argn); |
80 |
// LLamamos a runCode para que no atrape los returns.
|
81 |
try {
|
82 |
statement = HostExpressionUtils.resolveHostExpressions(statement, interpreter); |
83 |
value = ((DefaultInterpreter)interpreter).runCode(statement); |
84 |
} catch(BreakException|ReturnException ex) {
|
85 |
throw ex;
|
86 |
} catch(Exception ex) { |
87 |
if( exceptionCode==null ) { |
88 |
LOGGER.warn("Error in BEGIN/END calling line "+ argn + ", "+ code2string(statement), ex); |
89 |
throw ex;
|
90 |
} else {
|
91 |
SymbolTable symbolTable = interpreter.getSymbolTable(); |
92 |
if( symbolTable instanceof MutableSymbolTable ) { |
93 |
((MutableSymbolTable) symbolTable).setVar("@ErrorMessage", ex.getMessage());
|
94 |
((MutableSymbolTable) symbolTable).setVar("@ErrorStatement", getErrorStatement(statement));
|
95 |
((MutableSymbolTable) symbolTable).setVar("@ErrorContext", this.getErrorContext(symbolTable)); |
96 |
} |
97 |
value = ((DefaultInterpreter)interpreter).runCode(exceptionCode); |
98 |
} |
99 |
} |
100 |
} |
101 |
return value;
|
102 |
} |
103 |
|
104 |
private String getErrorContext(SymbolTable symbolTable) { |
105 |
try {
|
106 |
StringBuilder builder = new StringBuilder(); |
107 |
List<String> names = new ArrayList<>(symbolTable.variables()); |
108 |
Collections.sort(names);
|
109 |
for (String name : names) { |
110 |
if( StringUtils.equalsIgnoreCase("@ErrorContext", name) ) { |
111 |
continue;
|
112 |
} |
113 |
String value = "##ERROR##"; |
114 |
try {
|
115 |
value = Objects.toString(symbolTable.value(name)); |
116 |
} catch(Throwable t) { |
117 |
LOGGER.debug("Can't retrieve value for '"+name+"'.",t); |
118 |
} |
119 |
builder.append(name); |
120 |
builder.append("=");
|
121 |
builder.append(value); |
122 |
builder.append(";\n");
|
123 |
} |
124 |
return builder.toString();
|
125 |
} catch(Throwable t) { |
126 |
return "##ERROR##"; |
127 |
} |
128 |
} |
129 |
|
130 |
private String getErrorStatement(Code statement) { |
131 |
try {
|
132 |
return statement.toString();
|
133 |
} catch(Throwable t) { |
134 |
return "Unknown"; |
135 |
} |
136 |
} |
137 |
|
138 |
private String code2string(Code code) { |
139 |
try {
|
140 |
return Objects.toString(code,""); |
141 |
} catch(Exception ex2) { |
142 |
return "unknown"; |
143 |
} |
144 |
} |
145 |
@Override
|
146 |
public String toString(Codes args, Formatter<Code> formatter) { |
147 |
Code exceptionCode = args.get(args.size()-2);
|
148 |
Code declareCode = args.get(args.size()-1);
|
149 |
|
150 |
StringBuilder builder = new StringBuilder(); |
151 |
if( declareCode != null ) { |
152 |
builder.append("DECLARE ");
|
153 |
builder.append(declareCode.toString(formatter)); |
154 |
} |
155 |
builder.append("BEGIN ");
|
156 |
for (int argn = 0; argn < args.size()-2; argn++) { |
157 |
Code statement = args.get(argn); |
158 |
builder.append(statement.toString(formatter)); |
159 |
builder.append("; ");
|
160 |
} |
161 |
if( exceptionCode != null ) { |
162 |
builder.append("EXCEPTION ");
|
163 |
builder.append(exceptionCode.toString(formatter)); |
164 |
builder.append(" ");
|
165 |
} |
166 |
builder.append("END ");
|
167 |
return builder.toString();
|
168 |
} |
169 |
} |