Revision 441
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/resources/org/gvsig/scripting/impl/InitializeEngine.py | ||
---|---|---|
1 |
|
|
2 |
|
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/DefaultScriptingManager.java | ||
---|---|---|
1 | 1 |
package org.gvsig.scripting.impl; |
2 | 2 |
|
3 | 3 |
import java.io.BufferedReader; |
4 |
import java.io.BufferedWriter; |
|
5 | 4 |
import java.io.File; |
6 | 5 |
import java.io.FileNotFoundException; |
7 | 6 |
import java.io.FileReader; |
8 |
import java.io.FileWriter; |
|
9 | 7 |
import java.io.IOException; |
10 |
import java.io.Writer; |
|
11 | 8 |
import java.util.ArrayList; |
12 | 9 |
import java.util.HashMap; |
13 | 10 |
import java.util.Iterator; |
... | ... | |
21 | 18 |
import javax.script.SimpleBindings; |
22 | 19 |
import javax.swing.ImageIcon; |
23 | 20 |
|
21 |
import org.apache.commons.io.FileUtils; |
|
24 | 22 |
import org.gvsig.scripting.ScriptingBaseScript; |
25 | 23 |
import org.gvsig.scripting.ScriptingDialog; |
26 | 24 |
import org.gvsig.scripting.ScriptingFolder; |
... | ... | |
61 | 59 |
private void createFolder(File f){ |
62 | 60 |
if( !f.exists() ) { |
63 | 61 |
try { |
64 |
if( f.mkdir() ) { |
|
65 |
LOG.info("Created scripting folder '"+f.getAbsolutePath()+"'"); |
|
66 |
} |
|
62 |
FileUtils.forceMkdir(f); |
|
63 |
LOG.info("Created scripting folder '"+f.getAbsolutePath()+"'"); |
|
67 | 64 |
} catch (Throwable e) { |
68 |
// Ignore it.
|
|
65 |
LOG.warn("Can't Create scripting folder '"+f.getAbsolutePath()+"'");
|
|
69 | 66 |
} |
70 | 67 |
} |
71 | 68 |
} |
... | ... | |
221 | 218 |
|
222 | 219 |
BufferedReader b = null; |
223 | 220 |
try { |
224 |
b = new BufferedReader(new FileReader(filename)); |
|
225 |
} catch (FileNotFoundException e) { |
|
226 |
LOG.warn("Can't open file '"+filename+"'.",e); |
|
227 |
} |
|
228 |
StringBuffer sb=new StringBuffer(); |
|
229 |
String str; |
|
230 |
try { |
|
231 |
while((str=b.readLine())!=null) { |
|
232 |
sb.append(str); |
|
233 |
sb.append('\n'); |
|
221 |
try { |
|
222 |
b = new BufferedReader(new FileReader(filename)); |
|
223 |
} catch (FileNotFoundException e) { |
|
224 |
LOG.warn("Can't open file '"+filename+"'.",e); |
|
234 | 225 |
} |
235 |
} catch (IOException e) { |
|
236 |
LOG.warn("Can't read file '"+filename+"'.",e); |
|
226 |
StringBuffer sb=new StringBuffer(); |
|
227 |
String str; |
|
228 |
try { |
|
229 |
while((str=b.readLine())!=null) { |
|
230 |
sb.append(str); |
|
231 |
sb.append('\n'); |
|
232 |
} |
|
233 |
} catch (IOException e) { |
|
234 |
LOG.warn("Can't read file '"+filename+"'.",e); |
|
235 |
} |
|
236 |
return sb.toString(); |
|
237 |
} finally { |
|
238 |
if( b!=null ) { |
|
239 |
try { |
|
240 |
b.close(); |
|
241 |
} catch (IOException e) { |
|
242 |
LOG.warn("Can't close file '"+filename+"'", e); |
|
243 |
} |
|
244 |
b = null; |
|
245 |
} |
|
237 | 246 |
} |
238 |
return sb.toString(); |
|
239 |
|
|
240 | 247 |
} |
241 | 248 |
|
249 |
public ScriptingScript createScript(ScriptingFolder folder, String id) { |
|
250 |
return this.createScript(folder, id, null); |
|
251 |
} |
|
252 |
|
|
253 |
private ScriptingScript createScript(ScriptingFolder folder, String id, String language) { |
|
254 |
DefaultScriptingScript script = new DefaultScriptingScript(folder, this, id); |
|
255 |
if( !script.getFile().exists() ) { |
|
256 |
script.create(folder, id, language); |
|
257 |
} else { |
|
258 |
script.load(folder, id); |
|
259 |
} |
|
260 |
return script; |
|
261 |
} |
|
262 |
|
|
242 | 263 |
public ScriptingDialog createDialog(ScriptingFolder folder, String id) { |
243 |
DefaultScriptingDialog sd = new DefaultScriptingDialog(this); |
|
244 |
sd.setId(id); |
|
245 |
File file = new File(((DefaultScriptingFolder)folder).getPath()+File.separator+id); |
|
246 |
try { |
|
247 |
file.createNewFile(); |
|
248 |
} catch (IOException e) { |
|
249 |
LOG.warn("Can't create file of the dialog in '"+file.toString()+"'.",e); |
|
264 |
return this.createDialog(folder, id, null); |
|
265 |
} |
|
266 |
|
|
267 |
private ScriptingDialog createDialog(ScriptingFolder folder, String id, String language) { |
|
268 |
DefaultScriptingDialog dialog = new DefaultScriptingDialog(folder, this, id); |
|
269 |
if( !dialog.getFile().exists() ) { |
|
270 |
dialog.create(folder, id, language); |
|
271 |
} else { |
|
272 |
dialog.load(folder, id); |
|
250 | 273 |
} |
251 |
|
|
252 |
// Eliminamos la extensión, para crear el '.dlg' |
|
253 |
String s[] = id.split("\\."); |
|
254 |
String newId; |
|
255 |
if (s.length>=2){ |
|
256 |
String extension ="." + s[s.length-1]; |
|
257 |
newId = id.substring(0,id.length()-extension.length()); |
|
258 |
}else{ |
|
259 |
newId = id; |
|
260 |
} |
|
261 |
File fileDlg = new File(((DefaultScriptingFolder)folder).getPath()+File.separator+newId+".dlg"); |
|
262 |
try { |
|
263 |
fileDlg.createNewFile(); |
|
264 |
} catch (IOException e) { |
|
265 |
LOG.warn("Can't create the dialog in '"+fileDlg.toString()+"'.",e); |
|
266 |
} |
|
267 |
|
|
268 |
// Escribimos en el fichero '.dlg' lo mínimo para que pueda ejecutarse |
|
269 |
Writer output = null; |
|
270 |
try { |
|
271 |
output = new BufferedWriter(new FileWriter(fileDlg)); |
|
272 |
output.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<panel/>"); |
|
273 |
output.close(); |
|
274 |
} catch (IOException e) { |
|
275 |
LOG.warn("Can't write xml code of the dialog to '"+fileDlg.toString()+"'.",e); |
|
276 |
} |
|
277 |
|
|
278 |
sd.load(folder, id); |
|
279 |
if( "python".equalsIgnoreCase(sd.getLangName()) ) { |
|
280 |
sd.setCode("\nfrom gvsig import *\n\ndef onload():\n #Remove this lines and add here your code\n\n print \"hola mundo\"\n pass\n\n"); |
|
281 |
} |
|
282 |
return sd; |
|
274 |
return dialog; |
|
283 | 275 |
} |
284 | 276 |
|
285 | 277 |
public ScriptingFolder createFolder(ScriptingFolder folder, String id) { |
286 |
DefaultScriptingFolder fd = new DefaultScriptingFolder(this); |
|
287 |
File dir = new File(((DefaultScriptingFolder)folder).getPath()+File.separator+id); |
|
288 |
dir.mkdir(); |
|
289 |
fd.load(folder,id); |
|
290 |
return fd; |
|
278 |
DefaultScriptingFolder unit = new DefaultScriptingFolder(folder, this); |
|
279 |
if( !unit.getFile().exists() ) { |
|
280 |
unit.create(folder, id); |
|
281 |
} else { |
|
282 |
unit.load(folder,id); |
|
283 |
} |
|
284 |
return folder; |
|
291 | 285 |
} |
292 | 286 |
|
293 | 287 |
public ScriptingProject createProject(ScriptingFolder folder, String id) { |
294 |
// TODO Auto-generated method stub
|
|
288 |
LOG.warn("Projects can't be implementeds");
|
|
295 | 289 |
return null; |
296 | 290 |
} |
297 | 291 |
|
298 |
public ScriptingScript createScript(ScriptingFolder folder, String id) { |
|
299 |
DefaultScriptingScript sc = new DefaultScriptingScript(this); |
|
300 |
sc.setId(id); |
|
301 |
File file = new File(((DefaultScriptingFolder)folder).getPath()+File.separator+id); |
|
302 |
try { |
|
303 |
file.createNewFile(); |
|
304 |
} catch (IOException e) { |
|
305 |
LOG.warn("Can't create file '"+file.toString()+"'.",e); |
|
306 |
return null; |
|
307 |
} |
|
308 |
sc.load(folder, id); |
|
309 |
|
|
310 |
if( "python".equalsIgnoreCase(sc.getLangName()) ) { |
|
311 |
sc.setCode("\nfrom gvsig import *\n\ndef main():\n #Remove this lines and add here your code\n\n print \"hola mundo\"\n pass\n\n"); |
|
312 |
} |
|
313 |
return sc; |
|
314 |
} |
|
315 |
|
|
316 | 292 |
public ScriptingBaseScript getScript(File file) { |
317 | 293 |
ScriptingBaseScript script = (ScriptingBaseScript) this.getUnit(file); |
318 |
|
|
319 | 294 |
if (script == null){ |
320 | 295 |
throw new RuntimeException(file.getPath()); |
321 | 296 |
} |
... | ... | |
378 | 353 |
return unit; |
379 | 354 |
} |
380 | 355 |
List<ScriptingUnit> units = folder.getUnits(); |
381 |
Iterator it = units.iterator(); |
|
356 |
Iterator<ScriptingUnit> it = units.iterator();
|
|
382 | 357 |
while( it.hasNext() ) { |
383 |
unit = (ScriptingUnit) it.next();
|
|
358 |
unit = it.next(); |
|
384 | 359 |
if( unit instanceof ScriptingFolder ) { |
385 | 360 |
unit = findScript((ScriptingFolder) unit, name); |
386 | 361 |
if( unit!= null ) { |
... | ... | |
456 | 431 |
} |
457 | 432 |
|
458 | 433 |
public ScriptingUnit createUnit(String unitType, ScriptingFolder folder, String id) { |
434 |
return createUnit(unitType, folder, id, null); |
|
435 |
} |
|
436 |
|
|
437 |
public ScriptingUnit createUnit(String unitType, ScriptingFolder folder, String id, String language) { |
|
459 | 438 |
if(unitType.equals(UNIT_SCRIPT)){ |
460 |
return this.createScript(folder, id); |
|
439 |
return this.createScript(folder, id, language);
|
|
461 | 440 |
} |
462 | 441 |
if(unitType.equals(UNIT_DIALOG)){ |
463 |
return this.createDialog(folder, id); |
|
442 |
return this.createDialog(folder, id, language);
|
|
464 | 443 |
} |
465 | 444 |
if(unitType.equals(UNIT_FOLDER)){ |
466 | 445 |
return this.createFolder(folder, id); |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/AbstractUnit.java | ||
---|---|---|
5 | 5 |
import java.util.List; |
6 | 6 |
|
7 | 7 |
import org.ini4j.Ini; |
8 |
|
|
8 |
import org.slf4j.Logger; |
|
9 |
import org.slf4j.LoggerFactory; |
|
10 |
import org.apache.commons.io.FilenameUtils; |
|
9 | 11 |
import org.gvsig.scripting.ScriptingFolder; |
10 | 12 |
import org.gvsig.scripting.ScriptingManager; |
11 | 13 |
import org.gvsig.scripting.ScriptingScript; |
... | ... | |
13 | 15 |
|
14 | 16 |
public abstract class AbstractUnit implements ScriptingUnit, Unit{ |
15 | 17 |
|
18 |
private static final Logger logger = LoggerFactory.getLogger(AbstractUnit.class); |
|
19 |
|
|
16 | 20 |
protected DefaultScriptingManager manager; |
17 | 21 |
|
18 | 22 |
protected String id; |
... | ... | |
20 | 24 |
protected String description; |
21 | 25 |
protected String createdBy; |
22 | 26 |
protected String version; |
23 |
protected ScriptingFolder parent; |
|
27 |
protected ScriptingFolder parent; |
|
28 |
protected String typename; |
|
24 | 29 |
|
25 |
public AbstractUnit(ScriptingManager manager){ |
|
30 |
public AbstractUnit(ScriptingFolder parent, String typename, ScriptingManager manager, String id){ |
|
31 |
this.parent = parent; |
|
26 | 32 |
this.manager = (DefaultScriptingManager) manager; |
33 |
this.typename = typename; |
|
34 |
this.id = id; |
|
27 | 35 |
} |
28 | 36 |
|
37 |
public String getTypeName() { |
|
38 |
return typename; |
|
39 |
} |
|
40 |
|
|
29 | 41 |
/* (non-Javadoc) |
30 | 42 |
* @see org.gvsig.scripting.impl.Unit#load(org.gvsig.scripting.ScriptingFolder, java.lang.String) |
31 | 43 |
*/ |
32 | 44 |
public abstract void load(ScriptingFolder folder, String id); |
33 | 45 |
|
34 |
@Override |
|
35 | 46 |
public String toString(){ |
47 |
if( this.getName() == null ) { |
|
48 |
return "("+this.getClass().getSimpleName()+")"; |
|
49 |
} |
|
36 | 50 |
return this.getName(); |
37 | 51 |
} |
38 | 52 |
|
... | ... | |
41 | 55 |
} |
42 | 56 |
|
43 | 57 |
protected File getFileResource(String extension){ |
44 |
return new File(this.getParent().getPathFile(),this.id + extension);
|
|
58 |
return new File(this.getParent().getFile(),this.id + extension).getAbsoluteFile();
|
|
45 | 59 |
} |
46 | 60 |
|
47 | 61 |
/* (non-Javadoc) |
... | ... | |
98 | 112 |
if(this instanceof ScriptingFolder){ |
99 | 113 |
this.id = id; |
100 | 114 |
} else { |
101 |
String s[] = id.split("\\."); |
|
102 |
if (s.length<2){ |
|
103 |
this.id = id; |
|
104 |
} else{ |
|
105 |
String extension ="." + s[s.length-1]; |
|
106 |
this.id = id.substring(0,id.length()-extension.length()); |
|
107 |
} |
|
115 |
this.id = FilenameUtils.getBaseName(id); |
|
108 | 116 |
} |
109 | 117 |
} |
110 | 118 |
|
... | ... | |
147 | 155 |
} |
148 | 156 |
if(fileName.equals(oldId)){ |
149 | 157 |
//renombramos fichero |
150 |
File f = new File(this.getParent().getPath()+File.separator+fileName+extension);
|
|
151 |
File fDest = new File(folder.getPath()+File.separator+id+extension);
|
|
158 |
File f = new File(this.getParent().getFile(),fileName+extension);
|
|
159 |
File fDest = new File(folder.getFile(),id+extension);
|
|
152 | 160 |
if(this instanceof ScriptingScript){ |
153 | 161 |
String code = DefaultScriptingManager.getStringFromFile(f.getPath()); |
154 | 162 |
((ScriptingScript)this).setCode(code); |
155 | 163 |
} |
156 | 164 |
f.renameTo(fDest); |
157 | 165 |
|
158 |
File fInf = new File(this.getParent().getPath()+File.separator+fileName+".inf");
|
|
166 |
File fInf = new File(this.getParent().getFile(),fileName+".inf");
|
|
159 | 167 |
if(fInf.exists()){ |
160 |
File fInfDest = new File(folder.getPath()+File.separator+id+".inf");
|
|
168 |
File fInfDest = new File(folder.getFile(),id+".inf");
|
|
161 | 169 |
fInf.renameTo(fInfDest); |
162 | 170 |
} |
163 |
File fDialog = new File(this.getParent().getPath()+File.separator+fileName+".dlg");
|
|
171 |
File fDialog = new File(this.getParent().getFile(),fileName+".dlg");
|
|
164 | 172 |
if(fDialog.exists()){ |
165 |
File fDialogDest = new File(folder.getPath()+File.separator+id+".dlg");
|
|
173 |
File fDialogDest = new File(folder.getFile(),id+".dlg");
|
|
166 | 174 |
fDialog.renameTo(fDialogDest); |
167 | 175 |
} |
168 | 176 |
} |
... | ... | |
185 | 193 |
return moveFiles(this.getParent(), newId); |
186 | 194 |
} |
187 | 195 |
|
196 |
private String toStringNotNull(String s) { |
|
197 |
if( s==null ) { |
|
198 |
return ""; |
|
199 |
} |
|
200 |
return s; |
|
201 |
} |
|
202 |
|
|
188 | 203 |
protected void save(Ini prefs){ |
204 |
prefs.put("Unit", "type",toStringNotNull(this.getTypeName())); |
|
189 | 205 |
prefs.put("Unit", "name",this.getName()); |
190 |
if (this.getDescription() == null){ |
|
191 |
prefs.put("Unit", "description",""); |
|
192 |
}else { |
|
193 |
prefs.put("Unit", "description",this.getDescription()); |
|
194 |
} |
|
206 |
prefs.put("Unit", "description",toStringNotNull(this.getDescription())); |
|
207 |
prefs.put("Unit", "createdBy",toStringNotNull(this.getCreatedBy())); |
|
208 |
prefs.put("Unit", "version",toStringNotNull(this.getVersion())); |
|
195 | 209 |
|
196 |
if (this.getCreatedBy() == null){ |
|
197 |
prefs.put("Unit", "createdBy",""); |
|
198 |
}else { |
|
199 |
prefs.put("Unit", "createdBy",this.getCreatedBy()); |
|
200 |
} |
|
201 |
|
|
202 |
if (this.getVersion() == null){ |
|
203 |
prefs.put("Unit", "version",""); |
|
204 |
}else { |
|
205 |
prefs.put("Unit", "version",this.getVersion()); |
|
206 |
} |
|
207 |
|
|
208 | 210 |
try { |
209 | 211 |
prefs.store(); |
210 | 212 |
} catch (IOException e) { |
211 |
// TODO Auto-generated catch block |
|
212 |
e.printStackTrace(); |
|
213 |
File f = prefs.getFile(); |
|
214 |
String fname = (f==null)? "(null)":f.getAbsolutePath(); |
|
215 |
logger.warn("Can't save inf file '"+fname+"'."); |
|
213 | 216 |
} |
214 | 217 |
|
215 | 218 |
} |
216 | 219 |
|
217 | 220 |
protected void loadInf(Ini prefs){ |
218 |
|
|
219 |
this.setName((String)getInfValue(prefs,"Unit","name", this.getName()));
|
|
220 |
String description = (String) getInfValue(prefs,"Unit","description", null);
|
|
221 |
if (description != null && description.length() == 0){
|
|
222 |
description = null;
|
|
221 |
String typename = getInfString(prefs,"Unit","type", this.getTypeName()); |
|
222 |
if( !this.getTypeName().equalsIgnoreCase(typename) ) {
|
|
223 |
File f = prefs.getFile();
|
|
224 |
String fname = (f==null)? "(null)":f.getAbsolutePath();
|
|
225 |
logger.warn("inconsistent type in inf file '"+fname+"'. Curent type '"+this.getTypeName()+"', type from inf file '"+typename+"'.");
|
|
223 | 226 |
} |
224 |
this.setDescription(description); |
|
225 |
|
|
226 |
String createdBy = (String) getInfValue(prefs,"Unit","createdBy", null); |
|
227 |
if (createdBy != null && createdBy.length() == 0){ |
|
228 |
createdBy = null; |
|
229 |
} |
|
230 |
this.setCreatedBy(createdBy); |
|
231 |
|
|
232 |
String version = (String) getInfValue(prefs,"Unit","version", null); |
|
233 |
if (version != null && version.length() == 0){ |
|
234 |
version = null; |
|
235 |
} |
|
236 |
this.setVersion(version); |
|
227 |
this.setName(getInfString(prefs,"Unit","name", this.getName())); |
|
228 |
this.setDescription(getInfString(prefs,"Unit","description", null)); |
|
229 |
this.setCreatedBy(getInfString(prefs,"Unit","createdBy", null)); |
|
230 |
this.setVersion(getInfString(prefs,"Unit","version", null)); |
|
237 | 231 |
|
238 | 232 |
} |
239 | 233 |
|
240 | 234 |
protected Object getInfValue(Ini prefs, String section, String option, Object defaultValue){ |
241 | 235 |
Object r = prefs.get(section,option); |
242 |
if (r==null) |
|
236 |
if (r==null) {
|
|
243 | 237 |
return defaultValue; |
244 |
else
|
|
238 |
} else {
|
|
245 | 239 |
return r; |
240 |
} |
|
246 | 241 |
} |
242 |
|
|
243 |
private String getInfString(Ini prefs, String section, String option, Object defaultValue) { |
|
244 |
String s = (String) getInfValue(prefs, section, option, defaultValue); |
|
245 |
if( s!=null && s.trim().length()<1) { |
|
246 |
return null; |
|
247 |
} |
|
248 |
return s; |
|
249 |
} |
|
250 |
|
|
251 |
protected void console_println(String s) { |
|
252 |
// When running from the composer messages of stdout shows in the console tab. |
|
253 |
logger.info(s); |
|
254 |
System.out.println(s); |
|
255 |
} |
|
256 |
|
|
257 |
public void create(ScriptingFolder folder, String id) { |
|
258 |
this.setParent(folder); |
|
259 |
this.setId(id); |
|
260 |
|
|
261 |
File file = new File(folder.getFile(),id +".inf"); |
|
262 |
try { |
|
263 |
file.createNewFile(); |
|
264 |
} catch (IOException e) { |
|
265 |
logger.warn("Can't create inf file in '"+file.getAbsolutePath()+"'.",e); |
|
266 |
} |
|
267 |
} |
|
268 |
|
|
269 |
public File getFile() { |
|
270 |
if( this.getId()==null ) { |
|
271 |
return null; |
|
272 |
} |
|
273 |
if( this.getParent()==null ) { |
|
274 |
return null; |
|
275 |
} |
|
276 |
if( this.getParent().getFile()==null ) { |
|
277 |
return null; |
|
278 |
} |
|
279 |
return new File(getParent().getFile(),this.getId()+".inf"); |
|
280 |
} |
|
247 | 281 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/DefaultScriptingFolder.java | ||
---|---|---|
1 | 1 |
package org.gvsig.scripting.impl; |
2 | 2 |
|
3 | 3 |
import java.io.File; |
4 |
import java.io.FilenameFilter; |
|
5 |
import java.io.IOException; |
|
4 | 6 |
import java.net.URL; |
5 | 7 |
import java.util.ArrayList; |
6 | 8 |
import java.util.Iterator; |
... | ... | |
9 | 11 |
import java.util.Arrays; |
10 | 12 |
import java.util.Comparator; |
11 | 13 |
|
14 |
import org.apache.commons.io.FileUtils; |
|
15 |
import org.apache.commons.io.FilenameUtils; |
|
12 | 16 |
import org.gvsig.scripting.ScriptingFolder; |
13 | 17 |
import org.gvsig.scripting.ScriptingManager; |
14 | 18 |
import org.gvsig.scripting.ScriptingScript; |
15 | 19 |
import org.gvsig.scripting.ScriptingUnit; |
20 |
import org.ini4j.Ini; |
|
21 |
import org.python.core.util.FileUtil; |
|
22 |
import org.slf4j.Logger; |
|
23 |
import org.slf4j.LoggerFactory; |
|
16 | 24 |
|
17 | 25 |
|
18 | 26 |
public class DefaultScriptingFolder extends AbstractUnit implements ScriptingFolder{ |
19 | 27 |
|
28 |
private static final Logger logger = LoggerFactory.getLogger(DefaultScriptingFolder.class); |
|
20 | 29 |
protected File folder; |
21 | 30 |
|
22 |
public DefaultScriptingFolder(ScriptingManager manager) { |
|
23 |
super(manager); |
|
24 |
this.folder = null; |
|
31 |
public DefaultScriptingFolder(ScriptingFolder parent, ScriptingManager manager) { |
|
32 |
this(parent, manager,(File)null); |
|
25 | 33 |
} |
26 | 34 |
|
27 |
public DefaultScriptingFolder(ScriptingManager manager, File folder) { |
|
28 |
super(manager);
|
|
35 |
public DefaultScriptingFolder(ScriptingFolder parent, ScriptingManager manager, File folder) {
|
|
36 |
super(parent, ScriptingManager.UNIT_FOLDER, manager, null);
|
|
29 | 37 |
this.folder = folder; |
30 |
this.setId(folder.getName()); |
|
38 |
if( folder != null ) { |
|
39 |
this.setId(folder.getName()); |
|
40 |
} |
|
31 | 41 |
} |
32 | 42 |
|
33 |
public DefaultScriptingFolder(ScriptingManager manager, URL folder) { |
|
34 |
super(manager);
|
|
35 |
if(!folder.getProtocol().equals("file")){ |
|
43 |
public DefaultScriptingFolder(ScriptingFolder parent, ScriptingManager manager, URL folder) {
|
|
44 |
super(parent, ScriptingManager.UNIT_FOLDER, manager, null);
|
|
45 |
if(!folder.getProtocol().equalsIgnoreCase("file")){
|
|
36 | 46 |
throw new RuntimeException("Protocol URL not supported"); |
37 | 47 |
} |
38 | 48 |
File f = new File(folder.getPath()); |
49 |
this.setId(f.getName()); |
|
39 | 50 |
this.folder = f; |
40 |
this.setId(f.getName()); |
|
41 | 51 |
} |
42 | 52 |
|
43 |
public String getPath(){ |
|
44 |
return this.folder.getPath(); |
|
53 |
public File getFile(){ |
|
54 |
return this.folder; |
|
55 |
} |
|
56 |
|
|
57 |
public ScriptingFolder getParent() { |
|
58 |
ScriptingFolder parent = super.getParent(); |
|
59 |
if( parent != null ) { |
|
60 |
return parent; |
|
61 |
} |
|
62 |
return new DefaultScriptingFolder(null, manager, this.folder.getParentFile()); |
|
45 | 63 |
} |
46 | 64 |
|
47 |
public File getPathFile(){ |
|
48 |
return this.folder; |
|
49 |
} |
|
50 |
|
|
65 |
private String getTypenameFromInf(File file) { |
|
66 |
Ini inf = null; |
|
67 |
try { |
|
68 |
inf = new Ini(file); |
|
69 |
} catch (Exception e) { |
|
70 |
logger.warn("Can't load 'inf' file '"+file.getAbsolutePath()+"'.",e); |
|
71 |
} |
|
72 |
String typename = inf.get("Unit", "type"); |
|
73 |
if( typename == null ) { |
|
74 |
typename = ScriptingManager.UNIT_SCRIPT; |
|
75 |
} |
|
76 |
return typename; |
|
77 |
} |
|
78 |
|
|
51 | 79 |
public Unit getUnit(File afile) { |
52 | 80 |
Unit unit=null; |
81 |
|
|
53 | 82 |
File file = null; |
54 | 83 |
if( afile.isAbsolute() ) { |
55 |
file = new File( /*this.getPath() + */afile.getPath());
|
|
84 |
file = afile.getAbsoluteFile();
|
|
56 | 85 |
} else { |
57 |
file = new File( this.getPath()+File.separator+afile.getPath());
|
|
86 |
file = new File( this.getFile(),afile.getPath());
|
|
58 | 87 |
} |
59 | 88 |
|
60 |
|
|
61 |
|
|
89 |
String id = FilenameUtils.getBaseName(afile.getAbsolutePath()); |
|
90 |
ScriptingFolder parent = null; |
|
91 |
if( FilenameUtils.equalsNormalizedOnSystem(this.getFile().getAbsolutePath(), file.getParentFile().getAbsolutePath() ) ) { |
|
92 |
parent = this; |
|
93 |
} else { |
|
94 |
parent = new DefaultScriptingFolder(null, manager, file.getParentFile()); |
|
95 |
} |
|
96 |
|
|
62 | 97 |
if (file.isDirectory()){ |
63 |
File inf = new File(file.getAbsolutePath()+File.separator+file.getName()+".inf"); |
|
64 |
if(inf.isFile()){ |
|
65 |
unit = new DefaultScriptingProject(this.manager); |
|
66 |
}else{ |
|
67 |
unit = new DefaultScriptingFolder(this.manager); |
|
98 |
unit = (Unit) this.manager.createUnit(ScriptingManager.UNIT_FOLDER, parent, id); |
|
99 |
|
|
100 |
} else if( file.isFile() ) { |
|
101 |
if( !file.getName().endsWith(".inf") ) { |
|
102 |
file = new File( file.getParentFile(), |
|
103 |
FilenameUtils.getBaseName(file.getName())+".inf"); |
|
68 | 104 |
} |
69 |
} else if(file.getName().endsWith(".dlg") && file.isFile() ) { |
|
70 |
unit = new DefaultScriptingDialog(this.manager); |
|
71 |
} else{ |
|
72 |
Map<String, String> extensions = |
|
73 |
this.manager.getSupportedLanguagesByExtension(); |
|
74 |
Iterator<String> iterator = extensions.keySet().iterator(); |
|
75 |
|
|
76 |
while (iterator.hasNext()){ |
|
77 |
String extension = iterator.next(); |
|
78 |
if (file.getName().toLowerCase().endsWith(extension)){ |
|
79 |
String fname = file.getName(); |
|
80 |
String id = fname.substring(0,fname.length()-extension.length()); |
|
81 |
File dlg = new File(file.getParentFile().getAbsolutePath()+File.separator+id+".dlg"); |
|
82 |
if (dlg.isFile()){ |
|
83 |
unit = new DefaultScriptingDialog(this.manager); |
|
84 |
}else{ |
|
85 |
unit = new DefaultScriptingScript(this.manager); |
|
86 |
} |
|
87 |
} |
|
105 |
if( !file.exists() ) { |
|
106 |
logger.warn("Can't get Unit, file '"+file.getAbsolutePath()+"' not exists."); |
|
107 |
return null; |
|
88 | 108 |
} |
109 |
unit = (Unit) this.manager.createUnit(getTypenameFromInf(file), parent, id); |
|
89 | 110 |
} |
90 |
|
|
91 |
if(unit != null){ |
|
92 |
unit.load(new DefaultScriptingFolder(this.manager,file.getParentFile()), file.getName()); |
|
93 |
} |
|
94 | 111 |
return unit; |
95 | 112 |
} |
96 | 113 |
|
97 | 114 |
public List<ScriptingUnit> getUnits() { |
98 | 115 |
// Listar los ficheros del directorio y pasarlo a un vector para ordenarlo |
99 | 116 |
List<ScriptingUnit> ol = new ArrayList<ScriptingUnit>(); |
100 |
File[] files = this.folder.listFiles(); |
|
117 |
File[] files = this.folder.listFiles(new FilenameFilter() { |
|
118 |
public boolean accept(File arg0, String arg1) { |
|
119 |
File f = new File(arg0,arg1); |
|
120 |
if( !f.canRead() ) { |
|
121 |
return false; |
|
122 |
} |
|
123 |
if( f.isHidden() ) { |
|
124 |
return false; |
|
125 |
} |
|
126 |
if( f.isDirectory() ) { |
|
127 |
return true; |
|
128 |
} |
|
129 |
String ext = FilenameUtils.getExtension(f.getAbsolutePath()); |
|
130 |
return ext.equalsIgnoreCase("inf"); |
|
131 |
} |
|
132 |
}); |
|
101 | 133 |
if (files != null){ |
102 |
Arrays.sort( files, new Comparator<File>() |
|
103 |
{ |
|
104 |
public int compare(File f1, File f2) { |
|
105 |
if (f1.isDirectory() && !f2.isDirectory()) { |
|
106 |
return -1; |
|
107 |
} else if (!f1.isDirectory() && f2.isDirectory()) { |
|
108 |
return 1; |
|
109 |
} else { |
|
110 |
return f1.getName().toLowerCase().compareTo(f2.getName().toLowerCase()); |
|
111 |
} |
|
112 |
|
|
134 |
Arrays.sort( files, new Comparator<File>() { |
|
135 |
public int compare(File f1, File f2) { |
|
136 |
if (f1.isDirectory() && !f2.isDirectory()) { |
|
137 |
return -1; |
|
138 |
} else if (!f1.isDirectory() && f2.isDirectory()) { |
|
139 |
return 1; |
|
140 |
} else { |
|
141 |
return f1.getName().toLowerCase().compareTo(f2.getName().toLowerCase()); |
|
113 | 142 |
} |
114 |
}); |
|
143 |
} |
|
144 |
}); |
|
115 | 145 |
for (int i = 0; i < files.length; i++){ |
116 |
if(files[i].canRead() && !files[i].isHidden()){ |
|
117 |
ScriptingUnit unit = this.getUnit(files[i]); |
|
118 |
if (unit!=null){ |
|
119 |
ol.add(unit); |
|
120 |
} |
|
146 |
File f = files[i]; |
|
147 |
ScriptingUnit unit = null; |
|
148 |
try { |
|
149 |
unit = this.getUnit(f); |
|
150 |
} catch(Exception ex) { |
|
151 |
logger.warn("Can't create unit from file '"+f.getAbsolutePath()+"'."); |
|
121 | 152 |
} |
153 |
if (unit!=null){ |
|
154 |
ol.add(unit); |
|
155 |
} |
|
122 | 156 |
} |
123 | 157 |
} |
124 | 158 |
return ol; |
... | ... | |
129 | 163 |
} |
130 | 164 |
|
131 | 165 |
public void remove(ScriptingUnit unit) { |
132 |
String fileName = null; |
|
133 |
String s[] = null; |
|
134 |
String extension = null; |
|
135 |
List<ScriptingUnit> units = this.getUnits(); |
|
136 |
for(int i=0; i<units.size();i++){ |
|
137 |
fileName = (units.get(i)).getId(); |
|
138 |
s = fileName.split("\\."); |
|
139 |
extension = ""; |
|
140 |
if (s.length>1){ |
|
141 |
extension ="." + s[s.length-1]; |
|
142 |
fileName = fileName.substring(0,fileName.length()-extension.length()); |
|
143 |
} |
|
144 |
if(extension.equals("") && unit instanceof ScriptingScript){ |
|
145 |
extension = manager.getExtensionByLanguage(((ScriptingScript)unit).getLangName()); |
|
146 |
} |
|
147 |
if(fileName.equals(unit.getId())){ |
|
148 |
//borramos fichero |
|
149 |
File f = new File(this.getPath()+File.separator+fileName+extension); |
|
150 |
f.delete(); |
|
151 |
f = new File(this.getPath()+File.separator+fileName+".inf"); |
|
152 |
if(f.exists()){ |
|
153 |
f.delete(); |
|
154 |
} |
|
155 |
f = new File(this.getPath()+File.separator+fileName+".dlg"); |
|
156 |
if(f.exists()){ |
|
157 |
f.delete(); |
|
158 |
} |
|
159 |
} |
|
160 |
} |
|
166 |
unit.remove(); |
|
161 | 167 |
} |
162 | 168 |
|
163 | 169 |
@Override |
... | ... | |
165 | 171 |
DefaultScriptingFolder parent = (DefaultScriptingFolder) folder; |
166 | 172 |
this.setParent(folder); |
167 | 173 |
this.setId(id); |
168 |
this.folder =new File(parent.getPath()+File.separator+id);
|
|
174 |
this.folder = new File(parent.getFile(),id);
|
|
169 | 175 |
} |
170 | 176 |
|
171 | 177 |
public String[] getIconNames() { |
172 | 178 |
return new String[]{"folder", "folder-drag-accept"}; |
173 | 179 |
} |
174 | 180 |
|
181 |
public boolean remove() { |
|
182 |
boolean r = true; |
|
183 |
File folder = this.getParent().getFile(); |
|
184 |
File f = null; |
|
185 |
try { |
|
186 |
f = new File(folder,this.getId()); |
|
187 |
FileUtils.forceDelete(f); |
|
188 |
} catch (IOException e) { |
|
189 |
logger.warn("Can't remove project '"+f.getAbsolutePath()+"'.",e); |
|
190 |
r = false; |
|
191 |
} |
|
192 |
return r; |
|
193 |
} |
|
194 |
|
|
195 |
public void create(ScriptingFolder folder, String id, String language) { |
|
196 |
File f = new File(folder.getFile(),id); |
|
197 |
f.mkdir(); |
|
198 |
this.load(folder, id); |
|
199 |
} |
|
175 | 200 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/DefaultScriptingDialog.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.awt.event.ActionEvent; |
4 | 4 |
import java.awt.event.ActionListener; |
5 |
import java.io.BufferedWriter; |
|
5 | 6 |
import java.io.File; |
7 |
import java.io.FileWriter; |
|
8 |
import java.io.IOException; |
|
9 |
import java.io.Writer; |
|
10 |
import java.util.Map; |
|
6 | 11 |
|
12 |
import org.apache.commons.io.FileUtils; |
|
13 |
import org.apache.commons.io.FilenameUtils; |
|
7 | 14 |
import org.gvsig.scripting.ExecuteErrorException; |
8 | 15 |
import org.gvsig.scripting.ScriptingDialog; |
16 |
import org.gvsig.scripting.ScriptingFolder; |
|
9 | 17 |
import org.gvsig.scripting.ScriptingManager; |
10 | 18 |
import org.gvsig.scripting.swing.api.JThinlet; |
11 | 19 |
import org.gvsig.scripting.swing.api.ScriptingSwingLocator; |
12 | 20 |
import org.gvsig.scripting.swing.api.ScriptingUIManager; |
13 | 21 |
import org.ini4j.Ini; |
22 |
import org.slf4j.Logger; |
|
23 |
import org.slf4j.LoggerFactory; |
|
14 | 24 |
|
15 | 25 |
public class DefaultScriptingDialog extends DefaultScriptingScript implements ScriptingDialog, ActionListener{ |
16 | 26 |
|
27 |
private static final Logger logger = LoggerFactory.getLogger(DefaultScriptingDialog.class); |
|
17 | 28 |
private int showMode; |
18 | 29 |
|
19 |
public DefaultScriptingDialog(ScriptingManager manager) {
|
|
20 |
super(manager);
|
|
30 |
public DefaultScriptingDialog(ScriptingFolder parent, ScriptingManager manager, String id) {
|
|
31 |
super(parent, ScriptingManager.UNIT_DIALOG,manager, id);
|
|
21 | 32 |
this.setMainName("onload"); |
22 | 33 |
this.showMode=MODE_WINDOW; |
23 | 34 |
} |
... | ... | |
80 | 91 |
} |
81 | 92 |
|
82 | 93 |
public String[] getIconNames() { |
83 |
return new String[]{"scripting_dialog", null}; |
|
94 |
return new String[]{ |
|
95 |
"scripting_dialog", |
|
96 |
"scripting_dialog_open" |
|
97 |
}; |
|
84 | 98 |
} |
85 | 99 |
|
86 | 100 |
|
... | ... | |
105 | 119 |
this.showMode=mode; |
106 | 120 |
} |
107 | 121 |
|
122 |
public boolean remove() { |
|
123 |
boolean r = super.remove(); |
|
124 |
File folder = this.getParent().getFile(); |
|
125 |
File f = null; |
|
126 |
try { |
|
127 |
f = new File(folder,this.getId()+".dlg"); |
|
128 |
FileUtils.forceDelete(f); |
|
129 |
} catch (IOException e) { |
|
130 |
logger.warn("Can't remove dialog file '"+f.getAbsolutePath()+"'.",e); |
|
131 |
r = false; |
|
132 |
} |
|
133 |
return r; |
|
134 |
} |
|
135 |
|
|
136 |
public void create(ScriptingFolder folder, String id, String language) { |
|
137 |
super.create(folder, id, language); |
|
138 |
|
|
139 |
File fileDlg = this.getResource(this.getId()+".dlg"); |
|
140 |
try { |
|
141 |
fileDlg.createNewFile(); |
|
142 |
} catch (IOException e) { |
|
143 |
logger.warn("Can't create the dialog in '"+fileDlg.getAbsolutePath()+"'.",e); |
|
144 |
} |
|
145 |
|
|
146 |
// Escribimos en el fichero '.dlg' lo mi?nimo para que pueda ejecutarse |
|
147 |
Writer output = null; |
|
148 |
try { |
|
149 |
output = new BufferedWriter(new FileWriter(fileDlg)); |
|
150 |
output.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<panel/>"); |
|
151 |
output.close(); |
|
152 |
} catch (IOException e) { |
|
153 |
logger.warn("Can't write xml code of the dialog to '"+fileDlg.getAbsolutePath()+"'.",e); |
|
154 |
} |
|
155 |
|
|
156 |
this.save(); |
|
157 |
} |
|
108 | 158 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/AbstractScript.java | ||
---|---|---|
1 | 1 |
package org.gvsig.scripting.impl; |
2 | 2 |
|
3 | 3 |
import org.gvsig.scripting.ScriptingBaseScript; |
4 |
import org.gvsig.scripting.ScriptingFolder; |
|
4 | 5 |
import org.gvsig.scripting.ScriptingManager; |
5 | 6 |
import org.ini4j.Ini; |
6 | 7 |
|
7 | 8 |
public abstract class AbstractScript extends AbstractUnit implements ScriptingBaseScript{ |
8 | 9 |
|
9 |
protected String path; |
|
10 |
|
|
11 |
|
|
12 |
public AbstractScript(ScriptingManager manager){ |
|
13 |
super(manager); |
|
10 |
public AbstractScript(ScriptingFolder parent, String typename, ScriptingManager manager, String id){ |
|
11 |
super(parent, typename, manager, id); |
|
14 | 12 |
} |
15 |
|
|
16 |
public String getPath() { |
|
17 |
return this.path; |
|
18 |
} |
|
19 | 13 |
|
20 |
public void setPath(String path) { |
|
21 |
this.path = path; |
|
22 |
} |
|
23 |
|
|
24 | 14 |
protected void save(Ini prefs){ |
25 | 15 |
super.save(prefs); |
26 | 16 |
} |
... | ... | |
28 | 18 |
protected void loadInf(Ini prefs){ |
29 | 19 |
super.loadInf(prefs); |
30 | 20 |
} |
21 |
|
|
22 |
|
|
31 | 23 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/UserFolder.java | ||
---|---|---|
7 | 7 |
public class UserFolder extends DefaultScriptingFolder{ |
8 | 8 |
|
9 | 9 |
public UserFolder(ScriptingManager manager, File folder) { |
10 |
super(manager, folder); |
|
10 |
super(null, manager, folder);
|
|
11 | 11 |
this.setName("Scripts del usuario"); |
12 | 12 |
this.setId("UserFolder"); |
13 | 13 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/DefaultScriptingScript.java | ||
---|---|---|
1 | 1 |
package org.gvsig.scripting.impl; |
2 | 2 |
|
3 |
import java.io.BufferedWriter; |
|
4 | 3 |
import java.io.File; |
5 |
import java.io.FileWriter; |
|
6 | 4 |
import java.io.IOException; |
5 |
import java.io.InputStream; |
|
7 | 6 |
import java.util.List; |
8 | 7 |
import java.util.Map; |
9 | 8 |
|
... | ... | |
13 | 12 |
import javax.script.ScriptEngine; |
14 | 13 |
import javax.script.ScriptException; |
15 | 14 |
|
15 |
import org.apache.commons.io.FileUtils; |
|
16 |
import org.apache.commons.io.FilenameUtils; |
|
17 |
import org.apache.commons.io.IOUtils; |
|
16 | 18 |
import org.gvsig.scripting.CompileErrorException; |
17 | 19 |
import org.gvsig.scripting.ExecuteErrorException; |
18 | 20 |
import org.gvsig.scripting.ScriptingBaseScript; |
... | ... | |
24 | 26 |
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable; |
25 | 27 |
import org.gvsig.tools.task.AbstractMonitorableTask; |
26 | 28 |
import org.ini4j.Ini; |
27 |
import org.ini4j.InvalidFileFormatException; |
|
29 |
import org.slf4j.Logger; |
|
30 |
import org.slf4j.LoggerFactory; |
|
28 | 31 |
|
29 |
@SuppressWarnings("restriction") |
|
30 | 32 |
public class DefaultScriptingScript extends AbstractScript implements |
31 | 33 |
ScriptingScript { |
32 |
|
|
34 |
|
|
35 |
private static final Logger logger = LoggerFactory.getLogger(DefaultScriptingScript.class); |
|
33 | 36 |
protected String langName; |
34 | 37 |
protected String extension = null; |
35 | 38 |
protected ScriptEngine engine = null; |
... | ... | |
40 | 43 |
private boolean saved; |
41 | 44 |
private final DelegateWeakReferencingObservable delegatedObservable; |
42 | 45 |
|
43 |
public DefaultScriptingScript(ScriptingManager manager) {
|
|
44 |
super(manager);
|
|
46 |
protected DefaultScriptingScript(ScriptingFolder parent, String typename, ScriptingManager manager, String id) {
|
|
47 |
super(parent, typename, manager, id);
|
|
45 | 48 |
this.setLangName("python"); |
46 | 49 |
this.setSaved(true); |
47 | 50 |
this.delegatedObservable = new DelegateWeakReferencingObservable(this); |
48 | 51 |
} |
52 |
|
|
53 |
public DefaultScriptingScript(ScriptingFolder parent, ScriptingManager manager, String id) { |
|
54 |
this(parent, ScriptingManager.UNIT_SCRIPT, manager, id); |
|
55 |
} |
|
49 | 56 |
|
50 | 57 |
protected void notifyErrors(Exception exception, String command) { |
51 | 58 |
this.delegatedObservable.notifyObservers(new BaseScriptingNotifycation( |
... | ... | |
53 | 60 |
command, exception)); |
54 | 61 |
} |
55 | 62 |
|
56 |
// protected void launchCompilingException(Exception exception, String command) { |
|
57 |
// this.delegatedObservable.notifyObservers(new BaseScriptingNotifycation( |
|
58 |
// this, BaseScriptingNotifycation.COMPILE_ERROR_NOTIFICATION, |
|
59 |
// command, exception)); |
|
60 |
// } |
|
61 |
|
|
62 | 63 |
public void setSaved(boolean saved) { |
63 | 64 |
this.saved = saved; |
64 | 65 |
} |
65 | 66 |
|
66 | 67 |
public String getCode() { |
67 |
if (this.code != null) { |
|
68 |
return this.code; |
|
68 |
if (this.code == null) { |
|
69 |
File f=null; |
|
70 |
try { |
|
71 |
f = this.getFileResource(this.extension); |
|
72 |
this.code = FileUtils.readFileToString(f); |
|
73 |
} catch (IOException e) { |
|
74 |
String fname = (f==null)? "(null)":f.getAbsolutePath(); |
|
75 |
logger.warn("Can't load code from file '"+fname+"'."); |
|
76 |
} |
|
69 | 77 |
} |
70 |
return DefaultScriptingManager.getStringFromFile(this.getFileResource( |
|
71 |
this.extension).getAbsolutePath()); |
|
78 |
return this.code; |
|
72 | 79 |
} |
73 | 80 |
|
74 | 81 |
public void setCode(String code) { |
... | ... | |
79 | 86 |
} |
80 | 87 |
|
81 | 88 |
private String getPreparedCode() { |
82 |
String code = null; |
|
83 |
if ("python".equalsIgnoreCase(this.getLangName())) { |
|
84 |
List<File> libFolders = this.manager.getLibFolders(); |
|
85 |
if (libFolders != null && libFolders.size() > 0) { |
|
86 |
StringBuffer buffer = new StringBuffer(); |
|
87 |
buffer.append("import sys;"); |
|
88 |
for (File file : libFolders) { |
|
89 |
buffer.append("sys.path.append(r'"); |
|
90 |
buffer.append(file.getAbsolutePath()); |
|
91 |
buffer.append("');"); |
|
92 |
} |
|
93 |
code = buffer.toString(); |
|
89 |
return this.getCode(); |
|
90 |
} |
|
91 |
|
|
92 |
protected String getCodeToInitializeEngine() { |
|
93 |
List<File> libFolders = this.manager.getLibFolders(); |
|
94 |
if (libFolders == null || libFolders.size() < 1) { |
|
95 |
return null; |
|
96 |
} |
|
97 |
StringBuffer buffer = new StringBuffer(); |
|
98 |
buffer.append("import sys\n"); |
|
99 |
buffer.append("sys_path = list()\n"); |
|
100 |
for (File file : libFolders) { |
|
101 |
buffer.append("sys_path.append(r'"); |
|
102 |
buffer.append(file.getAbsolutePath()); |
|
103 |
buffer.append("')\n"); |
|
104 |
} |
|
105 |
buffer.append("sys.path.extend(sys_path)\n\n\n"); |
|
106 |
String resname = "/org/gvsig/scripting/impl/InitializaEngine.py"; |
|
107 |
InputStream code_is = this.getClass().getResourceAsStream(resname); |
|
108 |
if( code_is != null ) { |
|
109 |
try { |
|
110 |
String code = IOUtils.toString(code_is); |
|
111 |
buffer.append(code); |
|
112 |
} catch (IOException e) { |
|
113 |
logger.warn("Can't read code to initialize engine from resource '"+resname+"'.",e); |
|
94 | 114 |
} |
95 |
} |
|
96 |
|
|
97 |
if (code == null) { |
|
98 |
code = this.getCode(); |
|
99 | 115 |
} else { |
100 |
code = code + this.getCode();
|
|
116 |
logger.warn("Can't open resurce '"+resname+"'.");
|
|
101 | 117 |
} |
102 |
return code;
|
|
118 |
return buffer.toString();
|
|
103 | 119 |
} |
104 |
|
|
120 |
|
|
105 | 121 |
protected ScriptEngine getEngine() { |
106 | 122 |
if (this.engine == null) { |
107 | 123 |
ScriptEngine scriptEngine = this.manager.getEngine(this |
108 | 124 |
.getLangName()); |
109 | 125 |
scriptEngine.put("script", this); |
110 | 126 |
this.engine = scriptEngine; |
127 |
try { |
|
128 |
String code = this.getCodeToInitializeEngine(); |
|
129 |
if( code != null ) { |
|
130 |
this.engine.eval(code); |
|
131 |
} |
|
132 |
} catch(Exception ex) { |
|
133 |
logger.warn("Can't initialize engine with the code:\n"+code); |
|
134 |
} |
|
111 | 135 |
} |
112 | 136 |
return this.engine; |
113 | 137 |
} |
114 | 138 |
|
115 |
@Override |
|
116 | 139 |
protected void loadInf(Ini prefs) { |
117 | 140 |
super.loadInf(prefs); |
118 | 141 |
|
... | ... | |
121 | 144 |
this.getLangName())); |
122 | 145 |
} |
123 | 146 |
|
124 |
@Override |
|
125 | 147 |
public void load(ScriptingFolder folder, String id) { |
126 | 148 |
this.setId(id); |
127 | 149 |
this.setParent(folder); |
128 | 150 |
|
129 | 151 |
Map<String, String> languages = this.manager |
130 | 152 |
.getSupportedLanguagesByExtension(); |
131 |
String s[] = id.split("\\."); |
|
132 |
if (s.length >= 2) { |
|
133 |
String extension = "." + s[s.length - 1]; |
|
134 |
String langName = languages.get(extension); |
|
135 |
this.setLangName(langName); |
|
136 |
this.setExtension(extension); |
|
137 |
} |
|
153 |
String extension = FilenameUtils.getExtension(id); |
|
154 |
String langName = languages.get(extension); |
|
155 |
this.setLangName(langName); |
|
156 |
this.setExtension(extension); |
|
138 | 157 |
|
139 | 158 |
File f = getFileResource(".inf"); |
140 | 159 |
if (f.isFile()) { |
141 | 160 |
Ini prefs = null; |
142 |
|
|
143 | 161 |
try { |
144 | 162 |
prefs = new Ini(f); |
145 |
} catch (InvalidFileFormatException e) { |
|
146 |
e.printStackTrace(); |
|
147 |
} catch (IOException e) { |
|
148 |
e.printStackTrace(); |
|
163 |
} catch (Exception e) { |
|
164 |
logger.warn("Can't load 'inf' file '"+f.getAbsolutePath()+"'.",e); |
|
149 | 165 |
} |
150 |
|
|
151 | 166 |
loadInf(prefs); |
152 | 167 |
} |
153 | 168 |
this.setSaved(true); |
... | ... | |
158 | 173 |
if (!f.isFile()) { |
159 | 174 |
try { |
160 | 175 |
f.createNewFile(); |
161 |
} catch (IOException e) {
|
|
162 |
e.printStackTrace();
|
|
176 |
} catch (Exception e) { |
|
177 |
logger.warn("Can't create 'inf' file '"+f.getAbsolutePath()+"'.",e);
|
|
163 | 178 |
} |
164 | 179 |
} |
165 |
|
|
166 | 180 |
Ini prefs = null; |
167 |
|
|
168 | 181 |
try { |
169 | 182 |
prefs = new Ini(f); |
170 |
} catch (InvalidFileFormatException e) { |
|
171 |
e.printStackTrace(); |
|
172 |
} catch (IOException e) { |
|
173 |
e.printStackTrace(); |
|
183 |
} catch (Exception e) { |
|
184 |
logger.warn("Can't load 'inf' file '"+f.getAbsolutePath()+"'.",e); |
|
174 | 185 |
} |
175 |
|
|
176 | 186 |
save(prefs); |
177 |
|
|
178 | 187 |
// Guardo el codigo en el fichero |
188 |
File fcode = null; |
|
179 | 189 |
try { |
180 |
FileWriter fstream = new FileWriter( |
|
181 |
((DefaultScriptingFolder) this.getParent()).getPath() |
|
182 |
+ File.separator + this.getId() |
|
183 |
+ this.getExtension()); |
|
184 |
BufferedWriter out = new BufferedWriter(fstream); |
|
185 |
out.write(this.getCode()); |
|
186 |
out.close(); |
|
190 |
fcode = this.getFileResource(this.getExtension()); |
|
191 |
FileUtils.write(fcode, this.getCode()); |
|
187 | 192 |
} catch (Exception e) { |
188 |
System.err.println("Error: " + e.getMessage());
|
|
193 |
logger.warn("Can't write code to file '"+fcode.getAbsolutePath()+"'.",e);
|
|
189 | 194 |
} |
190 | 195 |
this.setSaved(true); |
191 | 196 |
} |
... | ... | |
197 | 202 |
try { |
198 | 203 |
prefs.store(); |
199 | 204 |
} catch (IOException e) { |
200 |
// TODO Auto-generated catch block
|
|
201 |
e.printStackTrace();
|
|
205 |
String fname = (prefs.getFile()==null)? "(null)" : prefs.getFile().getAbsolutePath();
|
|
206 |
logger.warn("Can't save inf file ("+fname+").",e);
|
|
202 | 207 |
} |
203 | 208 |
|
204 | 209 |
} |
... | ... | |
213 | 218 |
} |
214 | 219 |
|
215 | 220 |
public String[] getIconNames() { |
216 |
return new String[] { "scripting_" + this.getLangName().toLowerCase(), |
|
217 |
null }; |
|
221 |
return new String[] { |
|
222 |
"scripting_" + this.getLangName().toLowerCase(), |
|
223 |
"scripting_" + this.getLangName().toLowerCase() + "_open" |
|
224 |
}; |
|
218 | 225 |
} |
219 | 226 |
|
220 | 227 |
public String getMainName() { |
... | ... | |
230 | 237 |
} |
231 | 238 |
|
232 | 239 |
public void setExtension(final String extension) { |
233 |
this.extension = extension; |
|
240 |
if( !extension.startsWith(".") ) { |
|
241 |
this.extension = "." + extension; |
|
242 |
} else { |
|
243 |
this.extension = extension; |
|
244 |
} |
|
234 | 245 |
} |
235 | 246 |
|
236 | 247 |
public boolean isSaved() { |
... | ... | |
339 | 350 |
} |
340 | 351 |
|
341 | 352 |
public File getResource(String filename) { |
342 |
return new File( this.getParent().getPathFile(), filename);
|
|
353 |
return new File( this.getParent().getFile(), filename); |
|
343 | 354 |
} |
344 | 355 |
|
345 | 356 |
class ScriptTask extends AbstractMonitorableTask { |
... | ... | |
354 | 365 |
this.script.put("task",this); |
355 | 366 |
this.script.put("taskStatus",this.getTaskStatus()); |
356 | 367 |
} |
357 |
|
|
368 |
|
|
358 | 369 |
public void run() { |
359 | 370 |
try { |
360 |
// When running from the composer messages of stdout shows in the console tab. |
|
361 |
System.out.println("Running script "+ this.script.getName()+"."); |
|
371 |
console_println("Running script "+ this.script.getName()+"."); |
|
362 | 372 |
script.run(this.args); |
363 |
System.out.println("Script "+ this.script.getName()+ " terminated.");
|
|
373 |
console_println("Script "+ this.script.getName()+ " terminated.");
|
|
364 | 374 |
} catch(Throwable e) { |
365 |
System.out.println("Stript "+ this.script.getName()+ " aborted.");
|
|
375 |
console_println("Stript "+ this.script.getName()+ " aborted.");
|
|
366 | 376 |
} finally { |
367 | 377 |
this.taskStatus.terminate(); |
368 | 378 |
try { |
... | ... | |
384 | 394 |
task.start(); |
385 | 395 |
} |
386 | 396 |
|
397 |
public boolean remove() { |
|
398 |
boolean r = true; |
|
399 |
File folder = this.getParent().getFile(); |
|
400 |
File f = null; |
|
401 |
try { |
|
402 |
f = new File(folder,this.getId()+".inf"); |
|
403 |
FileUtils.forceDelete(f); |
|
404 |
} catch (IOException e) { |
|
405 |
logger.warn("Can't remove inf file '"+f.getAbsolutePath()+"'.",e); |
|
406 |
r = false; |
|
407 |
} |
|
408 |
try { |
|
409 |
f = new File(folder,this.getId()+this.getExtension()); |
|
410 |
FileUtils.forceDelete(f); |
|
411 |
} catch (IOException e) { |
|
412 |
logger.warn("Can't remove code file '"+f.getAbsolutePath()+"'.",e); |
|
413 |
r = false; |
|
414 |
} |
|
415 |
return r; |
|
416 |
} |
|
417 |
|
|
418 |
public void create(ScriptingFolder folder, String id, String language) { |
|
419 |
this.setParent(folder); |
|
420 |
this.setId(id); |
|
421 |
if( language==null ) { |
|
422 |
this.setLangName("python"); |
|
423 |
} else { |
|
424 |
this.setLangName(language); |
|
425 |
} |
|
426 |
this.setExtension(this.manager.getExtensionByLanguage(getLangName())); |
|
427 |
|
|
428 |
File file = new File(folder.getFile(),id +".inf"); |
|
429 |
try { |
|
430 |
file.createNewFile(); |
|
431 |
} catch (IOException e) { |
|
432 |
logger.warn("Can't create file of the dialog in '"+file.getAbsolutePath()+"'.",e); |
|
433 |
} |
|
434 |
|
|
435 |
if( "python".equalsIgnoreCase(this.getLangName()) ) { |
|
436 |
this.setCode("\nfrom gvsig import *\n\ndef main():\n #Remove this lines and add here your code\n\n print \"hola mundo\"\n pass\n\n"); |
|
437 |
} |
|
438 |
this.save(); |
|
439 |
} |
|
440 |
|
|
387 | 441 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/DefaultScriptingProject.java | ||
---|---|---|
1 | 1 |
package org.gvsig.scripting.impl; |
2 | 2 |
|
3 |
import java.io.File; |
|
4 |
import java.io.IOException; |
|
5 |
|
|
6 |
import org.apache.commons.io.FileUtils; |
|
3 | 7 |
import org.gvsig.scripting.ScriptingFolder; |
4 | 8 |
import org.gvsig.scripting.ScriptingManager; |
5 | 9 |
import org.gvsig.scripting.ScriptingProject; |
10 |
import org.slf4j.Logger; |
|
11 |
import org.slf4j.LoggerFactory; |
|
6 | 12 |
|
7 | 13 |
public class DefaultScriptingProject extends DefaultScriptingScript implements ScriptingProject { |
8 | 14 |
|
9 |
public DefaultScriptingProject(ScriptingManager manager) { |
|
10 |
super(manager); |
|
15 |
private static final Logger logger = LoggerFactory.getLogger(DefaultScriptingProject.class); |
|
16 |
|
|
17 |
public DefaultScriptingProject(ScriptingFolder parent, ScriptingManager manager, String id) { |
|
18 |
super(parent, ScriptingManager.UNIT_PROJECT,manager, id); |
|
11 | 19 |
} |
12 | 20 |
|
13 | 21 |
public Object run(Object args[]) { |
... | ... | |
23 | 31 |
public String[] getIconNames() { |
24 | 32 |
return new String[]{"scripting_project", "scripting_project_open"}; |
25 | 33 |
} |
34 |
|
|
35 |
public boolean remove() { |
|
36 |
boolean r = true; |
|
37 |
File folder = this.getParent().getFile(); |
|
38 |
File f = null; |
|
39 |
try { |
|
40 |
f = new File(folder,this.getId()); |
|
41 |
FileUtils.forceDelete(f); |
|
42 |
} catch (IOException e) { |
|
43 |
logger.warn("Can't remove project '"+f.getAbsolutePath()+"'.",e); |
|
44 |
r = false; |
|
45 |
} |
|
46 |
return r; |
|
47 |
} |
|
48 |
|
|
49 |
public void create(ScriptingFolder folder, String id, String language) { |
|
50 |
this.setParent(folder); |
|
51 |
this.setId(id); |
|
52 |
this.setLangName(language); |
|
53 |
this.setExtension(this.manager.getExtensionByLanguage(language)); |
|
54 |
|
|
55 |
File file = new File(folder.getFile(),id +".inf"); |
|
56 |
try { |
|
57 |
file.createNewFile(); |
|
58 |
} catch (IOException e) { |
|
59 |
logger.warn("Can't create file of the dialog in '"+file.getAbsolutePath()+"'.",e); |
|
60 |
} |
|
61 |
|
|
62 |
this.save(); |
|
63 |
} |
|
26 | 64 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/Unit.java | ||
---|---|---|
5 | 5 |
|
6 | 6 |
public interface Unit extends ScriptingUnit { |
7 | 7 |
|
8 |
public abstract void load(ScriptingFolder folder, String id);
|
|
8 |
public void load(ScriptingFolder folder, String id); |
|
9 | 9 |
|
10 |
public abstract String getDescription();
|
|
10 |
public void setId(String id);
|
|
11 | 11 |
|
12 |
public abstract String getCreatedBy(); |
|
13 |
|
|
14 |
public abstract String getVersion(); |
|
12 |
public void create(ScriptingFolder folder, String id, String language); |
|
15 | 13 |
|
16 |
public abstract String getId(); |
|
17 |
|
|
18 |
public abstract String getName(); |
|
19 |
|
|
20 |
public abstract void setDescription(String description); |
|
21 |
|
|
22 |
public abstract void setCreatedBy(String createdBy); |
|
23 |
|
|
24 |
public abstract void setVersion(String version); |
|
25 |
|
|
26 |
public abstract void setId(String id); |
|
27 |
|
|
28 |
public abstract void setName(String name); |
|
29 |
|
|
30 |
public abstract ScriptingFolder getParent(); |
|
31 |
|
|
32 |
public abstract boolean move(ScriptingFolder target); |
|
33 |
|
|
34 |
public abstract boolean rename(String newId); |
|
35 |
|
|
36 | 14 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/SystemFolder.java | ||
---|---|---|
4 | 4 |
import java.util.ArrayList; |
5 | 5 |
import java.util.List; |
6 | 6 |
|
7 |
import org.gvsig.scripting.ScriptingFolder; |
|
7 | 8 |
import org.gvsig.scripting.ScriptingManager; |
8 | 9 |
import org.gvsig.scripting.ScriptingUnit; |
9 | 10 |
import org.gvsig.scripting.impl.DefaultScriptingManager.RegisterSystemFolder; |
... | ... | |
11 | 12 |
public class SystemFolder extends DefaultScriptingFolder{ |
12 | 13 |
|
13 | 14 |
public SystemFolder(ScriptingManager manager) { |
14 |
super(manager); |
|
15 |
super((ScriptingFolder)null,manager);
|
|
15 | 16 |
this.setName("Scripts del Sistema"); |
16 | 17 |
} |
17 | 18 |
|
... | ... | |
31 | 32 |
List<RegisterSystemFolder> folders = this.manager.getSystemFolders(); |
32 | 33 |
for (int i = 0; i < folders.size(); i++){ |
33 | 34 |
RegisterSystemFolder systemfolder = folders.get(i); |
34 |
DefaultScriptingFolder folder = new DefaultScriptingFolder(manager, systemfolder.folder.getAbsoluteFile()); |
|
35 |
DefaultScriptingFolder folder = new DefaultScriptingFolder((ScriptingFolder)null,manager, systemfolder.folder.getAbsoluteFile());
|
|
35 | 36 |
if( path.startsWith(folder.getId()+"/")) { |
36 | 37 |
unit = folder.getUnit( new File(path.substring(folder.getId().length()+1))); |
37 | 38 |
if( unit != null ) { |
... | ... | |
48 | 49 |
List<RegisterSystemFolder> folders = this.manager.getSystemFolders(); |
49 | 50 |
for (int i = 0; i < folders.size(); i++){ |
50 | 51 |
RegisterSystemFolder systemfolder = folders.get(i); |
51 |
DefaultScriptingFolder folder = new DefaultScriptingFolder(manager, systemfolder.folder.getAbsoluteFile()); |
|
52 |
DefaultScriptingFolder folder = new DefaultScriptingFolder((ScriptingFolder)null,manager, systemfolder.folder.getAbsoluteFile());
|
|
52 | 53 |
folder.setName(systemfolder.name); |
53 | 54 |
ol.add(folder); |
54 | 55 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/pom.xml | ||
---|---|---|
13 | 13 |
<version>1.0.0-SNAPSHOT</version> |
14 | 14 |
</parent> |
15 | 15 |
<dependencies> |
16 |
<dependency> |
|
17 |
<groupId>commons-io</groupId> |
|
18 |
<artifactId>commons-io</artifactId> |
|
19 |
<scope>compile</scope> |
|
20 |
</dependency> |
|
16 | 21 |
<dependency> |
17 | 22 |
<groupId>org.gvsig</groupId> |
18 | 23 |
<artifactId>org.gvsig.scripting.lib.api</artifactId> |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.api/src/main/java/org/gvsig/scripting/ScriptingManager.java | ||
---|---|---|
98 | 98 |
public ScriptingFolder createFolder(ScriptingFolder folder, String id); |
99 | 99 |
|
100 | 100 |
public ScriptingUnit createUnit(String unitType, ScriptingFolder folder, String id); |
101 |
|
|
102 |
public ScriptingUnit createUnit(String unitType, ScriptingFolder folder, String id, String language); |
|
101 | 103 |
|
102 | 104 |
public List<String> getUnitTypes(); |
103 | 105 |
|
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.api/src/main/java/org/gvsig/scripting/ScriptingFolder.java | ||
---|---|---|
44 | 44 |
*/ |
45 | 45 |
public List<ScriptingUnit> getUnits(); |
46 | 46 |
|
47 |
/** |
|
48 |
* Gets the path of the ScriptingFolder. |
|
49 |
* Use getPathFile as a prefered option. |
|
50 |
* |
|
51 |
* @return a String with the ScriptingFolder's path. |
|
52 |
* |
|
53 |
*/ |
|
54 |
public String getPath(); |
|
55 |
|
|
56 |
/** |
|
57 |
* Gets the path of the ScriptingFolder. |
|
58 |
* |
|
59 |
* @return a File with the ScriptingFolder's path. |
|
60 |
* |
|
61 |
*/ |
|
62 |
public File getPathFile(); |
|
63 |
|
|
64 | 47 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.api/src/main/java/org/gvsig/scripting/ScriptingBaseScript.java | ||
---|---|---|
16 | 16 |
public interface ScriptingBaseScript extends ScriptingUnit, WeakReferencingObservable{ |
17 | 17 |
|
18 | 18 |
/** |
19 |
* Gets a String with the ScriptBaseScript's file path. |
|
20 |
* |
|
21 |
* @return a string with the path of the ScriptBaseScript main file path. |
|
22 |
*/ |
|
23 |
public String getPath(); |
|
24 |
|
Also available in: Unified diff