Revision 46085 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/CodeBlockFunction.java
CodeBlockFunction.java | ||
---|---|---|
1 | 1 |
package org.gvsig.expressionevaluator.impl.function.programming; |
2 | 2 |
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.List; |
|
3 | 5 |
import org.apache.commons.lang3.Range; |
6 |
import org.apache.commons.lang3.StringUtils; |
|
7 |
import org.apache.commons.lang3.mutable.MutableBoolean; |
|
4 | 8 |
import org.gvsig.expressionevaluator.Code; |
9 |
import org.gvsig.expressionevaluator.CodeBuilder; |
|
5 | 10 |
import org.gvsig.expressionevaluator.Codes; |
11 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
6 | 12 |
import org.gvsig.expressionevaluator.Function; |
7 | 13 |
import org.gvsig.expressionevaluator.Interpreter; |
8 | 14 |
import org.gvsig.expressionevaluator.impl.DefaultInterpreter; |
9 | 15 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
16 |
import org.gvsig.tools.exception.BaseException; |
|
17 |
import org.gvsig.tools.visitor.FilteredVisitable; |
|
18 |
import org.gvsig.tools.visitor.VisitCanceledException; |
|
10 | 19 |
|
20 |
@SuppressWarnings("UseSpecificCatch") |
|
11 | 21 |
public class CodeBlockFunction extends AbstractFunction { |
12 | 22 |
|
13 | 23 |
public static final String NAME = "BLOCK"; |
14 |
|
|
24 |
|
|
15 | 25 |
public CodeBlockFunction() { |
16 |
super(Function.GROUP_PROGRAMMING,
|
|
17 |
NAME,
|
|
18 |
Range.between(1,Integer.MAX_VALUE), |
|
26 |
super(Function.GROUP_PROGRAMMING, |
|
27 |
NAME, |
|
28 |
Range.between(1, Integer.MAX_VALUE),
|
|
19 | 29 |
"Evaluate each of the arguments sequentially.", |
20 | 30 |
"BEGIN\n {{sentence}};\nEXCEPT\n PASS\nEND\n", |
21 | 31 |
null, |
... | ... | |
26 | 36 |
|
27 | 37 |
@Override |
28 | 38 |
public boolean isHidden() { |
29 |
return true; |
|
39 |
return true;
|
|
30 | 40 |
} |
31 |
|
|
41 |
|
|
32 | 42 |
@Override |
33 | 43 |
public boolean useArgumentsInsteadObjects() { |
34 | 44 |
return true; |
... | ... | |
38 | 48 |
public boolean allowConstantFolding() { |
39 | 49 |
return false; |
40 | 50 |
} |
41 |
|
|
51 |
|
|
42 | 52 |
@Override |
43 | 53 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
44 | 54 |
throw new UnsupportedOperationException("Not supported yet."); |
45 | 55 |
} |
46 |
|
|
56 |
|
|
47 | 57 |
@Override |
48 | 58 |
public Object call(Interpreter interpreter, Codes args) throws Exception { |
49 | 59 |
Object value = null; |
50 |
for (Code arg : args) { |
|
60 |
for (Code statement : args) { |
|
61 |
statement = resolveHostExpressions((DefaultInterpreter) interpreter, statement); |
|
51 | 62 |
// LLamamos a runCode para que no atrape los returns. |
52 |
value = ((DefaultInterpreter)interpreter).runCode(arg);
|
|
63 |
value = ((DefaultInterpreter) interpreter).runCode(statement);
|
|
53 | 64 |
} |
54 | 65 |
return value; |
55 | 66 |
} |
56 |
|
|
67 |
|
|
68 |
/*friend*/ static Code resolveHostExpressions(DefaultInterpreter interpreter, Code statement) throws Exception { |
|
69 |
List<Code> hostExpressions = new ArrayList<>(); |
|
70 |
statement.accept( |
|
71 |
(Object code) -> { |
|
72 |
hostExpressions.add((Code) code); |
|
73 |
}, |
|
74 |
(FilteredVisitable code) -> !(code instanceof Code.Callable |
|
75 |
&& StringUtils.equalsIgnoreCase(((Code.Callable) code).name(), ExpressionBuilder.FUNCTION_$HOSTEXPRESSION)) |
|
76 |
); |
|
77 |
if (hostExpressions.isEmpty()) { |
|
78 |
return statement; |
|
79 |
} |
|
80 |
Code newstatement = (Code) statement.clone(); |
|
81 |
hostExpressions.clear(); |
|
82 |
newstatement.accept( |
|
83 |
(Object code) -> { |
|
84 |
hostExpressions.add((Code) code); |
|
85 |
}, |
|
86 |
(FilteredVisitable code) -> !(code instanceof Code.Callable |
|
87 |
&& StringUtils.equalsIgnoreCase(((Code.Callable) code).name(), ExpressionBuilder.FUNCTION_$HOSTEXPRESSION)) |
|
88 |
); |
|
89 |
if (hostExpressions.isEmpty()) { |
|
90 |
return statement; |
|
91 |
} |
|
92 |
|
|
93 |
CodeBuilder codeBuilder = interpreter.getCodeBuilder(); |
|
94 |
List<Code.Constant> constants = new ArrayList<>(); |
|
95 |
for (Code hostExpression : hostExpressions) { |
|
96 |
Object v = interpreter.run(hostExpression); |
|
97 |
constants.add(codeBuilder.constant(v)); |
|
98 |
} |
|
99 |
|
|
100 |
for (int i = 0; i < hostExpressions.size(); i++) { |
|
101 |
Code hostExpression = hostExpressions.get(i); |
|
102 |
Code value = constants.get(i); |
|
103 |
newstatement.replace(hostExpression, value); |
|
104 |
} |
|
105 |
return newstatement; |
|
106 |
// return statement; |
|
107 |
} |
|
57 | 108 |
} |
Also available in: Unified diff