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 @ 47077
History | View | Annotate | Download (25.2 KB)
1 |
package org.gvsig.expressionevaluator.impl; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.PushbackReader; |
5 |
import java.io.StringReader; |
6 |
import org.gvsig.expressionevaluator.spi.BaseExpressionEvaluator; |
7 |
import java.io.StringWriter; |
8 |
import java.io.Writer; |
9 |
import java.net.URI; |
10 |
import java.util.ArrayList; |
11 |
import org.gvsig.expressionevaluator.Grammar; |
12 |
import org.gvsig.expressionevaluator.GrammarFactory; |
13 |
import java.util.Collection; |
14 |
import java.util.Collections; |
15 |
import java.util.HashMap; |
16 |
import java.util.List; |
17 |
import java.util.Map; |
18 |
import java.util.Objects; |
19 |
import java.util.regex.Matcher; |
20 |
import java.util.regex.Pattern; |
21 |
import org.apache.commons.io.FilenameUtils; |
22 |
import org.apache.commons.io.IOUtils; |
23 |
import org.apache.commons.io.output.ProxyWriter; |
24 |
import org.apache.commons.lang3.ArrayUtils; |
25 |
import org.apache.commons.lang3.StringUtils; |
26 |
import org.gvsig.expressionevaluator.Code; |
27 |
import org.gvsig.expressionevaluator.CodeBuilder; |
28 |
import org.gvsig.expressionevaluator.Expression; |
29 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
30 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
31 |
import org.gvsig.expressionevaluator.Interpreter; |
32 |
import org.gvsig.expressionevaluator.LexicalAnalyzer; |
33 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
34 |
import org.gvsig.expressionevaluator.Optimizer; |
35 |
import org.gvsig.expressionevaluator.SymbolTable; |
36 |
import org.gvsig.expressionevaluator.SymbolTableFactory; |
37 |
import org.gvsig.expressionevaluator.Compiler; |
38 |
import org.gvsig.expressionevaluator.ExpressionEvaluator; |
39 |
import org.gvsig.expressionevaluator.Formatter; |
40 |
import org.gvsig.expressionevaluator.GrammarSet; |
41 |
import org.gvsig.expressionevaluator.ReprMethod; |
42 |
import org.gvsig.expressionevaluator.impl.repr.ReprNull; |
43 |
import org.gvsig.expressionevaluator.impl.repr.ReprObject; |
44 |
import org.gvsig.expressionevaluator.spi.formatter.value.BaseFormatter; |
45 |
import org.gvsig.tools.ToolsLocator; |
46 |
import org.gvsig.tools.bookmarksandhistory.Bookmarks; |
47 |
import org.gvsig.tools.bookmarksandhistory.History; |
48 |
import org.gvsig.tools.dispose.DisposeUtils; |
49 |
import org.gvsig.tools.exception.BaseException; |
50 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
51 |
import org.gvsig.tools.script.Script; |
52 |
import org.slf4j.Logger; |
53 |
import org.slf4j.LoggerFactory; |
54 |
|
55 |
|
56 |
@SuppressWarnings("UseSpecificCatch") |
57 |
public class DefaultExpressionEvaluatorManager implements ExpressionEvaluatorManager { |
58 |
|
59 |
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExpressionEvaluatorManager.class); |
60 |
|
61 |
private Double accuracy; |
62 |
private final Map<String,SymbolTableFactory>symbolTableFactories; |
63 |
private final Map<String,GrammarFactory> grammarFactories; |
64 |
private Bookmarks<Expression> bookmarks; |
65 |
private static final String BOOKMARKSANDHISTORY_NAME = "ExpressionManager"; |
66 |
private History<Expression> history; |
67 |
private SymbolTable inmutableSymbolTable;
|
68 |
private ResourcesStorage scriptsResourcesStorage;
|
69 |
private final List<ClassLoader> loaders; |
70 |
private Formatter<ExpressionBuilder.Value> expressionBuilderFormatter; |
71 |
|
72 |
public DefaultExpressionEvaluatorManager() {
|
73 |
this.symbolTableFactories = new HashMap<>(); |
74 |
this.grammarFactories = new HashMap<>(); |
75 |
this.loaders = new ArrayList<>(); |
76 |
this.scriptsResourcesStorage = ResourcesStorage.EMPTY_RESOURCESSTORAGE;
|
77 |
this.loaders.add(this.getClass().getClassLoader()); |
78 |
this.expressionBuilderFormatter = new BaseFormatter(); |
79 |
} |
80 |
|
81 |
@Override
|
82 |
public SymbolTable getSymbolTable(String name) { |
83 |
if( name == null ) { |
84 |
return null; |
85 |
} |
86 |
SymbolTableFactory factory = this.symbolTableFactories.get(name.toUpperCase());
|
87 |
if( factory == null ) { |
88 |
return null; |
89 |
} |
90 |
return factory.create();
|
91 |
} |
92 |
|
93 |
@Override
|
94 |
public Collection<SymbolTableFactory> getSymbolTableFactories() { |
95 |
return this.symbolTableFactories.values(); |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public final void registerSymbolTable(SymbolTableFactory factory) { |
100 |
if( factory == null ) { |
101 |
throw new IllegalArgumentException("factory can't be null"); |
102 |
} |
103 |
this.symbolTableFactories.put(factory.getName().toUpperCase(),factory);
|
104 |
this.inmutableSymbolTable = null; |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public SymbolTable getInmutableSymbolTable() {
|
109 |
if( this.inmutableSymbolTable==null ) { |
110 |
this.inmutableSymbolTable = new InmutableSymbolTable(); |
111 |
} |
112 |
return this.inmutableSymbolTable; |
113 |
} |
114 |
|
115 |
@Override
|
116 |
public Object evaluate(String source) { |
117 |
Compiler compiler = this.createCompiler(); |
118 |
Code code = compiler.compileExpression(source); |
119 |
DefaultInterpreter interpreter = new DefaultInterpreter();
|
120 |
return interpreter.run(code);
|
121 |
} |
122 |
|
123 |
@Override
|
124 |
public Object evaluate(SymbolTable symbolTable, String source) { |
125 |
Compiler compiler = this.createCompiler(); |
126 |
Code code = compiler.compileExpression(source); |
127 |
return this.evaluate(symbolTable, code); |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public Object evaluate(SymbolTable symbolTable, Code code) { |
132 |
DefaultInterpreter interpreter = new DefaultInterpreter();
|
133 |
if( symbolTable!=null ) { |
134 |
interpreter.setSymbolTable(symbolTable); |
135 |
} |
136 |
return interpreter.run(code);
|
137 |
} |
138 |
|
139 |
@Override
|
140 |
public String evaluateDynamicText(String source) { |
141 |
return evaluateDynamicText(null, source); |
142 |
} |
143 |
|
144 |
@Override
|
145 |
public boolean isDynamicText(String source) { |
146 |
String[] sources = StringUtils.substringsBetween(source, DYNAMICTEXT_STARTTAG, DYNAMICTEXT_ENDTAG); |
147 |
if( ArrayUtils.isEmpty(sources) ) {
|
148 |
return false; |
149 |
} |
150 |
return true; |
151 |
} |
152 |
|
153 |
|
154 |
private static final int MODE_EXPRESSION = 0; |
155 |
private static final int MODE_STATEMENT = 1; |
156 |
|
157 |
public static String dynamicTextToScript(String s, String contentsName) { |
158 |
PushbackReader reader = new PushbackReader(new StringReader(s),100); |
159 |
StringBuilder script = new StringBuilder(); |
160 |
StringBuilder buffer = new StringBuilder(); |
161 |
try {
|
162 |
script.append("begin\n");
|
163 |
// script.append(contentsName);
|
164 |
// script.append(" := StringWriter();\n");
|
165 |
int mode;
|
166 |
int ch;
|
167 |
int ch2;
|
168 |
while(true) { |
169 |
ch = reader.read(); |
170 |
if( ch == -1 ) { |
171 |
break;
|
172 |
} |
173 |
switch(ch) {
|
174 |
case '<': |
175 |
ch2 = reader.read(); |
176 |
if( ch2 != '%') { |
177 |
buffer.append((char)ch);
|
178 |
buffer.append((char)ch2);
|
179 |
break;
|
180 |
} |
181 |
ch2 = reader.read(); |
182 |
if( ch2 == '=' ) { |
183 |
mode = MODE_EXPRESSION; |
184 |
} else {
|
185 |
mode = MODE_STATEMENT; |
186 |
reader.unread(ch2); |
187 |
} |
188 |
if( buffer.length()>0 ) { |
189 |
script.append(contentsName); |
190 |
script.append(".append('");
|
191 |
script.append(buffer.toString()); |
192 |
script.append("');");
|
193 |
} |
194 |
buffer.setLength(0);
|
195 |
while(true) { |
196 |
ch = reader.read(); |
197 |
if( ch==-1) { |
198 |
break;
|
199 |
} |
200 |
if( ch == '%' ) { |
201 |
ch2 = reader.read(); |
202 |
if( ch2 == '>') { |
203 |
if( mode == MODE_EXPRESSION ) {
|
204 |
if( buffer.length()>0 ) { |
205 |
script.append(contentsName); |
206 |
script.append(".append(");
|
207 |
script.append(buffer.toString()); |
208 |
script.append(");");
|
209 |
} |
210 |
} else {
|
211 |
script.append(buffer.toString()); |
212 |
} |
213 |
buffer.setLength(0);
|
214 |
break;
|
215 |
} else {
|
216 |
buffer.append((char)ch);
|
217 |
buffer.append((char)ch2);
|
218 |
} |
219 |
} else {
|
220 |
buffer.append((char)ch);
|
221 |
} |
222 |
} |
223 |
break;
|
224 |
case '\'': |
225 |
buffer.append('\'');
|
226 |
buffer.append('\'');
|
227 |
break;
|
228 |
default:
|
229 |
buffer.append((char)ch);
|
230 |
break;
|
231 |
} |
232 |
} |
233 |
} catch (IOException ex) { |
234 |
|
235 |
} |
236 |
if( buffer.length()>0 ) { |
237 |
script.append(contentsName); |
238 |
script.append(".append('");
|
239 |
script.append(buffer.toString()); |
240 |
script.append("');");
|
241 |
script.append("\n");
|
242 |
} |
243 |
// script.append(contentsName);
|
244 |
// script.append(".toString();\n");
|
245 |
script.append("end\n");
|
246 |
|
247 |
return script.toString();
|
248 |
} |
249 |
|
250 |
@Override
|
251 |
public String evaluateDynamicText(SymbolTable symbolTable, String source) { |
252 |
return evaluateDynamicText(symbolTable, source, null); |
253 |
} |
254 |
|
255 |
public static class AppendExWriter extends ProxyWriter { |
256 |
public AppendExWriter(Writer writer) { |
257 |
super(writer);
|
258 |
} |
259 |
|
260 |
public Writer append(Object x) throws IOException { |
261 |
if( x!=null ) { |
262 |
this.append(x.toString());
|
263 |
} |
264 |
return this; |
265 |
} |
266 |
|
267 |
public Writer getWriter() { |
268 |
return this.out; |
269 |
} |
270 |
} |
271 |
|
272 |
@Override
|
273 |
public String evaluateDynamicText(SymbolTable symbolTable, String source, Writer output) { |
274 |
return this.evaluateDynamicText(symbolTable, source, output, scriptsResourcesStorage); |
275 |
} |
276 |
|
277 |
@Override
|
278 |
public String evaluateDynamicText(SymbolTable symbolTable, String source, Writer output, ResourcesStorage resources) { |
279 |
try {
|
280 |
if (!StringUtils.contains(source, DYNAMICTEXT_STARTTAG)) {
|
281 |
if (output == null) { |
282 |
return source;
|
283 |
} |
284 |
output.append(source); |
285 |
return null; |
286 |
} |
287 |
AppendExWriter writer; |
288 |
if (output == null) { |
289 |
writer = new AppendExWriter(new StringWriter()); |
290 |
} else {
|
291 |
writer = new AppendExWriter(output);
|
292 |
} |
293 |
String script = dynamicTextToScript(source, "contents"); |
294 |
MutableSymbolTable st = this.createSymbolTable();
|
295 |
if (symbolTable != null) { |
296 |
st.addSymbolTable(symbolTable); |
297 |
} |
298 |
st.setVar("contents", writer);
|
299 |
|
300 |
Compiler compiler = this.createCompiler(); |
301 |
DefaultInterpreter interpreter = new DefaultInterpreter();
|
302 |
interpreter.setSymbolTable(st); |
303 |
if( resources!=null ) { |
304 |
interpreter.setResourcesStorage(resources); |
305 |
} |
306 |
|
307 |
Code code = compiler.compileExpression(script); |
308 |
interpreter.run(code); |
309 |
|
310 |
if (output == null) { |
311 |
return ((StringWriter) writer.getWriter()).toString(); |
312 |
} |
313 |
return null; |
314 |
} catch (IOException ex) { |
315 |
throw new RuntimeException("Can't evaluate dynamic-text.", ex); |
316 |
} |
317 |
} |
318 |
|
319 |
public String evaluateDynamicText_old(SymbolTable symbolTable, String source) { |
320 |
String[] sources = StringUtils.substringsBetween(source, DYNAMICTEXT_STARTTAG, DYNAMICTEXT_ENDTAG); |
321 |
if( ArrayUtils.isEmpty(sources) ) {
|
322 |
return source;
|
323 |
} |
324 |
String[] values = new String[sources.length]; |
325 |
|
326 |
DefaultInterpreter interpreter = new DefaultInterpreter();
|
327 |
if( symbolTable!=null ) { |
328 |
interpreter.setSymbolTable(symbolTable); |
329 |
} |
330 |
StringWriter writer = new StringWriter(); |
331 |
interpreter.setWriter(writer); |
332 |
DefaultCompiler compiler = new DefaultCompiler(this); |
333 |
for (int i = 0; i < sources.length; i++) { |
334 |
String theSource = sources[i];
|
335 |
if( StringUtils.startsWith(theSource, "=") ) { |
336 |
Code code = compiler.compileExpression(theSource.substring(1));
|
337 |
Object value = interpreter.run(code);
|
338 |
values[i] = Objects.toString(value, "");
|
339 |
} else {
|
340 |
Code code = compiler.compileExpression(theSource.substring(0));
|
341 |
writer.getBuffer().setLength(0);
|
342 |
interpreter.run(code); |
343 |
values[i] = writer.toString(); |
344 |
} |
345 |
sources[i] = DYNAMICTEXT_STARTTAG+sources[i]+DYNAMICTEXT_ENDTAG; |
346 |
} |
347 |
String output = StringUtils.replaceEach(source, sources, values);
|
348 |
return output;
|
349 |
} |
350 |
|
351 |
@Override
|
352 |
public Code compile(String source) { |
353 |
Compiler compiler = this.createCompiler(); |
354 |
return compiler.compileExpression(source);
|
355 |
} |
356 |
|
357 |
@Override
|
358 |
public Code compile(LexicalAnalyzer lex, String source) { |
359 |
Compiler compiler = this.createCompiler(); |
360 |
compiler.setLexicalAnalyzer(lex); |
361 |
return compiler.compileExpression(source);
|
362 |
} |
363 |
|
364 |
@Override
|
365 |
public Code optimize(SymbolTable symbolTable, Code code) {
|
366 |
Optimizer optimizer = this.createOptimizer();
|
367 |
return optimizer.optimize(symbolTable, code);
|
368 |
} |
369 |
|
370 |
@Override
|
371 |
public MutableSymbolTable createSymbolTable() {
|
372 |
DefaultSymbolTable theSymbolTable = new DefaultSymbolTable();
|
373 |
return theSymbolTable;
|
374 |
} |
375 |
|
376 |
@Override
|
377 |
public void populateSymbolTable(SymbolTable aSymbolTable) { |
378 |
for (SymbolTableFactory factory : this.getSymbolTableFactories() ) { |
379 |
try {
|
380 |
if( factory.isAutoload() ) {
|
381 |
SymbolTable symbolTable = factory.create(); |
382 |
aSymbolTable.addSymbolTable(symbolTable); |
383 |
} |
384 |
} catch(Throwable th) { |
385 |
String factoryName = "Unknown"; |
386 |
try {
|
387 |
factoryName = factory.getName(); |
388 |
} catch(Throwable th2) { |
389 |
// Do nothing
|
390 |
} |
391 |
LOGGER.warn("Can't create symbol table '"+factoryName+"', ignore this symbol table."); |
392 |
LOGGER.debug("Error creating symbol table.",th);
|
393 |
} |
394 |
} |
395 |
} |
396 |
|
397 |
@Override
|
398 |
public LexicalAnalyzer createLexicalAnalyzer() {
|
399 |
return new SQLLexicalAnalyzer(); |
400 |
} |
401 |
|
402 |
@Override
|
403 |
public CodeBuilder createCodeBuilder() {
|
404 |
return new DefaultCodeBuilder(this); |
405 |
} |
406 |
|
407 |
@Override
|
408 |
public Compiler createCompiler() { |
409 |
DefaultCompiler compiler = new DefaultCompiler(this); |
410 |
this.populateGrammars(compiler);
|
411 |
return compiler;
|
412 |
} |
413 |
|
414 |
@Override
|
415 |
public Interpreter createInterpreter() {
|
416 |
Interpreter interpreter = new DefaultInterpreter();
|
417 |
interpreter.setResourcesStorage(this.scriptsResourcesStorage);
|
418 |
return interpreter;
|
419 |
} |
420 |
|
421 |
@Override
|
422 |
public Double getAccuracy() { |
423 |
return this.accuracy; |
424 |
} |
425 |
|
426 |
@Override
|
427 |
public void setAccuracy(Double accuracy) { |
428 |
this.accuracy = accuracy;
|
429 |
} |
430 |
|
431 |
@Override
|
432 |
public Expression createExpression() { |
433 |
DefaultExpression e = new DefaultExpression(this); |
434 |
return e;
|
435 |
} |
436 |
|
437 |
@Override
|
438 |
public ExpressionBuilder createExpressionBuilder() {
|
439 |
ExpressionBuilder x = new DefaultExpressionBuilder(this); |
440 |
return x;
|
441 |
} |
442 |
|
443 |
@Override
|
444 |
public Optimizer createOptimizer() {
|
445 |
Optimizer x = new DefaultOptimizer(this); |
446 |
return x;
|
447 |
} |
448 |
|
449 |
@Override
|
450 |
public void registerGrammar(GrammarFactory factory) { |
451 |
if( factory==null ) { |
452 |
throw new IllegalArgumentException("factory can't be null"); |
453 |
} |
454 |
this.grammarFactories.put(factory.getName(), factory);
|
455 |
} |
456 |
|
457 |
@Override
|
458 |
public Collection<GrammarFactory> getGrammarFactories() { |
459 |
return this.grammarFactories.values(); |
460 |
} |
461 |
|
462 |
public void populateGrammars(Compiler compiler) { |
463 |
GrammarSet grammarSet = compiler.getGrammars(); |
464 |
for (GrammarFactory factory : this.getGrammarFactories() ) { |
465 |
if( factory.isAutoload() ) {
|
466 |
Grammar grammar = factory.create(); |
467 |
grammarSet.add(grammar); |
468 |
} |
469 |
} |
470 |
} |
471 |
|
472 |
@Override
|
473 |
public Grammar createGrammar(String name) { |
474 |
DefaultGrammar grammar = new DefaultGrammar(name);
|
475 |
return grammar;
|
476 |
} |
477 |
|
478 |
@Override
|
479 |
public Bookmarks<Expression> getBookmarks() { |
480 |
if( this.bookmarks==null ) { |
481 |
this.bookmarks = ToolsLocator.getBookmarksAndHistoryManager().getBookmarksGroup(BOOKMARKSANDHISTORY_NAME);
|
482 |
|
483 |
} |
484 |
return this.bookmarks; |
485 |
} |
486 |
|
487 |
@Override
|
488 |
public History<Expression> getHistory() { |
489 |
if( this.history==null ) { |
490 |
this.history = ToolsLocator.getBookmarksAndHistoryManager().getHistoryGroup(BOOKMARKSANDHISTORY_NAME);
|
491 |
} |
492 |
return this.history; |
493 |
} |
494 |
|
495 |
@Override
|
496 |
public Script createScript(String name, String code, String languaje) { |
497 |
SimpleScript sc = new SimpleScript(this.createInterpreter(), name, code); |
498 |
return sc;
|
499 |
} |
500 |
|
501 |
@Override
|
502 |
public Script locateScript(String name) { |
503 |
return loadScript(this.scriptsResourcesStorage, name); |
504 |
} |
505 |
|
506 |
@Override
|
507 |
public Script loadScript(final URI location) { |
508 |
ResourcesStorage.Resource res = null;
|
509 |
try {
|
510 |
if( location==null ) { |
511 |
return null; |
512 |
} |
513 |
String resourceName = FilenameUtils.getBaseName(location.getPath());
|
514 |
res = ResourcesStorage.createResource(resourceName, location); |
515 |
if( res == null || !res.exists() ) { |
516 |
return null; |
517 |
} |
518 |
Script script = loadScript(res, resourceName); |
519 |
return script;
|
520 |
} catch (Exception ex) { |
521 |
LOGGER.warn("Can't load script from URI.", ex);
|
522 |
return null; |
523 |
} finally {
|
524 |
IOUtils.closeQuietly(res); |
525 |
} |
526 |
} |
527 |
|
528 |
|
529 |
@Override
|
530 |
public Script loadScript(ResourcesStorage storage, String name) { |
531 |
ResourcesStorage.Resource res = null;
|
532 |
try {
|
533 |
if( storage==null ) { |
534 |
return null; |
535 |
} |
536 |
res = storage.getResource(name); |
537 |
if( res == null || !res.exists() ) { |
538 |
return null; |
539 |
} |
540 |
Script script = loadScript(res, name); |
541 |
return script;
|
542 |
} catch (Exception ex) { |
543 |
LOGGER.warn("Can't load script from resources storage.", ex);
|
544 |
return null; |
545 |
} finally {
|
546 |
IOUtils.closeQuietly(res); |
547 |
} |
548 |
|
549 |
} |
550 |
|
551 |
private static final Pattern RE_LANG = Pattern.compile(".*lang[:]\\s*([a-zA-Z_][a-zA-Z_0-9_]*).*"); |
552 |
private static final Pattern RE_ENCODING = Pattern.compile(".*encoding[:]\\s*([a-zA-Z_][a-zA-Z0-9_-]*).*"); |
553 |
|
554 |
private Script loadScript(ResourcesStorage.Resource res, String name) { |
555 |
try {
|
556 |
if( res == null || !res.exists() ) { |
557 |
return null; |
558 |
} |
559 |
String head = EncodingUtils.getFirstLine(res.asInputStream());
|
560 |
IOUtils.closeQuietly(res); // Eliminar con org.gvsig.tools > 3.0.230
|
561 |
if( StringUtils.isEmpty(head) ) {
|
562 |
return null; |
563 |
} |
564 |
|
565 |
String encoding = EncodingUtils.getEncoding(head);
|
566 |
|
567 |
String lang = "cosa"; |
568 |
Matcher m = RE_LANG.matcher(head);
|
569 |
if( m!=null && m.matches() && m.groupCount()==1 ) { |
570 |
String s = m.group(1); |
571 |
if( !StringUtils.isBlank(s) ) {
|
572 |
lang = s; |
573 |
} |
574 |
} |
575 |
String source;
|
576 |
if( StringUtils.isBlank(encoding) ) {
|
577 |
source = IOUtils.toString(res.asInputStream()); |
578 |
} else {
|
579 |
source = IOUtils.toString(res.asInputStream(), encoding); |
580 |
} |
581 |
Script script = this.createScript(name, source, lang);
|
582 |
return script;
|
583 |
} catch (Exception ex) { |
584 |
LOGGER.warn("Can't load script from resource.", ex);
|
585 |
return null; |
586 |
} finally {
|
587 |
IOUtils.closeQuietly(res); |
588 |
} |
589 |
|
590 |
} |
591 |
|
592 |
@Override
|
593 |
public ResourcesStorage getScriptsResourcesStorage() {
|
594 |
return this.scriptsResourcesStorage; |
595 |
} |
596 |
|
597 |
@Override
|
598 |
public void setScriptsResourcesStorage(ResourcesStorage scriptsResourcesStorage) { |
599 |
if(this.scriptsResourcesStorage != null){ |
600 |
DisposeUtils.disposeQuietly(this.scriptsResourcesStorage);
|
601 |
} |
602 |
DisposeUtils.bind(scriptsResourcesStorage); |
603 |
this.scriptsResourcesStorage = scriptsResourcesStorage;
|
604 |
} |
605 |
|
606 |
|
607 |
private final List<ReprMethod> reprMethods = new ArrayList<>(); |
608 |
private final Map<Class,ReprMethod> reprMethodsCache = new HashMap<>(); |
609 |
private final ReprMethod reprNull = new ReprNull(); |
610 |
private final ReprMethod reprObject = new ReprObject(); |
611 |
|
612 |
@Override
|
613 |
public void addReprMethod(ReprMethod method) { |
614 |
this.reprMethods.add(method);
|
615 |
this.reprMethodsCache.clear();
|
616 |
} |
617 |
|
618 |
@Override
|
619 |
public ReprMethod getReprMethod(Object value) { |
620 |
if( value == null ) { |
621 |
return this.reprNull; |
622 |
} |
623 |
ReprMethod method = this.reprMethodsCache.get(value.getClass());
|
624 |
if( method!=null ) { |
625 |
return method;
|
626 |
} |
627 |
for (ReprMethod theMethod : reprMethods) {
|
628 |
if( theMethod.isApplicable(value) ) {
|
629 |
this.reprMethodsCache.put(value.getClass(), theMethod);
|
630 |
return theMethod;
|
631 |
} |
632 |
} |
633 |
return this.reprObject; |
634 |
} |
635 |
|
636 |
@Override
|
637 |
public void registerClassLoader(ClassLoader loader) { |
638 |
this.loaders.add(loader);
|
639 |
} |
640 |
|
641 |
@Override
|
642 |
public List<ClassLoader> getClassLoaders() { |
643 |
return Collections.unmodifiableList(loaders); |
644 |
} |
645 |
|
646 |
@Override
|
647 |
public Formatter<ExpressionBuilder.Value> getExpressionBuilderFormatter() { |
648 |
return expressionBuilderFormatter;
|
649 |
} |
650 |
|
651 |
@Override
|
652 |
public void registerExpressionBuilderFormatter(Formatter<ExpressionBuilder.Value> formatter) { |
653 |
this.expressionBuilderFormatter = formatter;
|
654 |
} |
655 |
|
656 |
@Override
|
657 |
public ExpressionEvaluator createExpressionEvaluator(Expression expression) { |
658 |
return new BaseExpressionEvaluator(expression); |
659 |
} |
660 |
|
661 |
@Override
|
662 |
public ExpressionEvaluator createEvaluator(String expression) { |
663 |
Expression exp = this.createExpression(); |
664 |
exp.setPhrase(expression); |
665 |
return new BaseExpressionEvaluator(exp); |
666 |
} |
667 |
|
668 |
@Override
|
669 |
public boolean hasHostExpressions(Code statement) { |
670 |
try {
|
671 |
List<Code> hostExpressions = new ArrayList<>(); |
672 |
statement.accept( |
673 |
(Object code) -> {
|
674 |
if( !(code instanceof Code.Callable) ) { |
675 |
return;
|
676 |
} |
677 |
Code.Callable callable = (Code.Callable) code; |
678 |
if( StringUtils.equalsIgnoreCase(callable.name(), ExpressionBuilder.FUNCTION_$HOSTEXPRESSION) ) { |
679 |
hostExpressions.add(callable); |
680 |
} |
681 |
}, null
|
682 |
); |
683 |
return !hostExpressions.isEmpty();
|
684 |
} catch (BaseException ex) {
|
685 |
throw new RuntimeException("Can't check host expressions", ex); |
686 |
} |
687 |
} |
688 |
|
689 |
@Override
|
690 |
public boolean hasHostExpressions(ExpressionBuilder.Value statement) { |
691 |
return HostExpressionUtils.hasHostExpressions(statement);
|
692 |
} |
693 |
|
694 |
@Override
|
695 |
public Code resolveHostExpressions(Code statement, Interpreter interpreter) {
|
696 |
return HostExpressionUtils.resolveHostExpressions(statement, interpreter);
|
697 |
} |
698 |
|
699 |
@Override
|
700 |
public ExpressionBuilder.Value resolveHostExpressions(ExpressionBuilder.Value statement, SymbolTable symbolTable) {
|
701 |
return HostExpressionUtils.resolveHostExpressions(statement, symbolTable);
|
702 |
} |
703 |
|
704 |
@Override
|
705 |
public Code resolveHostExpressions(Code statement, SymbolTable symbolTable) {
|
706 |
return HostExpressionUtils.resolveHostExpressions(statement, symbolTable);
|
707 |
} |
708 |
|
709 |
@Override
|
710 |
public Expression resolveHostExpressions(Expression expression, SymbolTable symbolTable) { |
711 |
return HostExpressionUtils.resolveHostExpressions(expression, symbolTable);
|
712 |
} |
713 |
|
714 |
@Override
|
715 |
public boolean hasHostExpressions(String statement) { |
716 |
return HostExpressionUtils.hasHostExpressions(statement);
|
717 |
} |
718 |
|
719 |
@Override
|
720 |
public ExpressionBuilder.Value getHostExpressionValue(ExpressionBuilder.Function hostExpression, ExpressionBuilder expbuilder) {
|
721 |
return HostExpressionUtils.getHostExpressionValue(hostExpression, expbuilder);
|
722 |
} |
723 |
|
724 |
@Override
|
725 |
public ExpressionBuilder.Value getHostExpressionValue(ExpressionBuilder.Function hostExpression, ExpressionBuilder expbuilder, SymbolTable symbolTable) {
|
726 |
return HostExpressionUtils.getHostExpressionValue(hostExpression, expbuilder, symbolTable);
|
727 |
} |
728 |
|
729 |
} |
730 |
|