List of usage examples for jdk.nashorn.internal.runtime.options Options Options
public Options(final String resource)
From source file:capframework.http.core.ConfigFileParser.java
License:Apache License
void parse() { if (isConfigFileExist()) { try {/*from w ww . j a v a2 s .c o m*/ FileInputStream inputStream = new FileInputStream(file); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int len; byte[] buffer = new byte[1024]; while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } String src = outputStream.toString(); Context context = new Context(new Options("cap"), new ErrorManager(), loader); JSONParser jsonParser = new JSONParser(src, new Global(context), true); json = (JD) jsonParser.parse(); configs = (JD) json.get("ApplicationConfig"); resources = (JD) json.get("ResourceMapping"); errors = (NativeArray) json.get("ErrorHandlerMapping"); } catch (Exception e) { e.printStackTrace(); } } }
From source file:com.siemens.ct.exi.javascript.JStoAST.java
License:Open Source License
public static String getAST(String jsCode) throws IOException, EXIException { // http://sites.psu.edu/robertbcolton/2015/07/31/java-8-nashorn-script-engine/ /*//from ww w . jav a 2 s .c om * This JSON encoding provided by Nashorn is compliant with the * community standard JavaScript JSON AST model popularized by Mozilla. * https://developer.mozilla.org/en-US/docs/Mozilla/Projects/ * SpiderMonkey/Parser_API */ // Additionally the OpenJDK project is developing a public interface for // Java 9 that allows AST traversal in a more standard and user friendly // way. // String code = "function a() { var b = 5; } function c() { }"; // String sin = "./data//jquery.min.js"; // String sin = "./data/animals.js"; // String code = new String(Files.readAllBytes(Paths.get(sin))); // ScriptEngine engine = new // ScriptEngineManager().getEngineByName("nashorn"); // engine.eval("print('Hello World!');"); // load jquery source file // engine.eval("load('" + // "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js" + // "');"); // engine.eval("load('" + sin + "');"); Options options = new Options("nashorn"); options.set("anon.functions", true); options.set("parse.only", true); options.set("scripting", true); ErrorManager errors = new ErrorManager(); Context contextm = new Context(options, errors, Thread.currentThread().getContextClassLoader()); Context.setGlobal(contextm.createGlobal()); String jsonAST = ScriptUtils.parse(jsCode, "<unknown>", false); return jsonAST; }
From source file:edu.brown.cs.bubbles.nobase.NobaseNashorn.java
License:Open Source License
/********************************************************************************/ @Override//w ww . j a va2 s .c o m public ISemanticData parse(NobaseProject proj, NobaseFile fd, boolean lib) { Source src = new Source(fd.getFileName(), fd.getContents()); Options opts = new Options("nashorn"); PrintWriter pw = new PrintWriter(new StringWriter()); ScriptEnvironment env = new ScriptEnvironment(opts, pw, pw); DeferredErrorManager em = new DeferredErrorManager(fd); if (fd.getFileName().endsWith(".json")) { JSONParser jsonparse = new JSONParser(src, em); Node nd = jsonparse.parse(); if (do_debug) { System.err.println("WORKING ON " + fd.getFileName()); try { ASTWriter pv = new ASTWriter(nd); System.err.println("PARSE: " + pv.toString()); } catch (Throwable t) { } } return null; } Parser parser = new Parser(env, src, em); FunctionNode fn = parser.parse(); if (fn == null) { NobaseMain.logE("Problem parsing " + fd.getFileName()); return null; } if (do_debug) { System.err.println("WORKING ON " + fd.getFileName()); try { ASTWriter pv = new ASTWriter(fn); System.err.println("PARSE: " + pv.toString()); } catch (Throwable t) { } } ParseData rslt = new ParseData(proj, fd, em, fn.getBody(), lib); if (do_debug) { System.err.println("RESULT: " + rslt.getRootNode().dumpTree(fd)); } return rslt; }
From source file:IDE.SyntaxTree.JWebParser.java
public JWebParser(String source) { strSource = source;//from w ww.j a v a 2 s . c o m Options opt = new Options("nashorn"); opt.set("anon.functions", true); // opt.set("parse.only", true); opt.set("scripting", true); Context context = new Context(opt, em, Thread.currentThread().getContextClassLoader()); Context.setGlobal(new Global(context)); Source textSource = Source.sourceFor("mySource", source); env = context.getEnv();//new ScriptEnvironment(opt, new PrintWriter(OutWriter), new PrintWriter(ErrorWriter) ); nashornParser = new jdk.nashorn.internal.parser.Parser(env, textSource, em); // jdk.nashorn.internal.ir.FunctionCall }
From source file:me.finalchild.nashornbukkit.util.BukkitImporter.java
License:MIT License
public static Set<String> getUsedIdentifiers(Path file) throws IOException { Set<String> result = new HashSet<>(); String text = new String(Files.readAllBytes(file), StandardCharsets.UTF_8); String json;// www. ja va2s .c om Options options = new Options(""); ErrorManager errorManager = new ErrorManager(); Context context = new Context(options, errorManager, NashornBukkit.class.getClassLoader()); Context.setGlobal(context.createGlobal()); json = ScriptUtils.parse(text, file.getFileName().toString(), false); JsonParser jsonParser = new JsonParser(); JsonObject rootObj = jsonParser.parse(json).getAsJsonObject(); addUsedIdentifiers(rootObj, result); return result; }