Parent: [r3] (diff)

Child: [r22] (diff)

Download this file

Script.java    306 lines (257 with data), 11.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* This class allows running custom scripts using Beanshell as interpreter for
* the commands. The goal is to provide direct access to most of the features
* within the builder without need to implement them manually.
*/
package main;
import bsh.EvalError;
import bsh.Interpreter;
import definitions.is;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
import script.FileExtension;
import script.License;
import script.Plugin;
//import script.log;
/**
*
* @author Nuno Brito, 21st of December 2012 in Darmstadt, Germany.
*/
public class Script {
// general place to hold to retrieve settings
HashMap<String, Object> settings = new HashMap<String, Object>();
// StudioUI2 UI = core.studio;
public Script(Properties settings){
}
public Script() {
}
/**
* Runs a script from the disk, the script must be based on Beanshell
* syntax
* @param scriptFile File that contains the script instructions
* @param what Method that will be run inside the script
* @param className The type class that is expected on the script
*/
public void run(File scriptFile, String what, String className){
// is this a java-like beanshell?
if(scriptFile.getName().contains(".java")){
runJava(scriptFile, what, className);
return;
}
// shall we interpret this file?
if(scriptFile.exists()==false){
script.log.write(is.NOTFOUND, "Couldn't find the mentioned script"
+ " at %1", scriptFile.getAbsolutePath());
return;
}
try {// let's start
// the bean shell instance with our settings
Interpreter runScript = new Interpreter();
// prepare the interpreted script with our imports and settings
addDefinitions(runScript, scriptFile);
// Interpret the settings file
runScript.source(scriptFile.getAbsolutePath());
// get the objects back from running
settings = (HashMap<String, Object>) runScript.get("settings");
//log = (Log) runScript.get("result");
// run the referred method at the settings file
if(what != null){
runScript.eval(what + "();");
}
} catch (EvalError e){
script.log.write(is.ERROR, "Error while interpreting %1, the "
+ "error message is: %2"
, scriptFile.getAbsolutePath(), e.getLocalizedMessage());
// e.printStackTrace();
} catch (IOException e) {
script.log.write(is.ERROR, "Error while interpreting %1, the "
+ "error message is: %2"
, scriptFile.getAbsolutePath(), e.getLocalizedMessage());
// e.printStackTrace();
}
// all done.
// all done.
}
/**
* Run the beanshell script without specifying a method
*/
// public void run(File Script){
// run(Script, null);
// }
/**
* Get the general properties object. Useful for overriding the default
* settings with values added on the script
* @return
*/
public HashMap<String, Object> getSettings() {
return settings;
}
public void setSettings(HashMap<String, Object> settings) {
this.settings = settings;
}
void addDefinitions(Interpreter runScript, File scriptFile)
throws EvalError{
// inject our parameters object
//runScript.set("log", log);
runScript.set("temp", core.temp);
runScript.set("settings", settings);
runScript.set("thisFile", scriptFile);
runScript.set("thisDir", scriptFile.getParentFile());
//runScript.set("list", core.studio.getList());
runScript.eval(
"import main.core;"
+ "import script.log;"
+ "import definitions.is;"
+ "import definitions.Messages;"
+ "import java.io.File;"
+ "import javax.swing.JEditorPane;"
+ "import javax.swing.JList;"
+ "import javax.swing.JTree;"
+ "import utils.SortedListModel;"
+ "import GUI.plugins;"
+ "import GUI.ListItem;"
+ "import GUI.swingUtils;"
+ "import GUI.StudioUI2;"
+ "import GUI.Actions;"
+ "import www.WebRequest;"
);
}
/**
* A beanshell script adapted for being edited like a Java file
* @param scriptFile The file where the beanshell script is located
* @param methodName The method that we want to run
* @param className Java-like type of class used by the script
*/
public void runJava(File scriptFile, String methodName, String className) {
// shall we interpret this file?
if(scriptFile.exists()==false){
script.log.write(is.NOTFOUND, "SC23 - Couldn't find script file"
+ " %1", scriptFile.getAbsolutePath());
return;
}
// read the text file
String rawText = utils.files.readAsString(scriptFile);
String[] lines = rawText.split("\n");
String codeText = "";
boolean // used for speeding up line interpretation
noPackage = true,
noClass = true;
// iterate each line
for(String line : lines){
// if(line.contains("//<---")){
// continue;
// }
// overrides are not accepted by beanshell language
if(line.contains("@Override")){
codeText = codeText.concat("\n");
continue;
}
if(noPackage)
if(line.startsWith("package ")){
noPackage = false;
codeText = codeText.concat("\n");
continue;
}
// replace the class statement with an object statement
if(noClass){
if((line.startsWith("public class "))){
line =
className +" plugin = new " + className + "(){"
+ "File thisFile = global.thisFile;"
+ "File thisFolder = global.thisFolder;"
;
noClass = false;
}
}
// add this line, along with a line break since they were removed
codeText = codeText.concat(line + "\n");
}
// need to add a ; in the end of this plugin object
codeText = codeText + ";";
try {// let's start
// the bean shell instance with our settings
Interpreter runScript = new Interpreter();
//core.thisFile = scriptFile;
addDefinitions(runScript, scriptFile);
runScript.set("global.thisFile", scriptFile);
runScript.set("global.thisFolder", scriptFile.getParentFile());
runScript.set("temp", core.temp);
runScript.set("settings", settings);
// runScript.set("thisFile", scriptFile);
// runScript.set("thisDir", scriptFile.getParentFile());
// do the intrepertation of our script
doInterpretation
(codeText, className, scriptFile, methodName, runScript);
// // run our modified text
// runScript.eval(codeText);
//
// Plugin plugin = (Plugin) runScript.get("plugin");
//
// // if no method is specified, run the initial method
// if(methodName == null){
// plugin.startup();
// }else{
// // otherwise, run the requested method
// runScript.eval("plugin." + methodName + "();");
// }
} catch (Exception e){
script.log.write(is.ERROR, "Error while interpreting %1, the "
+ "error message is: %2"
, scriptFile.getAbsolutePath(), e.getLocalizedMessage());
e.printStackTrace();
}
// all done.
}
/**
* This method is used by runJava() to ease the processing between different
* types of plugin, licenses and whatever else comes in the future
* @param className The type of class that we are processing
* @param runScript The instance of the script
*/
void doInterpretation(String sourceCode, String className,
File scriptFile, String methodName, Interpreter runScript) throws Exception{
// run the code
runScript.eval(sourceCode);
// react to the case that this is a normal plugin
if(className.equals(is.plugin)){
// extract the plugin object from the script
Plugin plugin = (Plugin) runScript.get("plugin");
// if no method is specified, run the initial method
if(methodName == null){
plugin.startup();
return;
}
// special consideration for special commands
if(methodName.contains("=")){
// run whatever is requested here
runScript.eval(methodName);
return;
}
else{
// otherwise, just run the requested method
runScript.eval("plugin." + methodName + "();");
// no need to continue
return;
}
}
// are we processing a license?
if(className.equals(is.license)){
// get the object
License plugin = (License) runScript.get("plugin");
// add it up
core.licenses.add(plugin);
script.log.write(is.COMPLETED, "Added license plugin: %1",
scriptFile.getName());
}
// is this a file extension that we want to archive?
if(className.equals(is.extension)){
// get the object
FileExtension extension = (FileExtension) runScript.get("plugin");
// add it up
core.extensions.add(extension);
}
// all done
}
}