Example usage for javax.script ScriptEngineFactory getLanguageName

List of usage examples for javax.script ScriptEngineFactory getLanguageName

Introduction

In this page you can find the example usage for javax.script ScriptEngineFactory getLanguageName.

Prototype

public String getLanguageName();

Source Link

Document

Returns the name of the scripting language supported by this ScriptEngine.

Usage

From source file:ca.hedlund.jiss.preprocessor.LangPreprocessor.java

private void printLangs(JissModel model, StringBuffer cmd) {
    final ScriptEngineManager manager = new ScriptEngineManager(JissModel.class.getClassLoader());
    final List<String> cmds = new ArrayList<String>();

    for (ScriptEngineFactory factory : manager.getEngineFactories()) {
        final String engineInfo = factory.getLanguageName() + " " + factory.getLanguageVersion() + ":"
                + factory.getEngineName() + " " + factory.getEngineVersion();
        cmds.add(createPrintCmd(model, engineInfo));
    }//w  ww  .  ja va  2  s . c  om
    final ScriptEngineFactory factory = model.getScriptEngine().getFactory();
    final String prog = StringEscapeUtils.unescapeJava(factory.getProgram(cmds.toArray(new String[0])));
    cmd.append(prog);
}

From source file:ca.hedlund.jiss.preprocessor.LangPreprocessor.java

private void printCurrentLang(JissModel model, StringBuffer cmd) {
    final List<String> cmds = new ArrayList<String>();

    final ScriptEngineFactory factory = model.getScriptEngine().getFactory();
    final String engineInfo = factory.getLanguageName() + " " + factory.getLanguageVersion() + ":"
            + factory.getEngineName() + " " + factory.getEngineVersion();
    cmds.add(createPrintCmd(model, engineInfo));

    final String prog = StringEscapeUtils.unescapeJava(factory.getProgram(cmds.toArray(new String[0])));
    cmd.append(prog);/*from   www  .  ja v a 2 s. c o  m*/
}

From source file:ca.hedlund.jiss.preprocessor.LangPreprocessor.java

@Override
public boolean preprocessCommand(JissModel jissModel, String orig, StringBuffer cmd) {
    final String c = cmd.toString();
    if (c.equals("::langs")) {
        cmd.setLength(0);/* w  w w.ja  v  a  2s . c  om*/
        printLangs(jissModel, cmd);
    } else if (c.equals("::lang")) {
        cmd.setLength(0);
        printCurrentLang(jissModel, cmd);
    } else if (c.startsWith("::lang")) {
        cmd.setLength(0);

        final String parts[] = c.split("\\p{Space}");
        if (parts.length == 2) {
            final String lang = parts[1];

            final ScriptEngineManager manager = new ScriptEngineManager(JissModel.class.getClassLoader());
            ScriptEngine newEngine = null;
            for (ScriptEngineFactory factory : manager.getEngineFactories()) {
                if (factory.getLanguageName().equals(lang) || factory.getExtensions().contains(lang)
                        || factory.getMimeTypes().contains(lang)) {
                    newEngine = factory.getScriptEngine();
                    break;
                }
            }

            if (newEngine != null) {
                jissModel.setScriptEngine(newEngine);
                printCurrentLang(jissModel, cmd);
            }
        }
    }
    return false;
}

From source file:it.geosolutions.geobatch.action.scripting.ScriptingTest.java

@Test
public void testLoop() throws ScriptException {

    String engineName = "groovy";

    ScriptEngine foundEngine = null;
    // create a script engine manager
    ScriptEngineManager mgr = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = mgr.getEngineFactories();
    System.out.println("FOUND " + factories.size() + " factories");
    for (ScriptEngineFactory sef : factories) {
        System.out.println("FACTORY: " + "'" + sef.getEngineName() + "' " + "'" + sef.getLanguageName() + "' "
                + "'" + sef.getExtensions() + "' " + "'" + sef.getNames() + "' ");
        if (sef.getEngineName().contains(engineName)) {
            foundEngine = sef.getScriptEngine();
        }//from   w  ww  .j  a v  a 2  s  .c o  m

    }

    assertNotNull("Can't find engine '" + engineName + "'", foundEngine);
    foundEngine.eval("print('Hello, World')");
}

From source file:it.geosolutions.geobatch.action.scripting.ScriptingTest.java

@Test
public void testGetEngineByExt() throws ScriptException {

    String engineExt = "js";

    ScriptEngineManager mgr = new ScriptEngineManager();
    // create a JavaScript engine
    ScriptEngine engine = mgr.getEngineByExtension(engineExt);
    assertNotNull("Can't find engine '" + engineExt + "'", engine);

    ScriptEngineFactory sef = engine.getFactory();
    System.out.println("FACTORY for " + engineExt + ": " + "'" + sef.getEngineName() + "' " + "'"
            + sef.getLanguageName() + "' " + "'" + sef.getExtensions() + "' " + "'" + sef.getNames() + "' ");

    // evaluate JavaScript code from String
    engine.eval("print('Hello, World')");

}

From source file:it.geosolutions.geobatch.action.scripting.ScriptingTest.java

@Test
public void testGroovy() throws ScriptException {

    String engineName = "groovy";

    ScriptEngineManager mgr = new ScriptEngineManager();
    // create a JavaScript engine
    ScriptEngine engine = mgr.getEngineByName(engineName);
    assertNotNull("Can't find engine '" + engineName + "'", engine);

    ScriptEngineFactory sef = engine.getFactory();
    System.out.println("FACTORY for " + engineName + ": " + "'" + sef.getEngineName() + "' " + "'"
            + sef.getLanguageName() + "' " + "'" + sef.getExtensions() + "' " + "'" + sef.getNames() + "' ");

    // evaluate code from String
    engine.eval("println \"hello, groovy\"");
}

From source file:com.seajas.search.profiler.controller.ModifierController.java

/**
 * Available script languages.//from w  w  w.  j a  va2s.  com
 * 
 * @return Map<String, String>
 */
@ModelAttribute("scriptLanguages")
public Map<String, String> populateScriptLanguages() {
    Map<String, String> scriptLanguages = new LinkedHashMap<String, String>();

    for (ScriptEngineFactory factory : new ScriptEngineManager().getEngineFactories())
        if (factory.getLanguageName().equalsIgnoreCase("ECMAScript"))
            scriptLanguages.put(factory.getLanguageName(), "JavaScript");
        else if (factory.getLanguageName().equalsIgnoreCase("Groovy")
                || factory.getLanguageName().equalsIgnoreCase("Scala")
                || factory.getLanguageName().equalsIgnoreCase("Ruby"))
            scriptLanguages.put(factory.getLanguageName(), StringUtils.capitalize(factory.getLanguageName()));

    scriptLanguages.put("xslt", "XSLT");

    return scriptLanguages;
}

From source file:org.springframework.data.hadoop.scripting.Jsr223ScriptEvaluator.java

protected ScriptEngine discoverEngine(ScriptSource script, Map<String, Object> arguments) {
    ScriptEngineManager engineManager = new ScriptEngineManager(classLoader);
    ScriptEngine engine = null;//from w  w w.  ja v a 2 s .co  m

    if (StringUtils.hasText(language)) {
        engine = engineManager.getEngineByName(language);
    } else {
        // make use the extension (enhanced ScriptSource interface)
        Assert.hasText(extension, "no language or extension specified");
        engine = engineManager.getEngineByExtension(extension);
    }

    Assert.notNull(engine, "No suitable engine found for "
            + (StringUtils.hasText(language) ? "language " + language : "extension " + extension));

    if (log.isDebugEnabled()) {
        ScriptEngineFactory factory = engine.getFactory();
        log.debug(String.format("Using ScriptEngine %s (%s), language %s (%s)", factory.getEngineName(),
                factory.getEngineVersion(), factory.getLanguageName(), factory.getLanguageVersion()));
    }

    return engine;
}

From source file:cc.osint.graphd.script.GScriptEngine.java

public void dumpScriptEngines() throws Exception {
    ScriptEngineManager mgr = new ScriptEngineManager();
    List<ScriptEngineFactory> factories = mgr.getEngineFactories();

    for (ScriptEngineFactory factory : factories) {
        log.info("ScriptEngineFactory Info");
        String engName = factory.getEngineName();
        String engVersion = factory.getEngineVersion();
        String langName = factory.getLanguageName();
        String langVersion = factory.getLanguageVersion();

        System.out.printf("\tScript Engine: %s (%s)\n", engName, engVersion);

        List<String> engNames = factory.getNames();
        for (String name : engNames) {
            System.out.printf("\tEngine Alias: %s\n", name);
        }//from w ww. ja  v a  2s. c om

        System.out.printf("\tLanguage: %s (%s)\n", langName, langVersion);
    }
}

From source file:org.pentaho.platform.plugin.condition.scriptable.ScriptableCondition.java

public void setListAvailableEngines(final boolean value) {
    this.listAvailableEngines = value;
    if (value) {//from w  w  w  .  j a  va  2  s. c  o m
        System.out.println("*** DEBUG - Display Script Engine List ***");
        ScriptEngineManager manager = new ScriptEngineManager();
        List<ScriptEngineFactory> factories = manager.getEngineFactories();
        for (ScriptEngineFactory factory : factories) {
            System.out.println(String.format("Engine %s, Version %s, Language %s, Registered Names: %s",
                    factory.getEngineName(), factory.getEngineVersion(), factory.getLanguageName(),
                    factory.getNames().toString()));
        }
    }
}