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 / SimpleScript.java @ 45739
History | View | Annotate | Download (3.18 KB)
1 |
package org.gvsig.expressionevaluator.impl; |
---|---|
2 |
|
3 |
import java.net.URL; |
4 |
import java.util.ArrayList; |
5 |
import java.util.Collection; |
6 |
import java.util.List; |
7 |
import org.gvsig.expressionevaluator.Code; |
8 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
9 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
10 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
11 |
import org.gvsig.expressionevaluator.Function; |
12 |
import org.gvsig.expressionevaluator.Interpreter; |
13 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
14 |
import org.gvsig.expressionevaluator.SymbolTable; |
15 |
import org.gvsig.tools.script.Script; |
16 |
|
17 |
/**
|
18 |
*
|
19 |
* @author jjdelcerro
|
20 |
*/
|
21 |
public class SimpleScript implements Script { |
22 |
|
23 |
private final String name; |
24 |
private String source; |
25 |
private Interpreter interpreter;
|
26 |
private List<String> names; |
27 |
private final MutableSymbolTable symbolTable; |
28 |
|
29 |
public SimpleScript(Interpreter interpreter, String name, String source) { |
30 |
this.interpreter = interpreter;
|
31 |
this.name = name;
|
32 |
this.symbolTable = ExpressionEvaluatorLocator.getManager().createSymbolTable();
|
33 |
this.setCode(source);
|
34 |
} |
35 |
|
36 |
public MutableSymbolTable getSymbolTable() {
|
37 |
return this.symbolTable; |
38 |
} |
39 |
|
40 |
@Override
|
41 |
public String getName() { |
42 |
return this.name; |
43 |
} |
44 |
|
45 |
@Override
|
46 |
public String getCode() { |
47 |
return this.source; |
48 |
} |
49 |
|
50 |
@Override
|
51 |
public String getTypeName() { |
52 |
return "COSA"; |
53 |
} |
54 |
|
55 |
@Override
|
56 |
public URL getURL() { |
57 |
return null; |
58 |
} |
59 |
|
60 |
@Override
|
61 |
public void put(String name, Object value) { |
62 |
this.symbolTable.setVar(name, value);
|
63 |
} |
64 |
|
65 |
@Override
|
66 |
public Object invokeFunction(String funcname, Object[] args) throws NoSuchMethodException { |
67 |
if( this.symbolTable.function(funcname)==null ) { |
68 |
if( !this.interpreter.hasFunction(funcname) ) { |
69 |
throw new NoSuchMethodException(funcname); |
70 |
} |
71 |
} |
72 |
try {
|
73 |
return this.interpreter.call(this.symbolTable, funcname, args); |
74 |
} catch (Exception ex) { |
75 |
throw new ExpressionRuntimeException(this.interpreter.getCurrentCode(),"Error calling funcion '"+funcname+"'.", ex); |
76 |
} |
77 |
} |
78 |
|
79 |
@Override
|
80 |
public final void setCode(String source) { |
81 |
this.source = source;
|
82 |
this.names = null; |
83 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
84 |
Code code = manager.compile(source); |
85 |
this.interpreter.run(this.symbolTable, code); |
86 |
} |
87 |
|
88 |
@Override
|
89 |
public List<String> getNames() { |
90 |
if( this.names == null ) { |
91 |
List<String> theNames = new ArrayList<>(); |
92 |
Collection<Function> funcs = this.symbolTable.localfunctions(); |
93 |
if( funcs!=null ) { |
94 |
for (Function function : funcs) {
|
95 |
theNames.add(function.name()); |
96 |
} |
97 |
} |
98 |
Collection<String> vars = symbolTable.localvariables(); |
99 |
if( vars!=null ) { |
100 |
for (String var : vars) { |
101 |
theNames.add(var); |
102 |
} |
103 |
} |
104 |
this.names = theNames;
|
105 |
} |
106 |
return this.names; |
107 |
} |
108 |
|
109 |
|
110 |
} |