Revision 46695 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
DefaultExpressionEvaluatorManager.java | ||
---|---|---|
12 | 12 |
import java.util.List; |
13 | 13 |
import java.util.Map; |
14 | 14 |
import java.util.Objects; |
15 |
import java.util.logging.Level; |
|
15 | 16 |
import java.util.regex.Matcher; |
16 | 17 |
import java.util.regex.Pattern; |
17 | 18 |
import org.apache.commons.io.FilenameUtils; |
... | ... | |
42 | 43 |
import org.gvsig.tools.bookmarksandhistory.History; |
43 | 44 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
44 | 45 |
import org.gvsig.tools.script.Script; |
46 |
import org.gvsig.tools.visitor.FilteredVisitable; |
|
45 | 47 |
import org.slf4j.Logger; |
46 | 48 |
import org.slf4j.LoggerFactory; |
47 | 49 |
|
... | ... | |
492 | 494 |
exp.setPhrase(expression); |
493 | 495 |
return new BaseExpressionEvaluator(exp); |
494 | 496 |
} |
497 |
|
|
498 |
public static Code resolveHostExpressions0(Interpreter interpreter, Code statement) throws Exception { |
|
499 |
List<Code> hostExpressions = new ArrayList<>(); |
|
500 |
statement.accept( |
|
501 |
(Object code) -> { |
|
502 |
hostExpressions.add((Code) code); |
|
503 |
}, |
|
504 |
(FilteredVisitable code) -> !(code instanceof Code.Callable |
|
505 |
&& StringUtils.equalsIgnoreCase(((Code.Callable) code).name(), ExpressionBuilder.FUNCTION_$HOSTEXPRESSION)) |
|
506 |
); |
|
507 |
if (hostExpressions.isEmpty()) { |
|
508 |
return statement; |
|
509 |
} |
|
510 |
Code newstatement = (Code) statement.clone(); |
|
511 |
hostExpressions.clear(); |
|
512 |
newstatement.accept( |
|
513 |
(Object code) -> { |
|
514 |
hostExpressions.add((Code) code); |
|
515 |
}, |
|
516 |
(FilteredVisitable code) -> !(code instanceof Code.Callable |
|
517 |
&& StringUtils.equalsIgnoreCase(((Code.Callable) code).name(), ExpressionBuilder.FUNCTION_$HOSTEXPRESSION)) |
|
518 |
); |
|
519 |
if (hostExpressions.isEmpty()) { |
|
520 |
return statement; |
|
521 |
} |
|
522 |
|
|
523 |
CodeBuilder codeBuilder = ((DefaultInterpreter)interpreter).getCodeBuilder(); |
|
524 |
List<Code.Constant> constants = new ArrayList<>(); |
|
525 |
for (Code hostExpression : hostExpressions) { |
|
526 |
Object v = interpreter.run(hostExpression); |
|
527 |
constants.add(codeBuilder.constant(v)); |
|
528 |
} |
|
529 |
|
|
530 |
for (int i = 0; i < hostExpressions.size(); i++) { |
|
531 |
Code hostExpression = hostExpressions.get(i); |
|
532 |
Code value = constants.get(i); |
|
533 |
newstatement.replace(hostExpression, value); |
|
534 |
} |
|
535 |
return newstatement; |
|
536 |
// return statement; |
|
537 |
} |
|
538 |
|
|
539 |
public Code resolveHostExpressions(Interpreter interpreter, Code statement) { |
|
540 |
try { |
|
541 |
return resolveHostExpressions0(interpreter, statement); |
|
542 |
} catch (Exception ex) { |
|
543 |
throw new RuntimeException("Can't resolve host expressions", ex); |
|
544 |
} |
|
545 |
} |
|
546 |
|
|
547 |
public Expression resolveHostExpressions(Expression expression, SymbolTable symbolTable) { |
|
548 |
try { |
|
549 |
Interpreter interpreter = this.createInterpreter(); |
|
550 |
interpreter.setSymbolTable(symbolTable); |
|
551 |
Code code0 = expression.getCode(); |
|
552 |
Code code1 = resolveHostExpressions0(interpreter, code0); |
|
553 |
if( code0 == code1 ) { |
|
554 |
// No hay "hosts expressions", devolvemos la expresion original. |
|
555 |
return expression; |
|
556 |
} |
|
557 |
DefaultExpression exp = new DefaultExpression(this); |
|
558 |
exp.setPhrase(code1.toValue().toString()); |
|
559 |
return exp; |
|
560 |
} catch (Exception ex) { |
|
561 |
throw new RuntimeException("Can't resolve host expressions", ex); |
|
562 |
} |
|
563 |
} |
|
564 |
|
|
495 | 565 |
} |
496 | 566 |
|
Also available in: Unified diff