Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_898 / extensions / extScripting / src / org / gvsig / scripting / Script.java @ 10513

History | View | Annotate | Download (2.63 KB)

1
package org.gvsig.scripting;
2

    
3
import java.io.File;
4
import java.io.FileReader;
5
import java.io.IOException;
6
import java.net.URL;
7
import java.util.Map;
8

    
9
import org.apache.bsf.BSFEngine;
10
import org.apache.bsf.BSFException;
11

    
12
import com.iver.andami.PluginServices;
13

    
14
public class Script {
15
        
16
    public static final String JAVASCRIPT = ScriptingExtension.JAVASCRIPT;
17
    public static final String BEANSHELL = ScriptingExtension.BEANSHELL;
18
    public static final String JYTHON = ScriptingExtension.JYTHON;
19
    public static final String GROOVY = ScriptingExtension.GROOVY;
20
    
21
        protected BSFEngine bsfEngine;
22
        protected String language;
23
        protected String fileName;
24
        protected Map params;
25
        protected String codeString;
26
        protected File file;        
27
        
28
        public Script(Map params) throws Throwable {
29
                this.params = params;
30
                if (!params.containsKey("fileName")) {
31
                        //TODO:
32
                        throw new Exception();
33
                }
34
                this.fileName = (String)params.get("fileName");
35
                if (!params.containsKey("language")) {
36
                        //TODO:
37
                        throw new Exception();
38
                }
39
                this.language = (String)params.get("language");
40
                this.params = params;
41
                this.initialize();                
42
        }
43
        
44
        private String readFile(File aFile) throws IOException {
45
                StringBuffer fileContents = new StringBuffer();
46
                FileReader fileReader = new FileReader(aFile);
47
                int c;
48
                while ((c = fileReader.read()) > -1) {
49
                        fileContents.append((char)c);
50
                }
51
                fileReader.close();
52
                return fileContents.toString();
53
        }
54
        
55
        private void initialize() throws BSFException, IOException {
56
                bsfEngine = ScriptingExtension.getBSFManager().loadScriptingEngine(this.language);
57
                this.file = new File(this.fileName);
58
                if (!this.file.exists()) {
59
                        //TODO:
60
                        throw new IOException();
61
                }
62
                this.codeString = this.readFile(this.file);
63
                
64
                
65
        }
66
        
67
        public void run() throws Throwable {
68
                ScriptingExtension.getBSFManager().declareBean("params",this.params,this.params.getClass());
69
                
70
        if (JYTHON.equals(this.language)){
71
                this.runStartupScripJython();
72
            this.bsfEngine.exec(this.fileName, -1, -1, this.codeString);
73
        }else{
74
            this.bsfEngine.eval(this.fileName, -1, -1, this.codeString);
75
        }
76
        }
77
        
78
        private void runStartupScripJython() throws IOException, BSFException {
79
                String startUpFileName = ScriptingExtension.getScriptingAppAccesor().getScriptsDirectory();
80
                startUpFileName = startUpFileName + File.separatorChar+ "jython"+ File.separatorChar+"startup.py";
81
                File startupFile = new File(startUpFileName);
82
                
83
                if (!startupFile.exists()) {
84
                        throw new IOException("Startup File "+startUpFileName+" not found");
85
                        //return;
86
                }
87
                String startupCode = this.readFile(startupFile);
88
                this.bsfEngine.exec(startUpFileName, -1, -1, startupCode);
89
        }
90
}