All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.obermuhlner.scriptengine.jshell.JShellScriptEngineFactory Maven / Gradle / Ivy

The newest version!
package ch.obermuhlner.scriptengine.jshell;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import java.util.Arrays;
import java.util.List;

/**
 * Factory for {@link JShellScriptEngine}.
 */
public class JShellScriptEngineFactory implements ScriptEngineFactory {
    @Override
    public String getEngineName() {
        return "JShell ScriptEngine";
    }

    @Override
    public String getEngineVersion() {
        return "1.1.0";
    }

    @Override
    public List getExtensions() {
        return Arrays.asList("jsh", "jshell");
    }

    @Override
    public List getMimeTypes() {
        return Arrays.asList("text/x-jshell-source");
    }

    @Override
    public List getNames() {
        return Arrays.asList("JShell", "jshell", "ch.obermuhlner:jshell-scriptengine", "obermuhlner-jshell");
    }

    @Override
    public String getLanguageName() {
        return "JShell";
    }

    @Override
    public String getLanguageVersion() {
        return System.getProperty("java.version");
    }

    @Override
    public Object getParameter(String key) {
        switch (key) {
            case ScriptEngine.ENGINE:
                return getEngineName();
            case ScriptEngine.ENGINE_VERSION:
                return getEngineVersion();
            case ScriptEngine.LANGUAGE:
                return getLanguageName();
            case ScriptEngine.LANGUAGE_VERSION:
                return getLanguageVersion();
            case ScriptEngine.NAME:
                return getNames().get(0);
            default:
                return null;
        }
    }

    @Override
    public String getMethodCallSyntax(String obj, String method, String... args) {
        StringBuilder s = new StringBuilder();
        s.append(obj);
        s.append(".");
        s.append(method);
        s.append("(");
        for(int i = 0; i < args.length; i++) {
            if (i > 0) {
                s.append(",");
            }
            s.append(args[i]);
        }
        s.append(")");
        return s.toString();
    }

    @Override
    public String getOutputStatement(String toDisplay) {
        return "System.out.println(" + toDisplay + ")";
    }

    @Override
    public String getProgram(String... statements) {
        StringBuilder s = new StringBuilder();
        for(String statement : statements) {
            s.append(statement);
            s.append(";\n");
        }
        return s.toString();
    }

    @Override
    public ScriptEngine getScriptEngine() {
        return new JShellScriptEngine();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy