com.ui4j.webkit.WebKitMapper Maven / Gradle / Ivy
The newest version!
package com.ui4j.webkit;
import static java.lang.Integer.parseInt;
import static java.lang.String.valueOf;
import static java.util.Collections.emptyMap;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.ui4j.spi.JavaScriptEngine;
import netscape.javascript.JSObject;
public class WebKitMapper {
public WebKitMapper(JavaScriptEngine engine) {
engine.executeScript(
"Object.prototype.__ui4j_keys = function() { return Object.keys(this); };" +
"Object.prototype.__ui4j_isArray = function() { return Array.isArray(this); };");
}
public Map toJava(JSObject object) {
if (object == null) {
return emptyMap();
}
Map map = new LinkedHashMap<>();
JSObject keys = (JSObject) object.call("__ui4j_keys");
int length = parseInt(valueOf(keys.getMember("length")));
for (int i = 0; i < length; i++) {
String key = (String) keys.getSlot(i);
Object value = object.getMember(key);
value = toJava(value);
if (!(value instanceof JSObject)) {
map.put(key, value);
} else {
JSObject jsObject = (JSObject) value;
boolean isArray = (Boolean) jsObject.call("__ui4j_isArray");
if (isArray) {
List