001: package bsh.engine;
002:
003: import javax.script.*;
004: import java.util.List;
005: import java.util.Arrays;
006:
007: public class BshScriptEngineFactory implements
008: javax.script.ScriptEngineFactory {
009: // Begin impl ScriptEnginInfo
010:
011: final List<String> extensions = Arrays.asList("bsh", "java");
012:
013: final List<String> mimeTypes = Arrays.asList(
014: "application/x-beanshell", "application/x-bsh",
015: "application/x-java-source");
016:
017: final List<String> names = Arrays
018: .asList("beanshell", "bsh", "java");
019:
020: public String getEngineName() {
021: return "BeanShell Engine";
022: }
023:
024: public String getEngineVersion() {
025: return "1.0";
026: }
027:
028: public List<String> getExtensions() {
029: return extensions;
030: }
031:
032: public List<String> getMimeTypes() {
033: return mimeTypes;
034: }
035:
036: public List<String> getNames() {
037: return names;
038: }
039:
040: public String getLanguageName() {
041: return "BeanShell";
042: }
043:
044: public String getLanguageVersion() {
045: return bsh.Interpreter.VERSION + "";
046: }
047:
048: public Object getParameter(String param) {
049: if (param.equals(ScriptEngine.ENGINE))
050: return getEngineName();
051: if (param.equals(ScriptEngine.ENGINE_VERSION))
052: return getEngineVersion();
053: if (param.equals(ScriptEngine.NAME))
054: return getEngineName();
055: if (param.equals(ScriptEngine.LANGUAGE))
056: return getLanguageName();
057: if (param.equals(ScriptEngine.LANGUAGE_VERSION))
058: return getLanguageVersion();
059: if (param.equals("THREADING"))
060: return "MULTITHREADED";
061:
062: return null;
063: }
064:
065: public String getMethodCallSyntax(String objectName,
066: String methodName, String... args) {
067: // Note: this is very close to the bsh.StringUtil.methodString()
068: // method, which constructs a method signature from arg *types*. Maybe
069: // combine these later.
070:
071: StringBuffer sb = new StringBuffer();
072: if (objectName != null)
073: sb.append(objectName + ".");
074: sb.append(methodName + "(");
075: if (args.length > 0)
076: sb.append(" ");
077: for (int i = 0; i < args.length; i++)
078: sb.append(((args[i] == null) ? "null" : args[i])
079: + (i < (args.length - 1) ? ", " : " "));
080: sb.append(")");
081: return sb.toString();
082: }
083:
084: public String getOutputStatement(String message) {
085: return "print( \"" + message + "\" );";
086: }
087:
088: public String getProgram(String... statements) {
089: StringBuffer sb = new StringBuffer();
090: for (int i = 0; i < statements.length; i++) {
091: sb.append(statements[i]);
092: if (!statements[i].endsWith(";"))
093: sb.append(";");
094: sb.append("\n");
095: }
096: return sb.toString();
097: }
098:
099: // End impl ScriptEngineInfo
100:
101: // Begin impl ScriptEngineFactory
102:
103: public ScriptEngine getScriptEngine() {
104: return new BshScriptEngine();
105: }
106:
107: // End impl ScriptEngineFactory
108: }
|