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 @ 44389
History | View | Annotate | Download (3.18 KB)
1 | 44339 | jjdelcerro | package org.gvsig.expressionevaluator.impl; |
---|---|---|---|
2 | |||
3 | 44340 | jjdelcerro | import java.net.URL; |
4 | import java.util.ArrayList; |
||
5 | import java.util.Collection; |
||
6 | import java.util.List; |
||
7 | 44339 | jjdelcerro | import org.gvsig.expressionevaluator.Code; |
8 | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
||
9 | import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
||
10 | import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
||
11 | 44340 | jjdelcerro | import org.gvsig.expressionevaluator.Function; |
12 | 44389 | jjdelcerro | import org.gvsig.expressionevaluator.Interpreter; |
13 | import org.gvsig.expressionevaluator.MutableSymbolTable; |
||
14 | 44340 | jjdelcerro | import org.gvsig.expressionevaluator.SymbolTable; |
15 | 44339 | jjdelcerro | 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 | 44389 | jjdelcerro | private Interpreter interpreter;
|
26 | 44340 | jjdelcerro | private List<String> names; |
27 | 44389 | jjdelcerro | private final MutableSymbolTable symbolTable; |
28 | 44339 | jjdelcerro | |
29 | 44389 | jjdelcerro | public SimpleScript(Interpreter interpreter, String name, String source) { |
30 | this.interpreter = interpreter;
|
||
31 | 44339 | jjdelcerro | this.name = name;
|
32 | 44389 | jjdelcerro | this.symbolTable = ExpressionEvaluatorLocator.getManager().createSymbolTable();
|
33 | this.setCode(source);
|
||
34 | 44339 | jjdelcerro | } |
35 | |||
36 | 44389 | jjdelcerro | public MutableSymbolTable getSymbolTable() {
|
37 | return this.symbolTable; |
||
38 | } |
||
39 | |||
40 | 44339 | jjdelcerro | @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 | 44340 | jjdelcerro | public URL getURL() { |
57 | 44339 | jjdelcerro | return null; |
58 | } |
||
59 | |||
60 | @Override
|
||
61 | public void put(String name, Object value) { |
||
62 | 44389 | jjdelcerro | this.symbolTable.setVar(name, value);
|
63 | 44339 | jjdelcerro | } |
64 | |||
65 | @Override
|
||
66 | public Object invokeFunction(String funcname, Object[] args) throws NoSuchMethodException { |
||
67 | 44389 | jjdelcerro | if( this.symbolTable.function(funcname)==null ) { |
68 | if( !this.interpreter.hasFunction(funcname) ) { |
||
69 | throw new NoSuchMethodException(funcname); |
||
70 | } |
||
71 | 44339 | jjdelcerro | } |
72 | try {
|
||
73 | 44389 | jjdelcerro | return this.interpreter.call(this.symbolTable, funcname, args); |
74 | 44339 | jjdelcerro | } 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 | 44340 | jjdelcerro | this.names = null; |
83 | 44339 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
84 | Code code = manager.compile(source); |
||
85 | 44389 | jjdelcerro | this.interpreter.run(this.symbolTable, code); |
86 | 44339 | jjdelcerro | } |
87 | 44340 | jjdelcerro | |
88 | @Override
|
||
89 | public List<String> getNames() { |
||
90 | if( this.names == null ) { |
||
91 | List<String> theNames = new ArrayList<>(); |
||
92 | 44389 | jjdelcerro | Collection<Function> funcs = this.symbolTable.localfunctions(); |
93 | 44340 | jjdelcerro | 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 | 44339 | jjdelcerro | |
109 | 44340 | jjdelcerro | |
110 | 44339 | jjdelcerro | } |