private static String get(final NativeObject obj, final String key) { return obj == null ? null : obj.has(key, null) ? obj.get(key, null).toString() : null; }
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.SimpleBindings; import sun.org.mozilla.javascript.internal.NativeObject; String buildInfoJS = "var build_info = new Object;build_info.BUILD_DISPLAY_VERSION = 'Build 201302232300'"; ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine jsEngine = mgr.getEngineByName("JavaScript"); jsEngine.eval(buildInfoJS); Object nativeObject = jsEngine.get("build_info"); if (nativeObject instanceof NativeObject) { NativeObject nObj = (NativeObject)nativeObject; for (String key: nObj.getAllIds()) { System.out.println(key); System.out.println(nObj.get(key, nObj)); } }
private void getPrefValues(NativeObject currentObject, String currentKey, Map<String, Serializable> values) { Object[] ids = currentObject.getIds(); for (Object id : ids) { String key = getAppendedKey(currentKey, id.toString()); Object value = currentObject.get(id.toString(), currentObject); if (value instanceof NativeObject) { getPrefValues((NativeObject)value, key, values); } else { values.put(key, (Serializable)value); } } }
public Object next() { return get((key = ids[index++])); }
private void getPrefValues(NativeObject currentObject, String currentKey, Map<String, Serializable> values) { Object[] ids = currentObject.getIds(); for (Object id : ids) { String key = getAppendedKey(currentKey, id.toString()); Object value = currentObject.get(id.toString(), currentObject); if (value instanceof NativeObject) { getPrefValues((NativeObject)value, key, values); } else { values.put(key, (Serializable)value); } } }
public Object next() { return get((key = ids[index++])); }
public Object next() { return get((key = ids[index++])); }
public Object next() { return get((key = ids[index++])); }
private static String getString(NativeObject object, String key) { return object.get(key).toString(); }
private static String getString(final NativeObject object, final String key) { return object.get(key).toString(); }
private static String getString(NativeObject object, String key) { return object.get(key).toString(); }
public static TypescriptCompilationProblem fromNativeObject(NativeObject jsProblem) { Double line = (Double) jsProblem.get("line"); Double column = (Double) jsProblem.get("col"); String message = extractMessage(jsProblem); Double compilationUnitIndex = (Double) jsProblem.get("compilationUnitIndex"); return new TypescriptCompilationProblem(line.intValue(),column.intValue(),message,compilationUnitIndex.intValue()); }
private static String extractMessage(NativeObject jsProblem) { Object rawMessage = jsProblem.get("message"); if (rawMessage instanceof ConsString) { return ((ConsString) rawMessage).toString(); } else if (rawMessage instanceof String) { return (String) rawMessage; } else { throw new IllegalStateException("Unable to extract an error message from type " + rawMessage.getClass().getCanonicalName()); } }
try (final WebClient webClient = new WebClient(BrowserVersion.CHROME)) { String url = "http://localhost/test.html"; HtmlPage htmlPage = webClient.getPage(url); NativeArray array = (NativeArray) htmlPage.executeJavaScript("arMailRuMessages").getJavaScriptResult(); for (int i = 0; i < array.getLength(); i++) { NativeObject object = (NativeObject) array.get(i); String id = (String) object.get("id"); System.out.println(id); NativeObject correspondents = (NativeObject) object.get("correspondents"); NativeArray from = (NativeArray) correspondents.get("from"); System.out.println(((NativeObject) from.get(0)).get("name")); } }
public Object remove(Object key) { Object value = get(key); if (key instanceof String) { delete((String) key); } else if (key instanceof Number) { delete(((Number) key).intValue()); } return value; }
public Object remove(Object key) { Object value = get(key); if (key instanceof String) { delete((String) key); } else if (key instanceof Number) { delete(((Number) key).intValue()); } return value; }
public Object remove(Object key) { Object value = get(key); if (key instanceof String) { delete((String) key); } else if (key instanceof Number) { delete(((Number) key).intValue()); } return value; }
public Object remove(Object key) { Object value = get(key); if (key instanceof String) { delete((String) key); } else if (key instanceof Number) { delete(((Number) key).intValue()); } return value; }
String strFunction = "function add(x,y){ " + "return { " + "id:x+y " + "}; " + "}"; Context context = Context.enter(); ScriptableObject scope = context.initStandardObjects(); context.evaluateString(scope, strFunction, "test", 1, null); Function functionAdd = (Function)scope.get("add"); NativeObject untypedResult = (NativeObject)functionAdd.call(context, scope, scope, new Object[] { 1, 2 }); double id = (Double)untypedResult.get("id", untypedResult);
public String compile(final String typeScript) { final StopWatch stopWatch = new StopWatch(); stopWatch.start("initContext"); final RhinoScriptBuilder builder = initScriptBuilder(); stopWatch.stop(); stopWatch.start("compile"); try { final String execute = getCompilationCommand(typeScript); final NativeObject compilationResult = (NativeObject) builder.evaluate(execute, "compile"); final NativeArray errors = (NativeArray) compilationResult.get(PARAM_ERRORS); if (errors.size() > 0) { throwCompilationError(errors); } return compilationResult.get(PARAM_SOURCE).toString(); } finally { stopWatch.stop(); LOG.debug(stopWatch.prettyPrint()); } }