Example usage for javax.script ScriptEngineFactory getOutputStatement

List of usage examples for javax.script ScriptEngineFactory getOutputStatement

Introduction

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

Prototype

public String getOutputStatement(String toDisplay);

Source Link

Document

Returns a String that can be used as a statement to display the specified String using the syntax of the supported scripting language.

Usage

From source file:Main.java

public static void main(String[] args) {
    // Get the script engine factory for Nashorn
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    ScriptEngineFactory factory = engine.getFactory();

    // Get the script
    String script = factory.getOutputStatement("\"java2s\"");
    System.out.println("Syntax: " + script);

    // Evaluate the script
    try {/*  w w  w  .  j  av  a  2s.  c o m*/
        engine.eval(script);
    } catch (ScriptException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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

@Override
public boolean preprocessCommand(JissModel jissModel, String orig, StringBuffer cmd) {
    final String c = cmd.toString();
    if (c.equals(INFO_CMD)) {
        // clear string
        cmd.setLength(0);/* ww w  .j a  v  a2  s .c om*/

        final ScriptEngine se = jissModel.getScriptEngine();
        final ScriptEngineFactory seFactory = se.getFactory();

        final boolean incnl = !seFactory.getOutputStatement("").startsWith("println");

        final String infoCmd = seFactory.getOutputStatement(INFO_TXT + (incnl ? "\\n" : ""));
        final String langCmd = seFactory.getOutputStatement("Language:" + seFactory.getLanguageName() + " "
                + seFactory.getLanguageVersion() + (incnl ? "\\n" : ""));
        final String engineCmd = seFactory.getOutputStatement("Engine:" + seFactory.getEngineName() + " "
                + seFactory.getEngineVersion() + (incnl ? "\\n" : ""));
        final String program = seFactory.getProgram(infoCmd, langCmd, engineCmd);
        cmd.append(StringEscapeUtils.unescapeJava(program));
    }
    // we want the scripting engine to handle the replaced command
    return false;
}

From source file:ca.hedlund.jiss.blocks.preprocessor.ListBlocksPreprocessor.java

@Override
public boolean preprocessCommand(JissModel jissModel, String orig, StringBuffer cmd) {
    if (!orig.startsWith("::block list"))
        return false;

    final ScriptEngine se = jissModel.getScriptEngine();
    final ScriptEngineFactory seFactory = se.getFactory();

    final boolean incnl = !seFactory.getOutputStatement("").startsWith("println");

    final List<String> blockInfoCmds = new ArrayList<String>();
    blockInfoCmds.add(seFactory.getOutputStatement("Available blocks:" + (incnl ? "\\n" : "")));

    final Matcher matcher = LIST_BLOCKS_PATTERN.matcher(orig);
    if (matcher.matches()) {
        final String blockNameRegex = matcher.group(1);
        final BlockManager bm = new BlockManager();

        for (String blockName : bm.getBlocks()) {
            boolean printBlock = true;
            if (blockNameRegex != null) {
                printBlock = blockName.matches(blockNameRegex);
            }/*  w  ww . ja va  2  s  .c o  m*/
            if (printBlock) {
                blockInfoCmds.add(seFactory.getOutputStatement(blockName) + (incnl ? "\\n" : ""));
            }
        }
    }

    final String program = seFactory.getProgram(blockInfoCmds.toArray(new String[0]));
    cmd.setLength(0);
    cmd.append(StringEscapeUtils.unescapeJava(program));

    return false;
}

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

private String createPrintCmd(JissModel model, String toPrint) {
    final ScriptEngineFactory factory = model.getScriptEngine().getFactory();
    try {/* www  .  ja v a2  s  . c om*/
        boolean includeQuotes = factory.getOutputStatement(new String()).indexOf('\"') < 0;
        final String cmd = factory
                .getOutputStatement((includeQuotes ? "\"" : "") + toPrint + (includeQuotes ? "\"" : ""));
        return cmd;
    } catch (Exception e) {
        return "";
    }
}