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