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 @ 43987
History | View | Annotate | Download (5.98 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 | private class ScriptFunction extends AbstractFunction { |
||
28 | |||
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 | protected List<Script> scripts; |
||
52 | |||
53 | public AbstractSymbolTable() {
|
||
54 | this(null); |
||
55 | } |
||
56 | |||
57 | public AbstractSymbolTable(String name) { |
||
58 | this.name = name;
|
||
59 | this.symbolTables = new ArrayList<>(); |
||
60 | this.vars = null; |
||
61 | this.functions = null; |
||
62 | this.scripts = null; |
||
63 | } |
||
64 | |||
65 | @Override
|
||
66 | public String getName() { |
||
67 | return name;
|
||
68 | } |
||
69 | 43987 | jjdelcerro | |
70 | protected void addFunction(Function function) { |
||
71 | if (function == null) { |
||
72 | throw new IllegalArgumentException("function can't be null"); |
||
73 | } |
||
74 | this.getFunctions().put(function.name().toUpperCase(), function);
|
||
75 | } |
||
76 | 43983 | jjdelcerro | |
77 | @Override
|
||
78 | public void addSymbolTable(SymbolTable symbolTable) { |
||
79 | if (this.symbolTables.contains(symbolTable)) { |
||
80 | return;
|
||
81 | } |
||
82 | this.symbolTables.add(symbolTable);
|
||
83 | } |
||
84 | |||
85 | protected Map<String, Object> getVars() { |
||
86 | if (this.vars == null) { |
||
87 | this.vars = new HashMap<>(); |
||
88 | } |
||
89 | return this.vars; |
||
90 | } |
||
91 | |||
92 | protected Map<String, Function> getFunctions() { |
||
93 | if (this.functions == null) { |
||
94 | this.functions = new HashMap<>(); |
||
95 | } |
||
96 | return this.functions; |
||
97 | } |
||
98 | |||
99 | protected List<Script> getScripts() { |
||
100 | if (this.scripts == null) { |
||
101 | this.scripts = new ArrayList<>(); |
||
102 | } |
||
103 | return this.scripts; |
||
104 | } |
||
105 | |||
106 | @Override
|
||
107 | public boolean exists(String name) { |
||
108 | if (StringUtils.isEmpty(name)) {
|
||
109 | return false; |
||
110 | } |
||
111 | if (this.vars != null) { |
||
112 | if (this.vars.containsKey(name.toUpperCase())) { |
||
113 | return true; |
||
114 | } |
||
115 | } |
||
116 | for (SymbolTable other : this.symbolTables) { |
||
117 | if (other.exists(name)) {
|
||
118 | return true; |
||
119 | } |
||
120 | } |
||
121 | return false; |
||
122 | } |
||
123 | |||
124 | @Override
|
||
125 | public Object value(String name) { |
||
126 | if (StringUtils.isEmpty(name)) {
|
||
127 | return null; |
||
128 | } |
||
129 | if (this.vars != null) { |
||
130 | name = name.toUpperCase(); |
||
131 | if (this.vars.containsKey(name)) { |
||
132 | return this.vars.get(name); |
||
133 | } |
||
134 | } |
||
135 | for (SymbolTable other : this.symbolTables) { |
||
136 | if (other.exists(name)) {
|
||
137 | return other.value(name);
|
||
138 | } |
||
139 | } |
||
140 | return null; |
||
141 | } |
||
142 | |||
143 | @Override
|
||
144 | public Function function(String name) { |
||
145 | if (StringUtils.isEmpty(name)) {
|
||
146 | return null; |
||
147 | } |
||
148 | if (this.functions != null) { |
||
149 | name = name.toUpperCase(); |
||
150 | if (this.functions.containsKey(name)) { |
||
151 | return this.functions.get(name); |
||
152 | } |
||
153 | } |
||
154 | for (SymbolTable other : this.symbolTables) { |
||
155 | Function fn = other.function(name); |
||
156 | if (fn != null) { |
||
157 | if (fn instanceof ScriptFunction) { |
||
158 | continue;
|
||
159 | } |
||
160 | return fn;
|
||
161 | } |
||
162 | } |
||
163 | if (this.scripts == null || this.scripts.isEmpty()) { |
||
164 | return null; |
||
165 | } |
||
166 | return new ScriptFunction(name); |
||
167 | } |
||
168 | |||
169 | @Override
|
||
170 | public Collection<String> variables() { |
||
171 | Set<String> theVars = new HashSet<>(); |
||
172 | for (SymbolTable symbolTable : this.symbolTables) { |
||
173 | theVars.addAll(symbolTable.variables()); |
||
174 | } |
||
175 | if (this.vars != null) { |
||
176 | theVars.addAll(this.vars.keySet());
|
||
177 | } |
||
178 | return Collections.unmodifiableCollection(theVars); |
||
179 | } |
||
180 | |||
181 | @Override
|
||
182 | public Collection<Function> functions() { |
||
183 | Set<Function> theFunctions = new HashSet<>(); |
||
184 | for (SymbolTable symbolTable : this.symbolTables) { |
||
185 | theFunctions.addAll(symbolTable.functions()); |
||
186 | } |
||
187 | if (this.functions != null) { |
||
188 | theFunctions.addAll(this.functions.values());
|
||
189 | } |
||
190 | return Collections.unmodifiableCollection(theFunctions); |
||
191 | } |
||
192 | |||
193 | @Override
|
||
194 | public Collection<Script> scripts() { |
||
195 | Set<Script> theScripts = new HashSet<>(); |
||
196 | for (SymbolTable symbolTable : this.symbolTables) { |
||
197 | theScripts.addAll(symbolTable.scripts()); |
||
198 | } |
||
199 | if (this.scripts != null) { |
||
200 | theScripts.addAll(this.scripts);
|
||
201 | } |
||
202 | return Collections.unmodifiableCollection(theScripts); |
||
203 | } |
||
204 | |||
205 | @Override
|
||
206 | public Iterator<Function> iterator() { |
||
207 | return this.functions().iterator(); |
||
208 | } |
||
209 | |||
210 | @Override
|
||
211 | public SymbolTable clone() throws CloneNotSupportedException { |
||
212 | SymbolTable other = (SymbolTable) super.clone();
|
||
213 | return other;
|
||
214 | } |
||
215 | |||
216 | } |