svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.api / src / main / java / org / gvsig / expressionevaluator / spi / AbstractSymbolTable.java @ 43983
History | View | Annotate | Download (5.86 KB)
1 |
package org.gvsig.expressionevaluator.spi; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Collection; |
5 |
import java.util.Collections; |
6 |
import java.util.HashMap; |
7 |
import java.util.HashSet; |
8 |
import java.util.Iterator; |
9 |
import java.util.List; |
10 |
import java.util.Map; |
11 |
import java.util.Set; |
12 |
import org.apache.commons.lang3.Range; |
13 |
import org.apache.commons.lang3.StringUtils; |
14 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
15 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
16 |
import org.gvsig.expressionevaluator.Function; |
17 |
import org.gvsig.expressionevaluator.Interpreter; |
18 |
import org.gvsig.expressionevaluator.SymbolTable; |
19 |
import org.gvsig.tools.script.Script; |
20 |
|
21 |
/**
|
22 |
*
|
23 |
* @author jjdelcerro
|
24 |
*/
|
25 |
public abstract class AbstractSymbolTable implements SymbolTable { |
26 |
|
27 |
private final String name; |
28 |
|
29 |
private class ScriptFunction extends AbstractFunction { |
30 |
|
31 |
public ScriptFunction(String funcName) { |
32 |
super("Script", funcName, Range.between(1, Integer.MAX_VALUE)); |
33 |
} |
34 |
|
35 |
@Override
|
36 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
37 |
for (Script script : scripts()) {
|
38 |
try {
|
39 |
return script.invokeFunction(this.name(), args); |
40 |
} catch (NoSuchMethodException ex) { |
41 |
// Si la funcion no existe en el script pasamos al siguiente
|
42 |
// script.
|
43 |
} |
44 |
} |
45 |
throw new NoSuchMethodException("Can't locate funcion '" + this.name() + "'."); |
46 |
} |
47 |
|
48 |
} |
49 |
|
50 |
protected final List<SymbolTable> symbolTables; |
51 |
protected Map<String, Object> vars; |
52 |
protected Map<String, Function> functions; |
53 |
protected List<Script> scripts; |
54 |
|
55 |
public AbstractSymbolTable() {
|
56 |
this(null); |
57 |
} |
58 |
|
59 |
public AbstractSymbolTable(String name) { |
60 |
this.name = name;
|
61 |
this.symbolTables = new ArrayList<>(); |
62 |
this.vars = null; |
63 |
this.functions = null; |
64 |
this.scripts = null; |
65 |
} |
66 |
|
67 |
@Override
|
68 |
public String getName() { |
69 |
return name;
|
70 |
} |
71 |
|
72 |
@Override
|
73 |
public void addSymbolTable(SymbolTable symbolTable) { |
74 |
if (this.symbolTables.contains(symbolTable)) { |
75 |
return;
|
76 |
} |
77 |
this.symbolTables.add(symbolTable);
|
78 |
} |
79 |
|
80 |
protected Map<String, Object> getVars() { |
81 |
if (this.vars == null) { |
82 |
this.vars = new HashMap<>(); |
83 |
} |
84 |
return this.vars; |
85 |
} |
86 |
|
87 |
protected Map<String, Function> getFunctions() { |
88 |
if (this.functions == null) { |
89 |
this.functions = new HashMap<>(); |
90 |
} |
91 |
return this.functions; |
92 |
} |
93 |
|
94 |
protected List<Script> getScripts() { |
95 |
if (this.scripts == null) { |
96 |
this.scripts = new ArrayList<>(); |
97 |
} |
98 |
return this.scripts; |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public boolean exists(String name) { |
103 |
if (StringUtils.isEmpty(name)) {
|
104 |
return false; |
105 |
} |
106 |
if (this.vars != null) { |
107 |
if (this.vars.containsKey(name.toUpperCase())) { |
108 |
return true; |
109 |
} |
110 |
} |
111 |
for (SymbolTable other : this.symbolTables) { |
112 |
if (other.exists(name)) {
|
113 |
return true; |
114 |
} |
115 |
} |
116 |
return false; |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public Object value(String name) { |
121 |
if (StringUtils.isEmpty(name)) {
|
122 |
return null; |
123 |
} |
124 |
if (this.vars != null) { |
125 |
name = name.toUpperCase(); |
126 |
if (this.vars.containsKey(name)) { |
127 |
return this.vars.get(name); |
128 |
} |
129 |
} |
130 |
for (SymbolTable other : this.symbolTables) { |
131 |
if (other.exists(name)) {
|
132 |
return other.value(name);
|
133 |
} |
134 |
} |
135 |
return null; |
136 |
} |
137 |
|
138 |
@Override
|
139 |
public Function function(String name) { |
140 |
if (StringUtils.isEmpty(name)) {
|
141 |
return null; |
142 |
} |
143 |
if (this.functions != null) { |
144 |
name = name.toUpperCase(); |
145 |
if (this.functions.containsKey(name)) { |
146 |
return this.functions.get(name); |
147 |
} |
148 |
} |
149 |
for (SymbolTable other : this.symbolTables) { |
150 |
Function fn = other.function(name); |
151 |
if (fn != null) { |
152 |
if (fn instanceof ScriptFunction) { |
153 |
continue;
|
154 |
} |
155 |
return fn;
|
156 |
} |
157 |
} |
158 |
if (this.scripts == null || this.scripts.isEmpty()) { |
159 |
return null; |
160 |
} |
161 |
return new ScriptFunction(name); |
162 |
} |
163 |
|
164 |
@Override
|
165 |
public Collection<String> variables() { |
166 |
Set<String> theVars = new HashSet<>(); |
167 |
for (SymbolTable symbolTable : this.symbolTables) { |
168 |
theVars.addAll(symbolTable.variables()); |
169 |
} |
170 |
if (this.vars != null) { |
171 |
theVars.addAll(this.vars.keySet());
|
172 |
} |
173 |
return Collections.unmodifiableCollection(theVars); |
174 |
} |
175 |
|
176 |
@Override
|
177 |
public Collection<Function> functions() { |
178 |
Set<Function> theFunctions = new HashSet<>(); |
179 |
for (SymbolTable symbolTable : this.symbolTables) { |
180 |
theFunctions.addAll(symbolTable.functions()); |
181 |
} |
182 |
if (this.functions != null) { |
183 |
theFunctions.addAll(this.functions.values());
|
184 |
} |
185 |
return Collections.unmodifiableCollection(theFunctions); |
186 |
} |
187 |
|
188 |
@Override
|
189 |
public Collection<Script> scripts() { |
190 |
Set<Script> theScripts = new HashSet<>(); |
191 |
for (SymbolTable symbolTable : this.symbolTables) { |
192 |
theScripts.addAll(symbolTable.scripts()); |
193 |
} |
194 |
if (this.scripts != null) { |
195 |
theScripts.addAll(this.scripts);
|
196 |
} |
197 |
return Collections.unmodifiableCollection(theScripts); |
198 |
} |
199 |
|
200 |
@Override
|
201 |
public Iterator<Function> iterator() { |
202 |
return this.functions().iterator(); |
203 |
} |
204 |
|
205 |
@Override
|
206 |
public SymbolTable clone() throws CloneNotSupportedException { |
207 |
SymbolTable other = (SymbolTable) super.clone();
|
208 |
return other;
|
209 |
} |
210 |
|
211 |
} |