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 @ 44533
History | View | Annotate | Download (7.73 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.StringUtils; |
13 |
import org.gvsig.expressionevaluator.Function; |
14 |
import org.gvsig.expressionevaluator.SymbolTable; |
15 |
|
16 |
/**
|
17 |
*
|
18 |
* @author jjdelcerro
|
19 |
*/
|
20 |
public abstract class AbstractSymbolTable implements SymbolTable { |
21 |
|
22 |
private final String name; |
23 |
|
24 |
// public class ScriptFunction extends AbstractFunction {
|
25 |
//
|
26 |
// public ScriptFunction(String funcName) {
|
27 |
// super("Script", funcName, Range.between(1, Integer.MAX_VALUE));
|
28 |
// }
|
29 |
//
|
30 |
// @Override
|
31 |
// public Object call(Interpreter interpreter, Object[] args) throws Exception {
|
32 |
// for (Script script : scripts()) {
|
33 |
// try {
|
34 |
// return script.invokeFunction(this.name(), args);
|
35 |
// } catch (NoSuchMethodException ex) {
|
36 |
// // Si la funcion no existe en el script pasamos al siguiente
|
37 |
// // script.
|
38 |
// }
|
39 |
// }
|
40 |
// throw new NoSuchMethodException("Can't locate funcion '" + this.name() + "'.");
|
41 |
// }
|
42 |
//
|
43 |
// }
|
44 |
|
45 |
protected final List<SymbolTable> symbolTables; |
46 |
protected Map<String, Object> vars; |
47 |
protected Map<String, Function> functions; |
48 |
protected Map<String, Function> functionAlias; |
49 |
// protected List<Script> scripts;
|
50 |
|
51 |
public AbstractSymbolTable() {
|
52 |
this(null); |
53 |
} |
54 |
|
55 |
public AbstractSymbolTable(String name) { |
56 |
this.name = name;
|
57 |
this.symbolTables = new ArrayList<>(); |
58 |
this.vars = null; |
59 |
this.functions = null; |
60 |
this.functionAlias = null; |
61 |
// this.scripts = null;
|
62 |
} |
63 |
|
64 |
@Override
|
65 |
public String getName() { |
66 |
return name;
|
67 |
} |
68 |
|
69 |
protected void addFunction(Function function) { |
70 |
if (function == null) { |
71 |
throw new IllegalArgumentException("function can't be null"); |
72 |
} |
73 |
this.getFunctions().put(function.name().toUpperCase(), function);
|
74 |
List<String> aliases = function.aliases(); |
75 |
if( aliases == null || aliases.isEmpty() ) { |
76 |
return;
|
77 |
} |
78 |
Map<String, Function> theFunctionAlias = this.getFunctionAlias(); |
79 |
for (String alias : aliases) { |
80 |
if( alias!=null ) { |
81 |
theFunctionAlias.put(alias, function); |
82 |
} |
83 |
} |
84 |
} |
85 |
|
86 |
@Override
|
87 |
public boolean addSymbolTable(SymbolTable symbolTable) { |
88 |
if (this.symbolTables.contains(symbolTable)) { |
89 |
return false; |
90 |
} |
91 |
this.symbolTables.add(symbolTable);
|
92 |
return true; |
93 |
} |
94 |
|
95 |
@Override
|
96 |
public boolean containsSymbolTable(SymbolTable symbolTable) { |
97 |
return this.symbolTables.contains(symbolTable); |
98 |
} |
99 |
|
100 |
@Override
|
101 |
public boolean removeSymbolTable(SymbolTable symbolTable) { |
102 |
boolean n = this.symbolTables.remove(symbolTable); |
103 |
return n;
|
104 |
} |
105 |
|
106 |
protected Map<String, Object> getVars() { |
107 |
if (this.vars == null) { |
108 |
this.vars = new HashMap<>(); |
109 |
} |
110 |
return this.vars; |
111 |
} |
112 |
|
113 |
protected Map<String, Function> getFunctions() { |
114 |
if (this.functions == null) { |
115 |
this.functions = new HashMap<>(); |
116 |
} |
117 |
return this.functions; |
118 |
} |
119 |
|
120 |
protected Map<String, Function> getFunctionAlias() { |
121 |
if (this.functionAlias == null) { |
122 |
this.functionAlias = new HashMap<>(); |
123 |
} |
124 |
return this.functionAlias; |
125 |
} |
126 |
|
127 |
// protected List<Script> getScripts() {
|
128 |
// if (this.scripts == null) {
|
129 |
// this.scripts = new ArrayList<>();
|
130 |
// }
|
131 |
// return this.scripts;
|
132 |
// }
|
133 |
//
|
134 |
@Override
|
135 |
public boolean exists(String name) { |
136 |
if (StringUtils.isEmpty(name)) {
|
137 |
return false; |
138 |
} |
139 |
if (this.vars != null) { |
140 |
if (this.vars.containsKey(name.toUpperCase())) { |
141 |
return true; |
142 |
} |
143 |
} |
144 |
for (SymbolTable other : this.symbolTables) { |
145 |
if (other.exists(name)) {
|
146 |
return true; |
147 |
} |
148 |
} |
149 |
return false; |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public Object value(String name) { |
154 |
if (StringUtils.isEmpty(name)) {
|
155 |
return null; |
156 |
} |
157 |
if (this.vars != null) { |
158 |
name = name.toUpperCase(); |
159 |
if (this.vars.containsKey(name)) { |
160 |
return this.vars.get(name); |
161 |
} |
162 |
} |
163 |
for (SymbolTable other : this.symbolTables) { |
164 |
if (other.exists(name)) {
|
165 |
return other.value(name);
|
166 |
} |
167 |
} |
168 |
return null; |
169 |
} |
170 |
|
171 |
@Override
|
172 |
public Function function(String name) { |
173 |
if (StringUtils.isEmpty(name)) {
|
174 |
return null; |
175 |
} |
176 |
if (this.functions != null) { |
177 |
name = name.toUpperCase(); |
178 |
if (this.functions.containsKey(name)) { |
179 |
return this.functions.get(name); |
180 |
} |
181 |
} |
182 |
if (this.functionAlias != null) { |
183 |
name = name.toUpperCase(); |
184 |
if (this.functionAlias.containsKey(name)) { |
185 |
return this.functionAlias.get(name); |
186 |
} |
187 |
} |
188 |
for (SymbolTable other : this.symbolTables) { |
189 |
Function fn = other.function(name); |
190 |
if (fn != null) { |
191 |
// if (fn instanceof ScriptFunction) {
|
192 |
// continue;
|
193 |
// }
|
194 |
return fn;
|
195 |
} |
196 |
} |
197 |
// if (this.scripts != null && ! this.scripts.isEmpty()) {
|
198 |
// return new ScriptFunction(name);
|
199 |
// }
|
200 |
return null; |
201 |
} |
202 |
|
203 |
@Override
|
204 |
public Collection<String> variables() { |
205 |
Set<String> theVars = new HashSet<>(); |
206 |
for (SymbolTable symbolTable : this.symbolTables) { |
207 |
theVars.addAll(symbolTable.variables()); |
208 |
} |
209 |
theVars.addAll(this.localvariables());
|
210 |
return Collections.unmodifiableCollection(theVars); |
211 |
} |
212 |
|
213 |
@Override
|
214 |
public Collection<Function> functions() { |
215 |
Set<Function> theFunctions = new HashSet<>(); |
216 |
for (SymbolTable symbolTable : this.symbolTables) { |
217 |
theFunctions.addAll(symbolTable.functions()); |
218 |
} |
219 |
if (this.functions != null) { |
220 |
theFunctions.addAll(this.functions.values());
|
221 |
} |
222 |
return Collections.unmodifiableCollection(theFunctions); |
223 |
} |
224 |
|
225 |
@Override
|
226 |
public Collection<Function> localfunctions() { |
227 |
if( this.functions == null ) { |
228 |
return Collections.EMPTY_LIST; |
229 |
} |
230 |
return Collections.unmodifiableCollection(this.functions.values()); |
231 |
} |
232 |
|
233 |
@Override
|
234 |
public Collection<String> localvariables() { |
235 |
if( this.vars == null ) { |
236 |
return Collections.EMPTY_LIST; |
237 |
} |
238 |
return Collections.unmodifiableCollection(this.vars.keySet()); |
239 |
} |
240 |
|
241 |
// @Override
|
242 |
// public Collection<Script> scripts() {
|
243 |
// Set<Script> theScripts = new HashSet<>();
|
244 |
// for (SymbolTable symbolTable : this.symbolTables) {
|
245 |
// theScripts.addAll(symbolTable.scripts());
|
246 |
// }
|
247 |
// if (this.scripts != null) {
|
248 |
// theScripts.addAll(this.scripts);
|
249 |
// }
|
250 |
// return Collections.unmodifiableCollection(theScripts);
|
251 |
// }
|
252 |
//
|
253 |
@Override
|
254 |
public Iterator<Function> iterator() { |
255 |
return this.functions().iterator(); |
256 |
} |
257 |
|
258 |
@Override
|
259 |
public SymbolTable clone() throws CloneNotSupportedException { |
260 |
SymbolTable other = (SymbolTable) super.clone();
|
261 |
return other;
|
262 |
} |
263 |
|
264 |
@Override
|
265 |
public boolean isSQLCompatible(String name) { |
266 |
Function f = function(name); |
267 |
if( f!=null ) { |
268 |
return f.isSQLCompatible();
|
269 |
} |
270 |
return true; |
271 |
} |
272 |
|
273 |
} |