Example usage for javax.script ScriptEngineFactory getProgram

List of usage examples for javax.script ScriptEngineFactory getProgram

Introduction

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

Prototype

public String getProgram(String... statements);

Source Link

Document

Returns a valid scripting language executable program with given statements.

Usage

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  ww w . ja  va  2s.c  om*/
}

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));
    }//from  w ww  .  ja  v  a2  s  . com
    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.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);
            }//from   www. j  a v  a  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;
}