Revision 468
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.api/src/main/java/org/gvsig/scripting/ScriptingProject.java | ||
---|---|---|
1 |
package org.gvsig.scripting; |
|
2 |
|
|
3 |
/** |
|
4 |
* <p> Interface that represents a set of {@link ScriptingBaseScript}.</p> |
|
5 |
* |
|
6 |
* @see ScriptingUnit |
|
7 |
* @see ScriptingBaseScript |
|
8 |
*/ |
|
9 |
public interface ScriptingProject extends ScriptingBaseScript { |
|
10 |
|
|
11 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.api/src/main/java/org/gvsig/scripting/ScriptingScript.java | ||
---|---|---|
76 | 76 |
*/ |
77 | 77 |
public Object invokeFunction(String name, Object[] args) throws NoSuchMethodException; |
78 | 78 |
|
79 |
public String getMimeType(); |
|
79 | 80 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.api/src/main/java/org/gvsig/scripting/ScriptingData.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.scripting; |
|
3 |
|
|
4 |
|
|
5 |
public interface ScriptingData extends ScriptingUnit { |
|
6 |
|
|
7 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.api/src/main/java/org/gvsig/scripting/ScriptingManager.java | ||
---|---|---|
3 | 3 |
import java.io.File; |
4 | 4 |
import java.util.List; |
5 | 5 |
import java.util.Map; |
6 |
import org.gvsig.tools.service.spi.ProviderFactory; |
|
6 | 7 |
|
7 | 8 |
/** |
8 |
* There are two top level management roles within ScriptingFramework: logical and User's Interface (UI) management. |
|
9 |
* There are two top level management roles within ScriptingFramework: logical |
|
10 |
* and User's Interface (UI) management. |
|
9 | 11 |
* |
10 |
* This class is responsible of the logical role. It provides all the services to manage the information used in ScriptingFramework. |
|
12 |
* This class is responsible of the logical role. It provides all the services |
|
13 |
* to manage the information used in ScriptingFramework. |
|
11 | 14 |
* |
12 | 15 |
* @see ScriptingUIManager |
13 | 16 |
* @see ScriptingHelpManager |
... | ... | |
17 | 20 |
public static final String INSTALLER_PROVIDER_NAME = "Script"; |
18 | 21 |
public static final String INSTALLER_PROVIDER_DESCRIPTION = "Provider to install scripts"; |
19 | 22 |
|
20 |
|
|
21 |
public static final String UNIT_SCRIPT = "Script"; |
|
22 |
public static final String UNIT_DIALOG = "Dialog"; |
|
23 |
public static final String UNIT_PROJECT = "Project"; |
|
24 |
public static final String UNIT_FOLDER = "Folder"; |
|
25 |
|
|
26 |
|
|
27 |
public void setHomeFolder(File home); |
|
28 |
|
|
29 |
public File getHomeFolder(); |
|
30 |
|
|
31 |
/** |
|
32 |
* Returns the System's Folder |
|
33 |
* |
|
34 |
* @return a {@link ScriptingFolder} with the System's Folder. |
|
35 |
*/ |
|
36 |
public ScriptingFolder getSystemFolder(); |
|
37 |
|
|
38 |
/** |
|
39 |
* Returns the User's Folder |
|
40 |
* |
|
41 |
* @return a {@link ScriptingFolder} with the User's Folder. |
|
42 |
*/ |
|
43 |
public ScriptingFolder getUserFolder(); |
|
44 |
|
|
45 |
public List<ScriptingFolder> getAlternativeUserFolders(); |
|
46 |
|
|
47 |
public void addAlternativeUserFolder(File f,String name, String description); |
|
48 |
|
|
49 |
/** |
|
50 |
* Sets a new path to the User's Folder |
|
51 |
* |
|
52 |
* @param path a String with the new path to the User's Folder |
|
53 |
*/ |
|
54 |
// public void setRootUserFolder(String path); |
|
55 |
|
|
56 |
/** |
|
57 |
* Creates a new instance of {@link ScriptingScript} named as the id and in the directory specified |
|
58 |
* |
|
59 |
* @param folder {@link ScriptingFolder} with the destiny directory information |
|
60 |
* @param id String with the identificator of the {@link ScriptingScript} created |
|
61 |
* |
|
62 |
* @return the new {@link ScriptingScript} |
|
63 |
* @deprecated see {@link #createUnit(String, ScriptingFolder, String)} |
|
64 |
*/ |
|
65 |
@Deprecated |
|
66 |
public ScriptingScript createScript(ScriptingFolder folder, String id); |
|
67 |
|
|
68 |
/** |
|
69 |
* Creates a new instance of {@link ScriptingProject} named as the id and in the directory specified |
|
70 |
* |
|
71 |
* @param folder {@link ScriptingFolder} with the destiny directory information |
|
72 |
* @param id String with the identificator of the {@link ScriptingProject} created |
|
73 |
* |
|
74 |
* @return the new {@link ScriptingProject} |
|
75 |
* @deprecated see {@link #createUnit(String, ScriptingFolder, String)} |
|
76 |
*/ |
|
77 |
@Deprecated |
|
78 |
public ScriptingProject createProject(ScriptingFolder folder, String id); |
|
79 |
|
|
80 |
/** |
|
81 |
* Creates a new instance of {@link ScriptingDialog} named as the id and in the directory specified |
|
82 |
* |
|
83 |
* @param folder {@link ScriptingFolder} with the destiny directory information |
|
84 |
* @param id String with the identificator of the {@link ScriptingDialog} created |
|
85 |
* |
|
86 |
* @return the new {@link ScriptingDialog} |
|
87 |
* @deprecated see {@link #createUnit(String, ScriptingFolder, String)} |
|
88 |
*/ |
|
89 |
@Deprecated |
|
90 |
public ScriptingDialog createDialog(ScriptingFolder folder, String id); |
|
91 |
|
|
92 |
/** |
|
93 |
* Creates a new instance of {@link ScriptingFolder} named as the id and in the directory specified |
|
94 |
* |
|
95 |
* @param folder {@link ScriptingFolder} with the destiny directory information |
|
96 |
* @param id String with the identificator of the {@link ScriptingFolder} created |
|
97 |
* |
|
98 |
* @return the new {@link ScriptingFolder} |
|
99 |
* @deprecated see {@link #createUnit(String, ScriptingFolder, String)} |
|
100 |
*/ |
|
101 |
@Deprecated |
|
102 |
public ScriptingFolder createFolder(ScriptingFolder folder, String id); |
|
103 |
|
|
23 |
public static final String UNIT_SCRIPT = "Script"; |
|
24 |
public static final String UNIT_DIALOG = "Dialog"; |
|
25 |
public static final String UNIT_FOLDER = "Folder"; |
|
26 |
|
|
27 |
public void setHomeFolder(File home); |
|
28 |
|
|
29 |
public File getHomeFolder(); |
|
30 |
|
|
31 |
/** |
|
32 |
* Returns the System's Folder |
|
33 |
* |
|
34 |
* @return a {@link ScriptingFolder} with the System's Folder. |
|
35 |
*/ |
|
36 |
public ScriptingFolder getSystemFolder(); |
|
37 |
|
|
38 |
/** |
|
39 |
* Returns the User's Folder |
|
40 |
* |
|
41 |
* @return a {@link ScriptingFolder} with the User's Folder. |
|
42 |
*/ |
|
43 |
public ScriptingFolder getUserFolder(); |
|
44 |
|
|
45 |
public List<ScriptingFolder> getAlternativeUserFolders(); |
|
46 |
|
|
47 |
public void addAlternativeUserFolder(File f, String name, String description); |
|
48 |
|
|
104 | 49 |
public ScriptingUnit createUnit(String unitType, ScriptingFolder folder, String id); |
105 | 50 |
|
106 | 51 |
public ScriptingUnit createUnit(String unitType, ScriptingFolder folder, String id, String language); |
107 |
|
|
52 |
|
|
108 | 53 |
public List<String> getUnitTypes(); |
109 |
|
|
110 |
/** |
|
111 |
* Returns the {@link ScriptingBaseScript} associated with the file specified |
|
112 |
* |
|
113 |
* @param file File where is contained the {@link ScriptingBaseScript}'s information |
|
114 |
* |
|
115 |
* @return {@link ScriptingBaseScript} |
|
116 |
*/ |
|
54 |
|
|
55 |
/** |
|
56 |
* Returns the {@link ScriptingBaseScript} associated with the file |
|
57 |
* specified |
|
58 |
* |
|
59 |
* @param file File where is contained the {@link ScriptingBaseScript}'s |
|
60 |
* information |
|
61 |
* |
|
62 |
* @return {@link ScriptingBaseScript} |
|
63 |
*/ |
|
117 | 64 |
public ScriptingBaseScript getScript(File file); |
118 | 65 |
|
119 | 66 |
public ScriptingBaseScript getScript(String name); |
120 |
/** |
|
121 |
* Returns the {@link ScriptingFolder} associated with the file specified |
|
122 |
* |
|
123 |
* @param file File where is contained the {@link ScriptingFolder}'s information |
|
124 |
* |
|
125 |
* @return {@link ScriptingFolder} |
|
126 |
*/ |
|
67 |
|
|
68 |
/** |
|
69 |
* Returns the {@link ScriptingFolder} associated with the file specified |
|
70 |
* |
|
71 |
* @param file File where is contained the {@link ScriptingFolder}'s |
|
72 |
* information |
|
73 |
* |
|
74 |
* @return {@link ScriptingFolder} |
|
75 |
*/ |
|
127 | 76 |
public ScriptingFolder getFolder(File file); |
128 |
|
|
129 |
/**
|
|
130 |
* Returns the path of the User's Folder
|
|
131 |
*
|
|
132 |
* @return a String with the path of the User's Folder
|
|
133 |
*/
|
|
77 |
|
|
78 |
/**
|
|
79 |
* Returns the path of the User's Folder
|
|
80 |
*
|
|
81 |
* @return a String with the path of the User's Folder
|
|
82 |
*/
|
|
134 | 83 |
// public String getRootUserFolder(); |
135 |
|
|
136 |
/**
|
|
137 |
* Registers a File System with {@link ScriptingUnit}s in the SystemFolder
|
|
138 |
*
|
|
139 |
* @param name String with the identificator name of the File System registered
|
|
140 |
* @param folder File with the root of the File System to register
|
|
141 |
*/
|
|
84 |
/** |
|
85 |
* Registers a File System with {@link ScriptingUnit}s in the SystemFolder
|
|
86 |
*
|
|
87 |
* @param name String with the identificator name of the File System
|
|
88 |
* registered
|
|
89 |
* @param folder File with the root of the File System to register
|
|
90 |
*/
|
|
142 | 91 |
public void registerSystemFolder(String name, File folder); |
143 | 92 |
|
93 |
public List<String> getSupportedLanguages(); |
|
94 |
|
|
95 |
public String getExtensionOfLanguage(String language); |
|
96 |
|
|
97 |
public String getEngineNameByLanguage(String langName); |
|
98 |
|
|
144 | 99 |
/** |
145 |
* Gets all the supported Language by their extensions associated with each |
|
146 |
* one. |
|
147 |
* |
|
148 |
* @return a Map with the pairs <key,value> where the key is a String with |
|
149 |
* the extension and the value is the Language associated |
|
100 |
* Checks if an id is unique in a determinate folder |
|
101 |
* |
|
102 |
* @param folder { |
|
103 |
* @ScriptingFolder} with the directory to verify the id |
|
104 |
* @param id String with the id to validate |
|
105 |
* |
|
106 |
* @return true if there isn't another {@link ScriptingUnit} with the same |
|
107 |
* id, and false in the other case |
|
150 | 108 |
*/ |
151 |
public Map<String, String> getSupportedLanguagesByExtension();
|
|
109 |
public boolean validateUnitId(ScriptingFolder folder, String id);
|
|
152 | 110 |
|
153 | 111 |
/** |
154 |
* Gets all the supported Language and the extensions associated with each |
|
155 |
* one. |
|
156 |
* |
|
157 |
* @return a Map with the pairs <key,value> where the key is a String with |
|
158 |
* the Language and the value is the extension associated |
|
112 |
* Sets a key/value pair in the state of the ScriptingManager that may |
|
113 |
* either create a Java Language Binding to be used in the execution of |
|
114 |
* scripts |
|
115 |
* |
|
116 |
* @param key The name of named value to add |
|
117 |
* @param value The value of named value to add. |
|
159 | 118 |
*/ |
160 |
public Map<String, String> getExtensionsByLanguages(); |
|
161 |
|
|
162 |
/** |
|
163 |
* Checks if an id is unique in a determinate folder |
|
164 |
* |
|
165 |
* @param folder {@ScriptingFolder} with the directory to verify the id |
|
166 |
* @param id String with the id to validate |
|
167 |
* |
|
168 |
* @return true if there isn't another {@link ScriptingUnit} with the same id, and false in the other case |
|
169 |
*/ |
|
170 |
public boolean validateUnitId(ScriptingFolder folder, String id); |
|
171 |
|
|
119 |
public void put(String key, Object value); |
|
172 | 120 |
|
173 |
/** |
|
174 |
* Sets a key/value pair in the state of the ScriptingManager that may |
|
175 |
* either create a Java Language Binding to be used in the |
|
176 |
* execution of scripts |
|
177 |
* |
|
178 |
* @param key The name of named value to add |
|
179 |
* @param value The value of named value to add. |
|
180 |
*/ |
|
181 |
public void put(String key, Object value); |
|
182 |
|
|
183 |
/** |
|
184 |
* Retrieves a value set in the state of this ScriptingManager |
|
185 |
* |
|
186 |
* @param key The key whose value is to be returned |
|
187 |
* |
|
188 |
* @return an Object with the value corresponding to the key |
|
189 |
*/ |
|
190 |
public Object get(String key); |
|
191 |
|
|
192 |
/** |
|
193 |
* Gets the Help manager |
|
194 |
* |
|
195 |
* @return a {@link ScriptingHelpManager} |
|
196 |
* |
|
197 |
* @see ScriptingHelpManager |
|
198 |
*/ |
|
199 |
public ScriptingHelpManager getHelpManager(); |
|
121 |
/** |
|
122 |
* Retrieves a value set in the state of this ScriptingManager |
|
123 |
* |
|
124 |
* @param key The key whose value is to be returned |
|
125 |
* |
|
126 |
* @return an Object with the value corresponding to the key |
|
127 |
*/ |
|
128 |
public Object get(String key); |
|
200 | 129 |
|
201 |
public void addLibFolder(File lib); |
|
130 |
/** |
|
131 |
* Gets the Help manager |
|
132 |
* |
|
133 |
* @return a {@link ScriptingHelpManager} |
|
134 |
* |
|
135 |
* @see ScriptingHelpManager |
|
136 |
*/ |
|
137 |
public ScriptingHelpManager getHelpManager(); |
|
202 | 138 |
|
203 |
public File getRootUserFolder(); |
|
139 |
public void addLibFolder(File lib); |
|
140 |
|
|
141 |
public File getRootUserFolder(); |
|
142 |
|
|
143 |
public ProviderFactory getInstallerFactory(); |
|
144 |
|
|
145 |
/** |
|
146 |
* Devuelbe la carpeta en la que se encuentran los paquetes |
|
147 |
* de la aplicacion. |
|
148 |
* |
|
149 |
* @return |
|
150 |
*/ |
|
151 |
public File getPackagesFolder(); |
|
152 |
|
|
153 |
/** |
|
154 |
* Establece la carpeta en la que se encuentran los paquetes |
|
155 |
* de la aplicacion. |
|
156 |
* @param folder |
|
157 |
*/ |
|
158 |
public void setPackagesFolder(File folder); |
|
204 | 159 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.api/src/main/java/org/gvsig/scripting/ScriptingFolder.java | ||
---|---|---|
43 | 43 |
* |
44 | 44 |
*/ |
45 | 45 |
public List<ScriptingUnit> getUnits(); |
46 |
|
|
47 |
public List<ScriptingFolder> getUnitFolders(); |
|
46 | 48 |
|
47 | 49 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.api/src/main/java/org/gvsig/scripting/ScriptingText.java | ||
---|---|---|
1 |
package org.gvsig.scripting; |
|
2 |
|
|
3 |
import org.gvsig.tools.observer.WeakReferencingObservable; |
|
4 |
|
|
5 |
public interface ScriptingText extends ScriptingData, WeakReferencingObservable { |
|
6 |
|
|
7 |
public String getText(); |
|
8 |
|
|
9 |
public void setText(String text); |
|
10 |
|
|
11 |
public void save(); |
|
12 |
|
|
13 |
public String getMimeType(); |
|
14 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.api/src/test/java/org/gvsig/scripting/TestManager.java | ||
---|---|---|
59 | 59 |
deleteWorkingFolderContent(); |
60 | 60 |
} |
61 | 61 |
|
62 |
public void testExtensionsAndLanguages(){ |
|
63 |
Map<String, String> langByExt = |
|
64 |
manager.getSupportedLanguagesByExtension(); |
|
65 |
Map<String, String> extByLang = manager.getExtensionsByLanguages(); |
|
66 |
|
|
67 |
Set<String> keyExt = langByExt.keySet(); |
|
68 |
Iterator<String> it = keyExt.iterator(); |
|
69 |
while(it.hasNext()){ |
|
70 |
Object ext = it.next(); |
|
71 |
Object lang = langByExt.get(ext); |
|
72 |
Assert.assertEquals(extByLang.get(lang), ext); |
|
73 |
} |
|
74 |
} |
|
75 | 62 |
|
76 | 63 |
public void checkUnitValues(ScriptingUnit unit1, ScriptingUnit unit2){ |
77 | 64 |
Assert.assertEquals(unit1.getId(),unit2.getId()); |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/nbactions.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<actions> |
|
3 |
<action> |
|
4 |
<actionName>CUSTOM-dependency:tree</actionName> |
|
5 |
<displayName>dependency:tree</displayName> |
|
6 |
<recursive>false</recursive> |
|
7 |
<goals> |
|
8 |
<goal>dependency:tree</goal> |
|
9 |
</goals> |
|
10 |
</action> |
|
11 |
</actions> |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/resources/org/gvsig/scripting/langs/groovy/new_template.txt | ||
---|---|---|
1 |
|
|
2 |
|
|
3 |
def main() { |
|
4 |
|
|
5 |
printf "hola\n" |
|
6 |
|
|
7 |
} |
|
8 |
|
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/resources/org/gvsig/scripting/langs/ecmascript/new_template.txt | ||
---|---|---|
1 |
|
|
2 |
|
|
3 |
|
|
4 |
function main() { |
|
5 |
|
|
6 |
print("hola\n"); |
|
7 |
|
|
8 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/resources/org/gvsig/scripting/langs/python/init.txt | ||
---|---|---|
1 |
|
|
2 |
|
|
3 |
__file__ = script.getFile().getAbsolutePath() |
|
4 |
|
|
5 |
|
|
6 |
def __init_python_path(): |
|
7 |
import sys |
|
8 |
|
|
9 |
# Add folder of script |
|
10 |
path = script.getFile().getParentFile().getCanonicalPath() |
|
11 |
if not path in sys.path: |
|
12 |
sys.path.append(path) |
|
13 |
|
|
14 |
# Add folder of root user scripts |
|
15 |
path = script.getManager().getUserFolder().getFile().getCanonicalPath() |
|
16 |
if not path in sys.path: |
|
17 |
sys.path.append(path) |
|
18 |
|
|
19 |
# Add folders of libs |
|
20 |
folders = script.getManager().getLibFolders() |
|
21 |
if folders != None: |
|
22 |
for folder in folders: |
|
23 |
path = folder.getCanonicalPath() |
|
24 |
if not path in sys.path: |
|
25 |
sys.path.append(path) |
|
26 |
|
|
27 |
# Add the scripts folder of system folders |
|
28 |
folders = script.getManager().getSystemFolder() |
|
29 |
if folders != None: |
|
30 |
for folder in folders.getUnits(): |
|
31 |
path = folder.getFile().getCanonicalPath() |
|
32 |
if not path in sys.path: |
|
33 |
sys.path.append(path) |
|
34 |
|
|
35 |
|
|
36 |
def use_plugin(pluginName): |
|
37 |
from org.gvsig.andami import PluginsLocator |
|
38 |
|
|
39 |
pluginsManager = PluginsLocator.getManager() |
|
40 |
other = pluginsManager.getPlugin(pluginName) |
|
41 |
scripting = pluginsManager.getPlugin("org.gvsig.scripting.app.extension") |
|
42 |
scripting.addDependencyWithPlugin(other) |
|
43 |
|
|
44 |
def use_jar(fname, root=__file__): |
|
45 |
from java.io import File |
|
46 |
import sys |
|
47 |
import os |
|
48 |
|
|
49 |
if isinstance(fname,File): |
|
50 |
f = fname |
|
51 |
fname = f.getPath() |
|
52 |
else: |
|
53 |
f = File(fname) |
|
54 |
if not f.isAbsolute() : |
|
55 |
rf = File(root) |
|
56 |
if rf.isFile() : |
|
57 |
rf = rf.getParentFile() |
|
58 |
f = File( rf,fname) |
|
59 |
|
|
60 |
fname = f.getCanonicalPath() |
|
61 |
if not fname in sys.path: |
|
62 |
sys.path.append(fname) |
|
63 |
|
|
64 |
|
|
65 |
__init_python_path() |
|
66 |
|
|
67 |
|
|
68 |
|
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/resources/org/gvsig/scripting/langs/python/new_template.txt | ||
---|---|---|
1 |
|
|
2 |
from gvsig import * |
|
3 |
|
|
4 |
def main(*args): |
|
5 |
|
|
6 |
#Remove this lines and add here your code |
|
7 |
|
|
8 |
print "hola mundo" |
|
9 |
pass |
|
10 |
|
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/resources/org/gvsig/scripting/langs/r/new_template.txt | ||
---|---|---|
1 |
|
|
2 |
main <- function() { |
|
3 |
|
|
4 |
cat( "Hello\n" ); |
|
5 |
|
|
6 |
} |
|
7 |
|
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 |
package org.gvsig.scripting.impl; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.IOException; |
|
5 |
|
|
6 |
import org.apache.commons.io.FileUtils; |
|
7 |
import org.gvsig.scripting.ScriptingFolder; |
|
8 |
import org.gvsig.scripting.ScriptingManager; |
|
9 |
import org.gvsig.scripting.ScriptingProject; |
|
10 |
import org.slf4j.Logger; |
|
11 |
import org.slf4j.LoggerFactory; |
|
12 |
|
|
13 |
public class DefaultScriptingProject extends DefaultScriptingScript implements ScriptingProject { |
|
14 |
|
|
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); |
|
19 |
} |
|
20 |
|
|
21 |
public Object run(Object args[]) { |
|
22 |
// TODO Auto-generated method stub |
|
23 |
return null; |
|
24 |
} |
|
25 |
|
|
26 |
public void load(ScriptingFolder folder, String id) { |
|
27 |
// TODO Auto-generated method stub |
|
28 |
|
|
29 |
} |
|
30 |
|
|
31 |
public String[] getIconNames() { |
|
32 |
return new String[]{"scripting_project", "scripting_project_open"}; |
|
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 |
} |
|
64 |
} |
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 | ||
---|---|---|
7 | 7 |
import java.io.FileWriter; |
8 | 8 |
import java.io.IOException; |
9 | 9 |
import java.io.Writer; |
10 |
import java.util.Map; |
|
11 | 10 |
|
12 | 11 |
import org.apache.commons.io.FileUtils; |
13 |
import org.apache.commons.io.FilenameUtils; |
|
14 | 12 |
import org.gvsig.scripting.ExecuteErrorException; |
15 | 13 |
import org.gvsig.scripting.ScriptingDialog; |
16 | 14 |
import org.gvsig.scripting.ScriptingFolder; |
... | ... | |
22 | 20 |
import org.slf4j.Logger; |
23 | 21 |
import org.slf4j.LoggerFactory; |
24 | 22 |
|
25 |
public class DefaultScriptingDialog extends DefaultScriptingScript implements ScriptingDialog, ActionListener{
|
|
23 |
public class DefaultScriptingDialog extends DefaultScriptingScript implements ScriptingDialog, ActionListener {
|
|
26 | 24 |
|
27 |
private static final Logger logger = LoggerFactory.getLogger(DefaultScriptingDialog.class);
|
|
28 |
private int showMode;
|
|
25 |
private static final Logger logger = LoggerFactory.getLogger(DefaultScriptingDialog.class);
|
|
26 |
private int showMode;
|
|
29 | 27 |
|
30 |
public DefaultScriptingDialog(ScriptingFolder parent, ScriptingManager manager, String id) { |
|
31 |
super(parent, ScriptingManager.UNIT_DIALOG,manager, id); |
|
32 |
this.setMainName("onload"); |
|
33 |
this.showMode=MODE_WINDOW; |
|
34 |
} |
|
35 |
|
|
36 |
public void runAsTask(Object[] args) { |
|
37 |
// Los dialogos no se ejecutan en otro hilo. |
|
38 |
this.run(args); |
|
39 |
} |
|
40 |
|
|
41 |
public Object run(Object args[]) { |
|
42 |
ScriptingUIManager uimanager = ScriptingSwingLocator.getUIManager(); |
|
28 |
public DefaultScriptingDialog(ScriptingFolder parent, ScriptingManager manager, String id) { |
|
29 |
super(parent, ScriptingManager.UNIT_DIALOG, manager, id); |
|
30 |
this.setMainName("onload"); |
|
31 |
this.showMode = MODE_WINDOW; |
|
32 |
} |
|
43 | 33 |
|
44 |
JThinlet thinlet = uimanager.createJThinlet(); |
|
45 |
thinlet.addActionListener(this); |
|
46 |
thinlet.load(this.getFileResource(".dlg")); |
|
34 |
@Override |
|
35 |
public void runAsTask(Object[] args) { |
|
36 |
// Los dialogos no se ejecutan en otro hilo. |
|
37 |
this.run(args); |
|
38 |
} |
|
47 | 39 |
|
48 |
this.put("dialog", thinlet.getThinlet()); |
|
49 |
|
|
50 |
this.compile(); |
|
51 |
try { |
|
52 |
this.invokeFunction(this.getMainName(), args); |
|
53 |
} catch( ExecuteErrorException ex) { |
|
54 |
Throwable e = ex.getCause(); |
|
55 |
if( !(e instanceof NoSuchMethodException) ) { |
|
56 |
throw ex; |
|
57 |
} else { |
|
58 |
// When running from the composer messages of stdout shows in the console tab. |
|
59 |
System.out.println("Code of dialog do not has function '"+ this.getMainName()+"'."); |
|
60 |
} |
|
61 |
} |
|
40 |
@Override |
|
41 |
public Object run(Object args[]) { |
|
42 |
ScriptingUIManager uimanager = ScriptingSwingLocator.getUIManager(); |
|
62 | 43 |
|
63 |
switch(this.getShowMode()){ |
|
64 |
case MODE_WINDOW: |
|
65 |
// When running from the composer messages of stdout shows in the console tab. |
|
66 |
System.out.println("Showing dialog as window "+ this.getName()+"."); |
|
67 |
uimanager.showWindow(thinlet, this.getName()); |
|
68 |
break; |
|
69 |
case MODE_TOOL: |
|
70 |
// When running from the composer messages of stdout shows in the console tab. |
|
71 |
System.out.println("Showing dialog as tool "+ this.getName()+"."); |
|
72 |
uimanager.showTool(thinlet, this.getName()); |
|
73 |
break; |
|
74 |
default: |
|
75 |
// When running from the composer messages of stdout shows in the console tab. |
|
76 |
System.out.println("Showing dialog as dialog "+ this.getName()+"."); |
|
77 |
uimanager.showDialog(thinlet, this.getName()); |
|
78 |
break; |
|
79 |
} |
|
80 |
return null; |
|
81 |
} |
|
82 |
|
|
83 |
protected void loadInf(Ini prefs){ |
|
84 |
super.loadInf(prefs); |
|
85 |
this.showMode=Integer.parseInt(getInfValue(prefs,"Dialog","showMode",new Integer(MODE_WINDOW)).toString()); |
|
86 |
} |
|
44 |
JThinlet thinlet = uimanager.createJThinlet(); |
|
45 |
thinlet.addActionListener(this); |
|
46 |
thinlet.load(this.getFileResource(".dlg")); |
|
87 | 47 |
|
88 |
protected void save(Ini prefs){ |
|
89 |
super.save(prefs); |
|
90 |
prefs.put("Dialog","showMode", new Integer(this.showMode)); |
|
91 |
} |
|
92 |
|
|
93 |
public String[] getIconNames() { |
|
94 |
return new String[]{ |
|
95 |
"scripting_dialog", |
|
96 |
"scripting_dialog_open" |
|
97 |
}; |
|
98 |
} |
|
48 |
this.put("dialog", thinlet.getThinlet()); |
|
99 | 49 |
|
50 |
this.compile(); |
|
51 |
try { |
|
52 |
this.invokeFunction(this.getMainName(), args); |
|
53 |
} catch (ExecuteErrorException ex) { |
|
54 |
Throwable e = ex.getCause(); |
|
55 |
if (e instanceof NoSuchMethodException) { |
|
56 |
// When running from the composer messages of stdout shows in the console tab. |
|
57 |
System.out.println("Code of dialog do not has function '" + this.getMainName() + "'."); |
|
58 |
} else { |
|
59 |
throw ex; |
|
60 |
} |
|
61 |
} |
|
100 | 62 |
|
101 |
public void actionPerformed(ActionEvent arg0) { |
|
102 |
try { |
|
103 |
this.invokeFunction(arg0.getActionCommand(),null); |
|
104 |
} catch (Throwable e) { |
|
105 |
// Ignore, invokeFunction handle error it self. |
|
106 |
} |
|
107 |
} |
|
63 |
switch (this.getShowMode()) { |
|
64 |
case MODE_WINDOW: |
|
65 |
// When running from the composer messages of stdout shows in the console tab. |
|
66 |
System.out.println("Showing dialog as window " + this.getName() + "."); |
|
67 |
uimanager.showWindow(thinlet, this.getName()); |
|
68 |
break; |
|
69 |
case MODE_TOOL: |
|
70 |
// When running from the composer messages of stdout shows in the console tab. |
|
71 |
System.out.println("Showing dialog as tool " + this.getName() + "."); |
|
72 |
uimanager.showTool(thinlet, this.getName()); |
|
73 |
break; |
|
74 |
default: |
|
75 |
// When running from the composer messages of stdout shows in the console tab. |
|
76 |
System.out.println("Showing dialog as dialog " + this.getName() + "."); |
|
77 |
uimanager.showDialog(thinlet, this.getName()); |
|
78 |
break; |
|
79 |
} |
|
80 |
return null; |
|
81 |
} |
|
108 | 82 |
|
109 |
public File getDialogFile(){ |
|
110 |
return this.getFileResource(".dlg"); |
|
111 |
} |
|
112 |
|
|
113 |
public int getShowMode() { |
|
114 |
return this.showMode; |
|
115 |
} |
|
83 |
@Override |
|
84 |
protected void loadInf(Ini prefs) { |
|
85 |
super.loadInf(prefs); |
|
86 |
this.showMode = Integer.parseInt(getInfValue(prefs, "Dialog", "showMode", MODE_WINDOW).toString()); |
|
87 |
} |
|
116 | 88 |
|
89 |
@Override |
|
90 |
protected void save(Ini prefs) { |
|
91 |
super.save(prefs); |
|
92 |
prefs.put("Dialog", "showMode", this.showMode); |
|
93 |
} |
|
117 | 94 |
|
118 |
public void setShowMode(int mode) { |
|
119 |
this.showMode=mode; |
|
120 |
} |
|
95 |
@Override |
|
96 |
public String[] getIconNames() { |
|
97 |
return new String[]{ |
|
98 |
"scripting_dialog", |
|
99 |
"scripting_dialog_open" |
|
100 |
}; |
|
101 |
} |
|
121 | 102 |
|
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 |
} |
|
103 |
@Override |
|
104 |
public void actionPerformed(ActionEvent arg0) { |
|
105 |
try { |
|
106 |
this.invokeFunction(arg0.getActionCommand(), null); |
|
107 |
} catch (Throwable e) { |
|
108 |
// Ignore, invokeFunction handle error it self. |
|
109 |
} |
|
110 |
} |
|
135 | 111 |
|
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 |
} |
|
112 |
@Override |
|
113 |
public File getDialogFile() { |
|
114 |
return this.getFileResource(".dlg"); |
|
115 |
} |
|
116 |
|
|
117 |
@Override |
|
118 |
public int getShowMode() { |
|
119 |
return this.showMode; |
|
120 |
} |
|
121 |
|
|
122 |
@Override |
|
123 |
public void setShowMode(int mode) { |
|
124 |
this.showMode = mode; |
|
125 |
} |
|
126 |
|
|
127 |
@Override |
|
128 |
public boolean remove() { |
|
129 |
boolean r = super.remove(); |
|
130 |
File folder = this.getParent().getFile(); |
|
131 |
File f = new File(folder, this.getId() + ".dlg"); |
|
132 |
try { |
|
133 |
FileUtils.forceDelete(f); |
|
134 |
} catch (IOException e) { |
|
135 |
logger.warn("Can't remove dialog file '" + f.getAbsolutePath() + "'.", e); |
|
136 |
r = false; |
|
137 |
} |
|
138 |
return r; |
|
139 |
} |
|
140 |
|
|
141 |
@Override |
|
142 |
public void create(ScriptingFolder folder, String id, String language) { |
|
143 |
super.create(folder, id, language); |
|
144 |
|
|
145 |
File fileDlg = this.getResource(this.getId() + ".dlg"); |
|
146 |
try { |
|
147 |
fileDlg.createNewFile(); |
|
148 |
} catch (IOException e) { |
|
149 |
logger.warn("Can't create the dialog in '" + fileDlg.getAbsolutePath() + "'.", e); |
|
150 |
} |
|
151 |
|
|
152 |
// Escribimos en el fichero '.dlg' lo mi?nimo para que pueda ejecutarse |
|
153 |
Writer output; |
|
154 |
try { |
|
155 |
output = new BufferedWriter(new FileWriter(fileDlg)); |
|
156 |
output.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<panel/>"); |
|
157 |
output.close(); |
|
158 |
} catch (IOException e) { |
|
159 |
logger.warn("Can't write xml code of the dialog to '" + fileDlg.getAbsolutePath() + "'.", e); |
|
160 |
} |
|
161 |
|
|
162 |
this.save(); |
|
163 |
} |
|
164 |
|
|
165 |
@Override |
|
166 |
public String getMimeType() { |
|
167 |
return "text"; |
|
168 |
} |
|
158 | 169 |
} |
org.gvsig.scripting/trunk/org.gvsig.scripting/org.gvsig.scripting.lib/org.gvsig.scripting.lib.impl/src/main/java/org/gvsig/scripting/impl/ScriptingInstallerProvider.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
package org.gvsig.scripting.impl; |
|
24 |
|
|
25 |
import java.io.File; |
|
26 |
import java.io.IOException; |
|
27 |
import java.io.InputStream; |
|
28 |
|
|
29 |
import org.gvsig.installer.lib.api.PackageInfo; |
|
30 |
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException; |
|
31 |
import org.gvsig.installer.lib.spi.InstallPackageProviderServices; |
|
32 |
import org.gvsig.installer.lib.spi.InstallerProviderLocator; |
|
33 |
import org.gvsig.installer.lib.spi.InstallerProviderManager; |
|
34 |
import org.gvsig.installer.lib.spi.execution.InstallPackageProvider; |
|
35 |
import org.gvsig.scripting.ScriptingLocator; |
|
36 |
import org.gvsig.scripting.ScriptingManager; |
|
37 |
import org.gvsig.tools.service.spi.AbstractProvider; |
|
38 |
import org.gvsig.tools.service.spi.ProviderServices; |
|
39 |
|
|
40 |
public class ScriptingInstallerProvider extends AbstractProvider |
|
41 |
implements InstallPackageProvider { |
|
42 |
|
|
43 |
public ScriptingInstallerProvider(ProviderServices providerServices) { |
|
44 |
super(providerServices); |
|
45 |
} |
|
46 |
|
|
47 |
public void install(File applicationDirectory, InputStream inputStream, |
|
48 |
PackageInfo packageInfo) throws InstallPackageServiceException { |
|
49 |
|
|
50 |
ScriptingManager scriptingManager = ScriptingLocator.getManager(); |
|
51 |
|
|
52 |
File target = scriptingManager.getRootUserFolder(); |
|
53 |
|
|
54 |
try { |
|
55 |
InstallerProviderManager installerProviderManager = |
|
56 |
InstallerProviderLocator.getProviderManager(); |
|
57 |
InstallPackageProviderServices installerProviderServices = |
|
58 |
installerProviderManager.createInstallerProviderServices(); |
|
59 |
|
|
60 |
installerProviderServices.decompress(inputStream, target); |
|
61 |
|
|
62 |
} catch (Exception e) { |
|
63 |
throw new InstallPackageServiceException(e); |
|
64 |
} |
|
65 |
} |
|
66 |
|
|
67 |
public void installLater(File applicationDirectory, |
|
68 |
InputStream inputStream, PackageInfo packageInfo) |
|
69 |
throws InstallPackageServiceException, IOException { |
|
70 |
// TODO Auto-generated method stub |
|
71 |
|
|
72 |
} |
|
73 |
} |
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 | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.io.File; |
4 | 4 |
import java.io.IOException; |
5 |
import java.io.InputStream; |
|
5 | 6 |
import java.util.List; |
6 |
import java.util.Map; |
|
7 | 7 |
|
8 | 8 |
import javax.script.Compilable; |
9 | 9 |
import javax.script.CompiledScript; |
... | ... | |
13 | 13 |
|
14 | 14 |
import org.apache.commons.io.FileUtils; |
15 | 15 |
import org.apache.commons.io.FilenameUtils; |
16 |
import org.apache.commons.io.IOUtils; |
|
17 |
import org.apache.commons.lang3.StringUtils; |
|
16 | 18 |
import org.gvsig.scripting.CompileErrorException; |
17 | 19 |
import org.gvsig.scripting.ExecuteErrorException; |
18 | 20 |
import org.gvsig.scripting.ScriptingBaseScript; |
... | ... | |
29 | 31 |
import org.slf4j.LoggerFactory; |
30 | 32 |
|
31 | 33 |
public class DefaultScriptingScript extends AbstractScript implements |
32 |
ScriptingScript { |
|
33 |
|
|
34 |
private static final Logger logger = LoggerFactory.getLogger(DefaultScriptingScript.class); |
|
35 |
protected String langName; |
|
36 |
protected String extension = null; |
|
37 |
protected ScriptEngine engine = null; |
|
38 |
protected CompiledScript compiledCode; |
|
34 |
ScriptingScript { |
|
39 | 35 |
|
40 |
private String code = null; |
|
41 |
private String mainName = "main"; |
|
42 |
private boolean saved; |
|
43 |
private final DelegateWeakReferencingObservable delegatedObservable; |
|
36 |
private static final Logger logger = LoggerFactory.getLogger(DefaultScriptingScript.class); |
|
37 |
protected String langName; |
|
38 |
protected String extension = null; |
|
39 |
protected ScriptEngine engine = null; |
|
40 |
protected CompiledScript compiledCode; |
|
44 | 41 |
|
45 |
protected DefaultScriptingScript(ScriptingFolder parent, String typename, ScriptingManager manager, String id) { |
|
46 |
super(parent, typename, manager, id); |
|
47 |
this.setLangName("python"); |
|
48 |
this.setSaved(true); |
|
49 |
this.delegatedObservable = new DelegateWeakReferencingObservable(this); |
|
50 |
} |
|
51 |
|
|
52 |
public DefaultScriptingScript(ScriptingFolder parent, ScriptingManager manager, String id) { |
|
53 |
this(parent, ScriptingManager.UNIT_SCRIPT, manager, id); |
|
54 |
} |
|
55 |
|
|
56 |
public ScriptingUnit load(String name) { |
|
57 |
ScriptingUnit script = null; |
|
58 |
File f = null; |
|
59 |
String scname = name; |
|
60 |
if (!scname.endsWith(".inf")) { |
|
61 |
scname = scname + ".inf"; |
|
62 |
} |
|
63 |
if (scname.startsWith("/")) { |
|
64 |
f = new File(scname.substring(1)); |
|
65 |
} else { |
|
66 |
f = new File(this.getParent().getFile(), scname); |
|
67 |
if (!f.exists()) { |
|
68 |
return null; |
|
69 |
} |
|
70 |
} |
|
71 |
try { |
|
72 |
script = this.getManager().getScript(f); |
|
73 |
} catch (ScriptNotFoundException ex) { |
|
74 |
return null; |
|
75 |
} |
|
76 |
if (script != null) { |
|
77 |
this.getEngine().put(script.getId(), script); |
|
78 |
} |
|
79 |
return script; |
|
80 |
} |
|
42 |
private String code = null; |
|
43 |
private String mainName = "main"; |
|
44 |
private boolean saved; |
|
45 |
private final DelegateWeakReferencingObservable delegatedObservable; |
|
81 | 46 |
|
82 |
public Object __getattr__(String name) { |
|
83 |
ScriptEngine engine = this.getEngine(); |
|
84 |
this.compile(); |
|
85 |
return engine.get(name); |
|
86 |
} |
|
47 |
protected DefaultScriptingScript(ScriptingFolder parent, String typename, ScriptingManager manager, String id) { |
|
48 |
super(parent, typename, manager, id); |
|
49 |
this.setLangName("python"); |
|
50 |
this.setSaved(true); |
|
51 |
this.delegatedObservable = new DelegateWeakReferencingObservable(this); |
|
52 |
} |
|
87 | 53 |
|
88 |
public void __setattr__(String name, Object value) { |
|
89 |
ScriptEngine engine = this.getEngine(); |
|
90 |
this.compile(); |
|
91 |
engine.put(name,value); |
|
92 |
} |
|
93 |
|
|
94 |
public Object __call__() { |
|
95 |
return this.run(); |
|
96 |
} |
|
97 |
|
|
98 |
public Object __call__(Object[] args) { |
|
99 |
return this.run(args); |
|
100 |
} |
|
101 |
|
|
102 |
protected void notifyErrors(Exception exception, String command) { |
|
103 |
this.delegatedObservable.notifyObservers(new BaseScriptingNotifycation( |
|
104 |
this, BaseScriptingNotifycation.RUNTIME_ERROR_NOTIFICATION, |
|
105 |
command, exception)); |
|
106 |
} |
|
54 |
public DefaultScriptingScript(ScriptingFolder parent, ScriptingManager manager, String id) { |
|
55 |
this(parent, ScriptingManager.UNIT_SCRIPT, manager, id); |
|
56 |
} |
|
107 | 57 |
|
108 |
public void setSaved(boolean saved) { |
|
109 |
this.saved = saved; |
|
110 |
} |
|
58 |
public Object __getattr__(String name) { |
|
59 |
ScriptEngine engine = this.getEngine(); |
|
60 |
this.compile(); |
|
61 |
return engine.get(name); |
|
62 |
} |
|
111 | 63 |
|
112 |
public String getCode() { |
|
113 |
if (this.code == null) { |
|
114 |
File f=null; |
|
115 |
try { |
|
116 |
f = this.getFileResource(this.extension); |
|
117 |
this.code = FileUtils.readFileToString(f); |
|
118 |
} catch (IOException e) { |
|
119 |
String fname = (f==null)? "(null)":f.getAbsolutePath(); |
|
120 |
logger.warn("Can't load code from file '"+fname+"'."); |
|
121 |
} |
|
122 |
} |
|
123 |
return this.code; |
|
124 |
} |
|
64 |
public void __setattr__(String name, Object value) { |
|
65 |
ScriptEngine engine = this.getEngine(); |
|
66 |
this.compile(); |
|
67 |
engine.put(name, value); |
|
68 |
} |
|
125 | 69 |
|
126 |
public void setCode(String code) { |
|
127 |
this.code = code; |
|
128 |
this.engine = null; |
|
129 |
this.compiledCode = null; |
|
130 |
this.setSaved(false); |
|
131 |
} |
|
70 |
public Object __call__() { |
|
71 |
return this.run(); |
|
72 |
} |
|
132 | 73 |
|
133 |
private String getPreparedCode() {
|
|
134 |
return this.getCode();
|
|
135 |
}
|
|
74 |
public Object __call__(Object[] args) {
|
|
75 |
return this.run(args);
|
|
76 |
}
|
|
136 | 77 |
|
137 |
protected String getCodeToInitializeEngine() { |
|
138 |
if( "python".equalsIgnoreCase(this.getLangName()) ) { |
|
139 |
List<File> libFolders = this.manager.getLibFolders(); |
|
140 |
if (libFolders == null || libFolders.size() < 1) { |
|
141 |
return null; |
|
142 |
} |
|
143 |
StringBuffer buffer = new StringBuffer(); |
|
144 |
buffer.append("import sys\n"); |
|
145 |
for (File file : libFolders) { |
|
146 |
buffer.append("sys.path.append(r'"); |
|
147 |
buffer.append(file.getAbsolutePath()); |
|
148 |
buffer.append("')\n"); |
|
149 |
} |
|
150 |
buffer.append("load = script.load\n"); |
|
151 |
return buffer.toString(); |
|
152 |
} |
|
153 |
return null; |
|
154 |
} |
|
155 |
|
|
156 |
protected ScriptEngine getEngine() { |
|
157 |
if (this.engine == null) { |
|
158 |
ScriptEngine scriptEngine = this.manager.getEngine(this |
|
159 |
.getLangName()); |
|
160 |
scriptEngine.put("script", this); |
|
161 |
this.engine = scriptEngine; |
|
162 |
try { |
|
163 |
String code = this.getCodeToInitializeEngine(); |
|
164 |
if( code != null ) { |
|
165 |
this.engine.eval(code); |
|
166 |
} |
|
167 |
} catch(Exception ex) { |
|
168 |
logger.warn("Can't initialize engine with the code:\n"+code); |
|
169 |
} |
|
170 |
} |
|
171 |
return this.engine; |
|
172 |
} |
|
78 |
protected void notifyErrors(Exception exception, String command) { |
|
79 |
this.delegatedObservable.notifyObservers(new BaseScriptingNotifycation( |
|
80 |
this, BaseScriptingNotifycation.RUNTIME_ERROR_NOTIFICATION, |
|
81 |
command, exception)); |
|
82 |
} |
|
173 | 83 |
|
174 |
protected void loadInf(Ini prefs) { |
|
175 |
super.loadInf(prefs); |
|
84 |
@Override |
|
85 |
public void setSaved(boolean saved) { |
|
86 |
this.saved = saved; |
|
87 |
} |
|
176 | 88 |
|
177 |
this.setMainName((String) getInfValue(prefs, "Script", "main", "main")); |
|
178 |
this.setLangName((String) getInfValue(prefs, "Script", "Lang", |
|
179 |
this.getLangName())); |
|
180 |
} |
|
89 |
@Override |
|
90 |
public String getCode() { |
|
91 |
if (this.code == null) { |
|
92 |
File f = null; |
|
93 |
try { |
|
94 |
f = this.getFileResource(this.extension); |
|
95 |
this.code = FileUtils.readFileToString(f); |
|
96 |
} catch (IOException e) { |
|
97 |
String fname = (f == null) ? "(null)" : f.getAbsolutePath(); |
|
98 |
logger.warn("Can't load code from file '" + fname + "'."); |
|
99 |
} |
|
100 |
} |
|
101 |
return this.code; |
|
102 |
} |
|
181 | 103 |
|
182 |
public void load(ScriptingFolder folder, String id) { |
|
183 |
this.setId(id); |
|
184 |
this.setParent(folder); |
|
104 |
@Override |
|
105 |
public void setCode(String code) { |
|
106 |
this.code = code; |
|
107 |
this.engine = null; |
|
108 |
this.compiledCode = null; |
|
109 |
this.setSaved(false); |
|
110 |
} |
|
185 | 111 |
|
186 |
Map<String, String> languages = this.manager |
|
187 |
.getSupportedLanguagesByExtension(); |
|
188 |
String extension = FilenameUtils.getExtension(id); |
|
189 |
String langName = languages.get(extension); |
|
190 |
this.setLangName(langName); |
|
191 |
this.setExtension(extension); |
|
112 |
protected String getCodeToInitializeEngine() { |
|
113 |
String name = "org/gvsig/scripting/langs/"+this.getLangName().toLowerCase()+"/init.txt"; |
|
114 |
try { |
|
115 |
InputStream template = this.getClass().getClassLoader().getResourceAsStream(name); |
|
116 |
if( template == null ) { |
|
117 |
return null; |
|
118 |
} |
|
119 |
List<String> lines = IOUtils.readLines(template); |
|
120 |
return StringUtils.join(lines, "\n"); |
|
121 |
} catch (Exception ex) { |
|
122 |
logger.warn("Can't load code to initialize the script from '"+name+".",ex); |
|
123 |
return null; |
|
124 |
} |
|
125 |
} |
|
192 | 126 |
|
193 |
File f = getFileResource(".inf"); |
|
194 |
if (f.isFile()) { |
|
195 |
Ini prefs = null; |
|
196 |
try { |
|
197 |
prefs = new Ini(f); |
|
198 |
} catch (Exception e) { |
|
199 |
logger.warn("Can't load 'inf' file '"+f.getAbsolutePath()+"'.",e); |
|
200 |
} |
|
201 |
loadInf(prefs); |
|
202 |
} |
|
203 |
this.setSaved(true); |
|
204 |
} |
|
127 |
protected ScriptEngine getEngine() { |
|
128 |
if (this.engine == null) { |
|
129 |
ScriptEngine scriptEngine = this.manager.getEngineByLanguage(langName); |
|
130 |
scriptEngine.put("script", this); |
|
131 |
this.engine = scriptEngine; |
|
132 |
String code = this.getCodeToInitializeEngine(); |
|
133 |
if (code != null) { |
|
134 |
try { |
|
135 |
this.engine.eval(code); |
|
136 |
} catch (Exception ex) { |
|
137 |
logger.warn("Can't initialize engine with the code:\n" + code, ex); |
|
138 |
} |
|
139 |
} |
|
140 |
} |
|
141 |
return this.engine; |
|
142 |
} |
|
205 | 143 |
|
206 |
public void save() { |
|
207 |
File f = getFileResource(".inf"); |
|
208 |
if (!f.isFile()) { |
|
209 |
try { |
|
210 |
f.createNewFile(); |
|
211 |
} catch (Exception e) { |
|
212 |
logger.warn("Can't create 'inf' file '"+f.getAbsolutePath()+"'.",e); |
|
213 |
} |
|
214 |
} |
|
215 |
Ini prefs = null; |
|
216 |
try { |
|
217 |
prefs = new Ini(f); |
|
218 |
} catch (Exception e) { |
|
219 |
logger.warn("Can't load 'inf' file '"+f.getAbsolutePath()+"'.",e); |
|
220 |
} |
|
221 |
save(prefs); |
|
222 |
// Guardo el codigo en el fichero |
|
223 |
File fcode = null; |
|
224 |
try { |
|
225 |
fcode = this.getFileResource(this.getExtension()); |
|
226 |
FileUtils.write(fcode, this.getCode()); |
|
227 |
} catch (Exception e) { |
|
228 |
logger.warn("Can't write code to file '"+fcode.getAbsolutePath()+"'.",e); |
|
229 |
} |
|
230 |
this.setSaved(true); |
|
231 |
} |
|
144 |
@Override |
|
145 |
protected void loadInf(Ini prefs) { |
|
146 |
super.loadInf(prefs); |
|
232 | 147 |
|
233 |
protected void save(Ini prefs) { |
|
234 |
super.save(prefs); |
|
235 |
prefs.put("Script", "main", this.getMainName()); |
|
236 |
prefs.put("Script", "Lang", this.getLangName()); |
|
237 |
try { |
|
238 |
prefs.store(); |
|
239 |
} catch (IOException e) { |
|
240 |
String fname = (prefs.getFile()==null)? "(null)" : prefs.getFile().getAbsolutePath(); |
|
241 |
logger.warn("Can't save inf file ("+fname+").",e); |
|
242 |
} |
|
148 |
this.setMainName((String) getInfValue(prefs, "Script", "main", "main")); |
|
149 |
this.setLangName((String) getInfValue(prefs, "Script", "Lang", |
|
150 |
this.getLangName())); |
|
151 |
} |
|
243 | 152 |
|
244 |
} |
|
153 |
@Override |
|
154 |
public void load(ScriptingFolder folder, String id) { |
|
155 |
this.setId(id); |
|
156 |
this.setParent(folder); |
|
245 | 157 |
|
246 |
public String getLangName() { |
|
247 |
return this.langName; |
|
248 |
} |
|
158 |
String extension = FilenameUtils.getExtension(id); |
|
159 |
if( extension != null ) { |
|
160 |
String language = this.manager.getLanguageOfExtension(extension); |
|
161 |
if( language != null ) { |
|
162 |
this.setLangName(language); |
|
163 |
} |
|
164 |
this.setExtension(extension); |
|
165 |
} |
|
166 |
File f = getFileResource(".inf"); |
|
167 |
if (f.isFile()) { |
|
168 |
Ini prefs = null; |
|
169 |
try { |
|
170 |
prefs = new Ini(f); |
|
171 |
} catch (Exception e) { |
|
172 |
logger.warn("Can't load 'inf' file '" + f.getAbsolutePath() + "'.", e); |
|
173 |
} |
|
174 |
loadInf(prefs); |
|
175 |
} |
|
176 |
this.setSaved(true); |
|
177 |
} |
|
249 | 178 |
|
250 |
protected void setLangName(final String langName) { |
|
251 |
this.langName = langName; |
|
252 |
this.extension = this.manager.getExtensionByLanguage(this.langName); |
|
253 |
} |
|
179 |
@Override |
|
180 |
public void save() { |
|
181 |
File f = getFileResource(".inf"); |
|
182 |
if (!f.isFile()) { |
|
183 |
try { |
|
184 |
f.createNewFile(); |
|
185 |
} catch (Exception e) { |
|
186 |
logger.warn("Can't create 'inf' file '" + f.getAbsolutePath() + "'.", e); |
|
187 |
} |
|
188 |
} |
|
189 |
Ini prefs = null; |
|
190 |
try { |
|
191 |
prefs = new Ini(f); |
|
192 |
} catch (Exception e) { |
|
193 |
logger.warn("Can't load 'inf' file '" + f.getAbsolutePath() + "'.", e); |
|
194 |
} |
|
195 |
save(prefs); |
|
196 |
// Guardo el codigo en el fichero |
|
197 |
File fcode = this.getFileResource(this.getExtension()); |
|
198 |
try { |
|
199 |
FileUtils.write(fcode, this.getCode()); |
|
200 |
} catch (Exception e) { |
|
201 |
logger.warn("Can't write code to file '" + fcode.getAbsolutePath() + "'.", e); |
|
202 |
} |
|
203 |
this.setSaved(true); |
|
204 |
} |
|
254 | 205 |
|
255 |
public String[] getIconNames() { |
|
256 |
return new String[] { |
|
257 |
"scripting_" + this.getLangName().toLowerCase(), |
|
258 |
"scripting_" + this.getLangName().toLowerCase() + "_open" |
|
259 |
}; |
|
260 |
} |
|
206 |
@Override |
|
207 |
protected void save(Ini prefs) { |
|
208 |
super.save(prefs); |
|
209 |
prefs.put("Script", "main", this.getMainName()); |
|
210 |
prefs.put("Script", "Lang", this.getLangName()); |
|
211 |
try { |
|
212 |
prefs.store(); |
|
213 |
} catch (IOException e) { |
|
214 |
String fname = (prefs.getFile() == null) ? "(null)" : prefs.getFile().getAbsolutePath(); |
|
215 |
logger.warn("Can't save inf file (" + fname + ").", e); |
|
216 |
} |
|
261 | 217 |
|
262 |
public String getMainName() { |
|
263 |
return this.mainName; |
|
264 |
} |
|
218 |
} |
|
265 | 219 |
|
266 |
public void setMainName(final String mainName) { |
|
267 |
this.mainName = mainName; |
|
268 |
} |
|
220 |
@Override |
|
221 |
public String getLangName() { |
|
222 |
return this.langName; |
|
223 |
} |
|
269 | 224 |
|
270 |
public String getExtension() { |
|
271 |
return this.extension; |
|
272 |
} |
|
225 |
protected void setLangName(final String langName) { |
|
226 |
if( langName == null ) { |
|
227 |
return; |
|
228 |
} |
|
229 |
this.langName = langName; |
|
230 |
this.setExtension(this.manager.getExtensionOfLanguage(this.langName)); |
|
231 |
} |
|
273 | 232 |
|
274 |
public void setExtension(final String extension) {
|
|
275 |
if( !extension.startsWith(".") ) {
|
|
276 |
this.extension = "." + extension;
|
|
277 |
} else {
|
|
278 |
this.extension = extension;
|
|
279 |
}
|
|
280 |
}
|
|
233 |
@Override
|
|
234 |
public String[] getIconNames() {
|
|
235 |
return new String[]{
|
|
236 |
"scripting_" + this.getLangName().toLowerCase(),
|
|
237 |
"scripting_" + this.getLangName().toLowerCase() + "_open"
|
|
238 |
};
|
|
239 |
}
|
|
281 | 240 |
|
282 |
public boolean isSaved() { |
|
283 |
return this.saved; |
|
284 |
} |
|
241 |
@Override |
|
242 |
public String getMainName() { |
|
243 |
return this.mainName; |
|
244 |
} |
|
285 | 245 |
|
286 |
public void addObserver(final Observer o) { |
|
287 |
this.delegatedObservable.addObserver(o); |
|
288 |
} |
|
246 |
@Override |
|
247 |
public void setMainName(final String mainName) { |
|
248 |
this.mainName = mainName; |
|
249 |
} |
|
289 | 250 |
|
290 |
public void deleteObserver(final Observer o) {
|
|
291 |
this.delegatedObservable.deleteObserver(o);
|
|
292 |
}
|
|
251 |
public String getExtension() {
|
|
252 |
return this.extension;
|
|
253 |
}
|
|
293 | 254 |
|
294 |
public void deleteObservers() { |
|
295 |
this.delegatedObservable.deleteObservers(); |
|
296 |
} |
|
255 |
public void setExtension(final String extension) { |
|
256 |
if (!extension.startsWith(".")) { |
|
257 |
this.extension = "." + extension; |
|
258 |
} else { |
|
259 |
this.extension = extension; |
|
260 |
} |
|
261 |
} |
|
297 | 262 |
|
298 |
public void put(final String name, final Object value) { |
|
299 |
this.getEngine().put(name, value); |
|
300 |
} |
|
263 |
@Override |
|
264 |
public boolean isSaved() { |
|
265 |
return this.saved; |
|
266 |
} |
|
301 | 267 |
|
302 |
public void compile() { |
|
303 |
if (this.compiledCode == null) { |
|
304 |
ScriptEngine engine = this.getEngine(); |
|
305 |
if (engine instanceof Compilable) { |
|
306 |
try { |
Also available in: Unified diff