svn-gvsig-desktop / trunk / extensions / extScripting / src / org / gvsig / scripting / ScriptingExtension.java @ 9223
History | View | Annotate | Download (8.42 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.URLDecoder; |
7 |
import java.util.Hashtable; |
8 |
import java.util.Map; |
9 |
|
10 |
import org.apache.bsf.BSFEngine; |
11 |
import org.apache.bsf.BSFException; |
12 |
import org.apache.bsf.BSFManager; |
13 |
import org.gvsig.scripting.xul.XULInfoToolSupport; |
14 |
import org.gvsig.scripting.xul.XULScriptablePanel; |
15 |
|
16 |
import thinlet.script.ScriptableThinlet; |
17 |
|
18 |
import com.iver.andami.PluginServices; |
19 |
import com.iver.andami.plugins.Extension; |
20 |
import com.iver.andami.ui.mdiManager.IWindow; |
21 |
import com.iver.cit.gvsig.project.documents.view.info.gui.InfoToolViewer; |
22 |
|
23 |
public class ScriptingExtension extends Extension { |
24 |
private static BSFManager bsfManager; |
25 |
|
26 |
public static final String JAVASCRIPT = ScriptableThinlet.JAVASCRIPT; |
27 |
public static final String BEANSHELL = ScriptableThinlet.BEANSHELL; |
28 |
public static final String JYTHON = ScriptableThinlet.JYTHON; |
29 |
public static final String GROOVY = ScriptableThinlet.GROOVY; |
30 |
|
31 |
private static ScriptingAppAccesor scriptingAppAccesor = null; |
32 |
|
33 |
|
34 |
public static ScriptingAppAccesor getScriptingAppAccesor() { |
35 |
if (scriptingAppAccesor == null) { |
36 |
scriptingAppAccesor =new ScriptingAppAccesor();
|
37 |
} |
38 |
|
39 |
return scriptingAppAccesor;
|
40 |
} |
41 |
|
42 |
|
43 |
|
44 |
public static BSFManager getBSFManager() { |
45 |
if(bsfManager == null){ |
46 |
bsfManager = new BSFManager(){
|
47 |
public BSFEngine loadScriptingEngine(String lang) throws BSFException { |
48 |
// if its already loaded return that
|
49 |
BSFEngine eng = (BSFEngine) loadedEngines.get(lang); |
50 |
if (eng != null) { |
51 |
return eng;
|
52 |
} |
53 |
|
54 |
// is it a registered language?
|
55 |
String engineClassName = (String) registeredEngines.get(lang); |
56 |
if (engineClassName == null) { |
57 |
throw new BSFException(BSFException.REASON_UNKNOWN_LANGUAGE, |
58 |
"unsupported language: " + lang);
|
59 |
} |
60 |
|
61 |
// create the engine and initialize it. if anything goes wrong
|
62 |
try {
|
63 |
Class engineClass = (classLoader == null) |
64 |
? Class.forName(engineClassName)
|
65 |
: classLoader.loadClass(engineClassName); |
66 |
eng = (BSFEngine) engineClass.newInstance(); |
67 |
eng.initialize(this, lang, declaredBeans);
|
68 |
loadedEngines.put(lang, eng); |
69 |
pcs.addPropertyChangeListener(eng); |
70 |
return eng;
|
71 |
} |
72 |
catch (Throwable t) { |
73 |
throw new BSFException(BSFException.REASON_OTHER_ERROR, |
74 |
"unable to load language: " + lang, t);
|
75 |
} |
76 |
} |
77 |
}; |
78 |
} |
79 |
try {
|
80 |
bsfManager.declareBean("gvSIG", getScriptingAppAccesor(), ScriptingAppAccesor.class);
|
81 |
} catch (BSFException e) {
|
82 |
// TODO Auto-generated catch block
|
83 |
e.printStackTrace(); |
84 |
} |
85 |
|
86 |
return bsfManager;
|
87 |
} |
88 |
|
89 |
public void initialize() { |
90 |
XULScriptablePanel.setBSFManager( getBSFManager() ); |
91 |
InfoToolViewer.XULPanelClass = XULInfoToolSupport.class; |
92 |
} |
93 |
|
94 |
public void execute(String actionCommand) { |
95 |
ScriptParams params = new ScriptParams(actionCommand);
|
96 |
|
97 |
if (params.action() == null) { |
98 |
// FIXME: FALTA mostara error
|
99 |
return;
|
100 |
} |
101 |
if (params.action().toLowerCase().equals("show") ) { |
102 |
show(params.getParams()); |
103 |
} else if (params.action().toLowerCase().equals("run") ){ |
104 |
run(params.getParams()); |
105 |
} |
106 |
|
107 |
} |
108 |
|
109 |
public static void run(Map params) { |
110 |
Script script; |
111 |
try {
|
112 |
script = new Script(params);
|
113 |
script.run(); |
114 |
} catch (BSFException e) {
|
115 |
// TODO Auto-generated catch block
|
116 |
e.printStackTrace(); |
117 |
return;
|
118 |
} catch (IOException e) { |
119 |
// TODO Auto-generated catch block
|
120 |
e.printStackTrace(); |
121 |
return;
|
122 |
} catch (Throwable e) { |
123 |
// TODO Auto-generated catch block
|
124 |
e.printStackTrace(); |
125 |
} |
126 |
} |
127 |
|
128 |
private void runStartupScripJython() { |
129 |
BSFEngine bsfEngine; |
130 |
try {
|
131 |
bsfEngine = getBSFManager().loadScriptingEngine(JYTHON); |
132 |
String startUpFileName = ScriptingExtension.getScriptingAppAccesor().getScriptsDirectory();
|
133 |
startUpFileName = startUpFileName + File.separatorChar+ "jython"+ File.separatorChar+"startup.py"; |
134 |
File startupFile = new File(startUpFileName); |
135 |
|
136 |
if (!startupFile.exists()) {
|
137 |
//throw new IOException("Startup File "+startUpFileName+" not found");
|
138 |
return;
|
139 |
} |
140 |
String startupCode = readFile(startupFile);
|
141 |
bsfEngine.exec(startUpFileName, -1, -1, startupCode); |
142 |
} catch (BSFException e) {
|
143 |
// TODO Auto-generated catch block
|
144 |
e.printStackTrace(); |
145 |
} catch (IOException e) { |
146 |
// TODO Auto-generated catch block
|
147 |
e.printStackTrace(); |
148 |
} |
149 |
} |
150 |
|
151 |
private String readFile(File aFile) throws IOException { |
152 |
StringBuffer fileContents = new StringBuffer(); |
153 |
FileReader fileReader = new FileReader(aFile); |
154 |
int c;
|
155 |
while ((c = fileReader.read()) > -1) { |
156 |
fileContents.append((char)c);
|
157 |
} |
158 |
fileReader.close(); |
159 |
return fileContents.toString();
|
160 |
} |
161 |
|
162 |
public static Object show(Map params) { |
163 |
DefaultThinlet.setBSFManager(getBSFManager()); |
164 |
ScriptPanel panel; |
165 |
try {
|
166 |
panel = new ScriptPanel(params);
|
167 |
} catch (BSFException e) {
|
168 |
// TODO Auto-generated catch block
|
169 |
e.printStackTrace(); |
170 |
return null; |
171 |
} catch (IOException e) { |
172 |
// TODO Auto-generated catch block
|
173 |
e.printStackTrace(); |
174 |
return null; |
175 |
} |
176 |
IWindow v = PluginServices.getMDIManager().addWindow(panel); |
177 |
v.getWindowInfo().setWidth(v.getWindowInfo().getWidth()+1);
|
178 |
return panel;
|
179 |
} |
180 |
|
181 |
private ScriptingAppAccesor newAppAccesor() {
|
182 |
return new ScriptingAppAccesor(); |
183 |
} |
184 |
|
185 |
public boolean isEnabled() { |
186 |
return true; |
187 |
} |
188 |
|
189 |
public boolean isVisible() { |
190 |
return true; |
191 |
} |
192 |
|
193 |
public class ScriptParams { |
194 |
private String paramString; |
195 |
private String action = null; |
196 |
private Hashtable params = null; |
197 |
|
198 |
public ScriptParams(String paramString) { |
199 |
this.paramString = paramString.trim();
|
200 |
} |
201 |
|
202 |
public Map getParams() { |
203 |
if (this.params == null) { |
204 |
if (!this.parse()) { |
205 |
return null; |
206 |
} |
207 |
} |
208 |
return this.params; |
209 |
} |
210 |
|
211 |
public String action() { |
212 |
if (this.action == null) { |
213 |
if (!this.parse()) { |
214 |
return null; |
215 |
} |
216 |
} |
217 |
return this.action; |
218 |
} |
219 |
|
220 |
private boolean parse() { |
221 |
//transformar este formato: 'show(fileName='./pepe.xml',language='jython',top=11,left=10,width=300,height=300)'
|
222 |
int primerParentesis = this.paramString.indexOf("("); |
223 |
if (primerParentesis < 1) { |
224 |
// TODO Informar error
|
225 |
return false; |
226 |
} |
227 |
if (!this.paramString.endsWith(")")) { |
228 |
// TODO Informar error
|
229 |
return false; |
230 |
} |
231 |
this.action = this.paramString.substring(0,primerParentesis); |
232 |
|
233 |
// Dividimos en 'clave=valor'
|
234 |
|
235 |
this.params = new Hashtable(); |
236 |
if (primerParentesis+1 == this.paramString.length()-1) { |
237 |
return true; |
238 |
} |
239 |
|
240 |
String[] listaParametros = this.paramString.substring(primerParentesis+1,this.paramString.length()-1).trim().split(","); |
241 |
|
242 |
|
243 |
|
244 |
|
245 |
for (int i=0;i < listaParametros.length; i++) { |
246 |
// Dividimos en clave y valor
|
247 |
String[] parametroConjunto = listaParametros[i].trim().split("="); |
248 |
if (parametroConjunto.length != 2) { |
249 |
// TODO Informar error
|
250 |
return false; |
251 |
} |
252 |
String clave = parametroConjunto[0].trim(); |
253 |
String cadenaValor = parametroConjunto[1].trim(); |
254 |
|
255 |
//Intentamos identificar y transformar el valor en un Objeto Java
|
256 |
Object valor;
|
257 |
if (cadenaValor.startsWith("'")) { |
258 |
if (cadenaValor.endsWith("'")) { |
259 |
// Recortamos las comillas y descodificamos por si tiene algun caracter escapado
|
260 |
// FIXME arreglar el deprecated
|
261 |
valor = URLDecoder.decode(cadenaValor.substring(1,cadenaValor.length()-1)); |
262 |
} else {
|
263 |
// TODO Informar error
|
264 |
return false; |
265 |
} |
266 |
} else{
|
267 |
try {
|
268 |
// Si contiene el punto decimal intentamos con un Double
|
269 |
if (cadenaValor.indexOf('.') >= 0) { |
270 |
valor = new Double(cadenaValor); |
271 |
} else {
|
272 |
// Si no debe ser un Entero
|
273 |
valor = new Integer(cadenaValor); |
274 |
} |
275 |
|
276 |
} catch (NumberFormatException e) { |
277 |
// No se ha podido transformar
|
278 |
// TODO Informar error
|
279 |
return false; |
280 |
} |
281 |
|
282 |
} |
283 |
// A?adimos al Map
|
284 |
this.params.put(clave,valor);
|
285 |
|
286 |
|
287 |
} |
288 |
|
289 |
return true; |
290 |
|
291 |
} |
292 |
|
293 |
|
294 |
} |
295 |
} |