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