1 package cz.cuni.amis.pogamut.ut2004.bot.sposh;
2
3 import java.io.FileReader;
4 import java.io.IOException;
5 import java.io.Reader;
6 import javax.script.ScriptEngine;
7 import javax.script.ScriptEngineManager;
8
9 /**
10 * This class is base class for users who program their agents in some scripting
11 * language and have thir file ready to use. Override {@link FileScriptedAgent.getScriptFile()}
12 * to specify the script that will be loaded.
13 * <p>
14 * To subclass this class, override {@link getScriptFile()}, {@link doLogic()} and
15 * other necessary methods. Use callFunction method to call scripted function
16 * from specified script.
17 *
18 * @author Honza
19 */
20 @Deprecated
21 public abstract class FileScriptLogic extends StreamScriptLogic {
22
23 /**
24 * Create a {@link ScriptEngine} according to extension of script specified in
25 * {@link getScriptFile()}
26 * @param manager
27 * @return Correct script engine for script file.
28 */
29 @Override
30 protected ScriptEngine createScriptEngine(ScriptEngineManager manager) {
31 String scriptExtension = getExt(getScriptFile());
32
33 return manager.getEngineByExtension(scriptExtension);
34 }
35
36 /**
37 * Get stream of file from script file specified in {@link FileScriptedAgent.getScriptFile()}.
38 * @return
39 * @throws IOException If some problem occurs with getting stream from script file.
40 */
41 @Override
42 protected Reader getScriptStream() throws IOException {
43 return new FileReader(getScriptFile());
44 }
45
46 /**
47 * Return path to the script file.
48 * Absolute path if possible, because I am not sure about relative path.
49 * TODO: some reliable way to get root directroy of project
50 * @return Return path
51 */
52 protected abstract String getScriptFile();
53
54 /**
55 * Get extension of
56 * @param path
57 * @return extension or "" if no extension found
58 */
59 protected String getExt(String path) {
60 int separatorIndex = path.lastIndexOf(System.getProperty("file.separator"));
61 String filename = (separatorIndex == -1 ? path : path.substring(separatorIndex + 1));
62
63 System.out.println("filename is "+filename);
64 return (filename.lastIndexOf('.') == -1 ? "" : filename.substring(filename.lastIndexOf('.') + 1));
65 }
66 }