Statistics
| Revision:

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 / SimpleScript.java @ 44339

History | View | Annotate | Download (1.92 KB)

1
package org.gvsig.expressionevaluator.impl;
2

    
3
import java.net.URI;
4
import org.gvsig.expressionevaluator.Code;
5
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
6
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
7
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
8
import org.gvsig.tools.script.Script;
9

    
10
/**
11
 *
12
 * @author jjdelcerro
13
 */
14
public class SimpleScript implements Script {
15

    
16
    private final String name;
17
    private String source;
18
    private DefaultInterpreter interpreter;
19

    
20
    public SimpleScript(String name, String source) {
21
        this.name = name;
22
        this.setCode(source);
23
    }
24
    
25
    @Override
26
    public String getName() {
27
        return this.name;
28
    }
29

    
30
    @Override
31
    public String getCode() {
32
        return this.source;
33
    }
34

    
35
    @Override
36
    public String getTypeName() {
37
        return "COSA";
38
    }
39

    
40
    @Override
41
    public URI getURI() {
42
        return null;
43
    }
44

    
45
    @Override
46
    public void put(String name, Object value) {
47
        DefaultSymbolTable symbolTable = (DefaultSymbolTable) this.interpreter.getSymbolTable();
48
        symbolTable.setVar(name, value);
49
    }
50

    
51
    @Override
52
    public Object invokeFunction(String funcname, Object[] args) throws NoSuchMethodException {
53
        if( !this.interpreter.hasFunction(funcname) ) {
54
            throw new NoSuchMethodException(funcname);
55
        }
56
        try {
57
            return this.interpreter.call(funcname, args);
58
        } catch (Exception ex) {
59
            throw new ExpressionRuntimeException(this.interpreter.getCurrentCode(),"Error calling funcion '"+funcname+"'.", ex);
60
        }
61
    }
62

    
63
    @Override
64
    public final void setCode(String source) {
65
        this.source = source;
66
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
67
        this.interpreter = new DefaultInterpreter();
68
        Code code = manager.compile(source);
69
        this.interpreter.run(code);
70
    }
71
    
72
}