List of usage examples for jdk.nashorn.internal.runtime ErrorManager ErrorManager
public ErrorManager()
From source file:capframework.http.core.ConfigFileParser.java
License:Apache License
void parse() { if (isConfigFileExist()) { try {//from ww w.j a v a 2 s . c om 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 www .j a v 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: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;//from w w w .j av a2s .co m 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; }