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 / DefaultExpressionEvaluatorManager.java @ 44390
History | View | Annotate | Download (9.53 KB)
1 |
package org.gvsig.expressionevaluator.impl; |
---|---|
2 |
|
3 |
import java.io.InputStream; |
4 |
import java.io.StringWriter; |
5 |
import java.net.URI; |
6 |
import java.net.URL; |
7 |
import org.gvsig.expressionevaluator.Grammar; |
8 |
import org.gvsig.expressionevaluator.GrammarFactory; |
9 |
import java.util.Collection; |
10 |
import java.util.HashMap; |
11 |
import java.util.Map; |
12 |
import java.util.Objects; |
13 |
import org.apache.commons.io.FilenameUtils; |
14 |
import org.apache.commons.io.IOUtils; |
15 |
import org.apache.commons.lang3.ArrayUtils; |
16 |
import org.apache.commons.lang3.StringUtils; |
17 |
import org.gvsig.expressionevaluator.Code; |
18 |
import org.gvsig.expressionevaluator.CodeBuilder; |
19 |
import org.gvsig.expressionevaluator.Expression; |
20 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
21 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
22 |
import org.gvsig.expressionevaluator.Interpreter; |
23 |
import org.gvsig.expressionevaluator.LexicalAnalyzer; |
24 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
25 |
import org.gvsig.expressionevaluator.Optimizer; |
26 |
import org.gvsig.expressionevaluator.SymbolTable; |
27 |
import org.gvsig.expressionevaluator.SymbolTableFactory; |
28 |
import org.gvsig.expressionevaluator.Compiler; |
29 |
import org.gvsig.expressionevaluator.GrammarSet; |
30 |
import org.gvsig.expressionevaluator.impl.javascripting.CosaScriptEngineFactory; |
31 |
import org.gvsig.tools.bookmarksandhistory.Bookmarks; |
32 |
import org.gvsig.tools.bookmarksandhistory.History; |
33 |
import org.gvsig.tools.bookmarksandhistory.impl.BaseBookmarks; |
34 |
import org.gvsig.tools.bookmarksandhistory.impl.BaseHistory; |
35 |
import org.gvsig.tools.script.Script; |
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
|
40 |
@SuppressWarnings("UseSpecificCatch") |
41 |
public class DefaultExpressionEvaluatorManager implements ExpressionEvaluatorManager { |
42 |
|
43 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExpressionEvaluatorManager.class); |
44 |
|
45 |
private Double accuracy; |
46 |
private final Map<String,SymbolTableFactory>symbolTableFactories; |
47 |
private final Map<String,GrammarFactory> grammarFactories; |
48 |
private Bookmarks<Expression> bookmarks; |
49 |
private History<Expression> history; |
50 |
|
51 |
public DefaultExpressionEvaluatorManager() {
|
52 |
this.symbolTableFactories = new HashMap<>(); |
53 |
this.grammarFactories = new HashMap<>(); |
54 |
} |
55 |
|
56 |
@Override
|
57 |
public SymbolTable getSymbolTable(String name) { |
58 |
if( name == null ) { |
59 |
return null; |
60 |
} |
61 |
SymbolTableFactory factory = this.symbolTableFactories.get(name.toUpperCase());
|
62 |
if( factory == null ) { |
63 |
return null; |
64 |
} |
65 |
return factory.create();
|
66 |
} |
67 |
|
68 |
@Override
|
69 |
public Collection<SymbolTableFactory> getSymbolTableFactories() { |
70 |
return this.symbolTableFactories.values(); |
71 |
} |
72 |
|
73 |
@Override
|
74 |
public final void registerSymbolTable(SymbolTableFactory factory) { |
75 |
if( factory == null ) { |
76 |
throw new IllegalArgumentException("factory can't be null"); |
77 |
} |
78 |
this.symbolTableFactories.put(factory.getName().toUpperCase(),factory);
|
79 |
} |
80 |
|
81 |
@Override
|
82 |
public Object evaluate(String source) { |
83 |
DefaultInterpreter interpreter = new DefaultInterpreter();
|
84 |
DefaultCompiler compiler = new DefaultCompiler();
|
85 |
Code code = compiler.compileExpression(source); |
86 |
return interpreter.run(code);
|
87 |
} |
88 |
|
89 |
@Override
|
90 |
public Object evaluate(SymbolTable symbolTable, String source) { |
91 |
DefaultInterpreter interpreter = new DefaultInterpreter();
|
92 |
DefaultCompiler compiler = new DefaultCompiler();
|
93 |
Code code = compiler.compileExpression(source); |
94 |
interpreter.setSymbolTable(symbolTable); |
95 |
return interpreter.run(code);
|
96 |
} |
97 |
|
98 |
@Override
|
99 |
public Object evaluate(SymbolTable symbolTable, Code code) { |
100 |
DefaultInterpreter interpreter = new DefaultInterpreter();
|
101 |
interpreter.setSymbolTable(symbolTable); |
102 |
return interpreter.run(code);
|
103 |
} |
104 |
|
105 |
@Override
|
106 |
public String evaluatePage(String source) { |
107 |
return evaluatePage(null, source); |
108 |
} |
109 |
|
110 |
@Override
|
111 |
public String evaluatePage(SymbolTable symbolTable, String source) { |
112 |
String[] sources = StringUtils.substringsBetween(source, "<%", "%>"); |
113 |
if( ArrayUtils.isEmpty(sources) ) {
|
114 |
return source;
|
115 |
} |
116 |
String[] values = new String[sources.length]; |
117 |
|
118 |
DefaultInterpreter interpreter = new DefaultInterpreter();
|
119 |
if( symbolTable!=null ) { |
120 |
interpreter.setSymbolTable(symbolTable); |
121 |
} |
122 |
StringWriter writer = new StringWriter(); |
123 |
interpreter.setWriter(writer); |
124 |
DefaultCompiler compiler = new DefaultCompiler();
|
125 |
for (int i = 0; i < sources.length; i++) { |
126 |
String theSource = sources[i];
|
127 |
if( StringUtils.startsWith(theSource, "=") ) { |
128 |
Code code = compiler.compileExpression(theSource.substring(1));
|
129 |
Object value = interpreter.run(code);
|
130 |
values[i] = Objects.toString(value, "");
|
131 |
} else {
|
132 |
Code code = compiler.compileExpression(theSource.substring(1));
|
133 |
writer.getBuffer().setLength(0);
|
134 |
interpreter.run(code); |
135 |
values[i] = writer.toString(); |
136 |
} |
137 |
sources[i] = "<%"+sources[i]+"%>"; |
138 |
} |
139 |
String output = StringUtils.replaceEach(source, sources, values);
|
140 |
return output;
|
141 |
} |
142 |
|
143 |
@Override
|
144 |
public Code compile(String source) { |
145 |
Compiler compiler = this.createCompiler(); |
146 |
return compiler.compileExpression(source);
|
147 |
} |
148 |
|
149 |
@Override
|
150 |
public Code compile(LexicalAnalyzer lex, String source) { |
151 |
Compiler compiler = this.createCompiler(); |
152 |
compiler.setLexicalAnalyzer(lex); |
153 |
return compiler.compileExpression(source);
|
154 |
} |
155 |
|
156 |
@Override
|
157 |
public Code optimize(SymbolTable symbolTable, Code code) {
|
158 |
Optimizer optimizer = this.createOptimizer();
|
159 |
return optimizer.optimize(symbolTable, code);
|
160 |
} |
161 |
|
162 |
@Override
|
163 |
public MutableSymbolTable createSymbolTable() {
|
164 |
DefaultSymbolTable theSymbolTable = new DefaultSymbolTable();
|
165 |
return theSymbolTable;
|
166 |
} |
167 |
|
168 |
public void populateSymbolTable(SymbolTable aSymbolTable) { |
169 |
for (SymbolTableFactory factory : this.getSymbolTableFactories() ) { |
170 |
if( factory.isAutoload() ) {
|
171 |
SymbolTable symbolTable = factory.create(); |
172 |
aSymbolTable.addSymbolTable(symbolTable); |
173 |
} |
174 |
} |
175 |
} |
176 |
|
177 |
@Override
|
178 |
public LexicalAnalyzer createLexicalAnalyzer() {
|
179 |
return new SQLLexicalAnalyzer(); |
180 |
} |
181 |
|
182 |
@Override
|
183 |
public CodeBuilder createCodeBuilder() {
|
184 |
return new DefaultCodeBuilder(); |
185 |
} |
186 |
|
187 |
@Override
|
188 |
public Compiler createCompiler() { |
189 |
DefaultCompiler compiler = new DefaultCompiler();
|
190 |
this.populateGrammars(compiler);
|
191 |
return compiler;
|
192 |
} |
193 |
|
194 |
@Override
|
195 |
public Interpreter createInterpreter() {
|
196 |
return new DefaultInterpreter(); |
197 |
} |
198 |
|
199 |
@Override
|
200 |
public Double getAccuracy() { |
201 |
return this.accuracy; |
202 |
} |
203 |
|
204 |
@Override
|
205 |
public void setAccuracy(Double accuracy) { |
206 |
this.accuracy = accuracy;
|
207 |
} |
208 |
|
209 |
@Override
|
210 |
public Expression createExpression() { |
211 |
DefaultExpression e = new DefaultExpression();
|
212 |
return e;
|
213 |
} |
214 |
|
215 |
@Override
|
216 |
public ExpressionBuilder createExpressionBuilder() {
|
217 |
ExpressionBuilder x = new DefaultExpressionBuilder();
|
218 |
return x;
|
219 |
} |
220 |
|
221 |
@Override
|
222 |
public Optimizer createOptimizer() {
|
223 |
Optimizer x = new DefaultOptimizer();
|
224 |
return x;
|
225 |
} |
226 |
|
227 |
@Override
|
228 |
public void registerGrammar(GrammarFactory factory) { |
229 |
if( factory==null ) { |
230 |
throw new IllegalArgumentException("factory can't be null"); |
231 |
} |
232 |
this.grammarFactories.put(factory.getName(), factory);
|
233 |
} |
234 |
|
235 |
@Override
|
236 |
public Collection<GrammarFactory> getGrammarFactories() { |
237 |
return this.grammarFactories.values(); |
238 |
} |
239 |
|
240 |
public void populateGrammars(Compiler compiler) { |
241 |
GrammarSet grammarSet = compiler.getGrammars(); |
242 |
for (GrammarFactory factory : this.getGrammarFactories() ) { |
243 |
if( factory.isAutoload() ) {
|
244 |
Grammar grammar = factory.create(); |
245 |
grammarSet.add(grammar); |
246 |
} |
247 |
} |
248 |
} |
249 |
|
250 |
@Override
|
251 |
public Grammar createGrammar(String name) { |
252 |
DefaultGrammar grammar = new DefaultGrammar(name);
|
253 |
return grammar;
|
254 |
} |
255 |
|
256 |
@Override
|
257 |
public Bookmarks<Expression> getBookmarks() { |
258 |
if( this.bookmarks==null ) { |
259 |
this.bookmarks = new BaseBookmarks<Expression>(); |
260 |
} |
261 |
return this.bookmarks; |
262 |
} |
263 |
|
264 |
@Override
|
265 |
public History<Expression> getHistory() { |
266 |
if( this.history==null ) { |
267 |
this.history = new BaseHistory<Expression>(20); |
268 |
} |
269 |
return this.history; |
270 |
} |
271 |
|
272 |
@Override
|
273 |
public Script createScript(String name, String code, String languaje) { |
274 |
SimpleScript sc = new SimpleScript(this.createInterpreter(), name, code); |
275 |
return sc;
|
276 |
} |
277 |
|
278 |
@Override
|
279 |
public Script locateScript(String name) { |
280 |
return null; |
281 |
} |
282 |
|
283 |
@Override
|
284 |
public Script loadScript(URI location) { |
285 |
InputStream is = null; |
286 |
String code = null; |
287 |
String name = null; |
288 |
try {
|
289 |
URL url = location.toURL();
|
290 |
is = url.openStream(); |
291 |
code = IOUtils.toString(is); |
292 |
name = FilenameUtils.getBaseName(location.getPath()); |
293 |
} catch(Exception ex) { |
294 |
return null; |
295 |
} finally {
|
296 |
IOUtils.closeQuietly(is); |
297 |
} |
298 |
return this.createScript(name, code, CosaScriptEngineFactory.LANGUAGE_NAME); |
299 |
} |
300 |
|
301 |
} |
302 |
|