Example usage for jdk.nashorn.api.scripting ScriptObjectMirror entrySet

List of usage examples for jdk.nashorn.api.scripting ScriptObjectMirror entrySet

Introduction

In this page you can find the example usage for jdk.nashorn.api.scripting ScriptObjectMirror entrySet.

Prototype

@Override
    public Set<Map.Entry<String, Object>> entrySet() 

Source Link

Usage

From source file:JavaScriptTest.java

public static String serialize(Object obj) {
    StringBuilder ret = new StringBuilder();
    if (obj instanceof ScriptObjectMirror) {
        ScriptObjectMirror om = (ScriptObjectMirror) obj;
        //System.out.println(om+" isArray "+om.isArray());
        //System.out.println(om+" isEmpty "+om.isEmpty());
        //System.out.println(om+" isExtensible "+om.isExtensible());
        //System.out.println(om+" isFrozen "+om.isFrozen());
        //System.out.println(om+" isFunction "+om.isFunction());
        //System.out.println(om+" isSealed "+om.isSealed());
        //System.out.println(om+" isStrictFunction "+om.isStrictFunction());            
        //System.out.println(om+" getOwnKeys "+Arrays.asList(om.getOwnKeys(true)));  

        if (om.isFunction()) {
            ret.append(om.toString());/*from  ww  w  .  j a  v  a2 s.c  om*/
        } else if (om.isArray()) {
            ret.append("[");
            //ret.append("isArray:"+om.toString());
            for (int x = 0; x < om.size(); x++) {
                Object o = om.getSlot(x);
                ret.append(serialize(o));
                if (x + 1 < om.size()) {
                    ret.append(",");
                }
            }
            ret.append("]");
        } else if (om.toString().indexOf("global") > -1) {
            Iterator<Map.Entry<String, Object>> it = om.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Object> entry = it.next();
                ret.append("var " + entry.getKey() + "=" + serialize(entry.getValue()) + ";\n");
            }
        } else {
            ret.append("{");
            Iterator<Map.Entry<String, Object>> it = om.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Object> entry = it.next();
                ret.append(entry.getKey() + ":" + serialize(entry.getValue()));
                if (it.hasNext()) {
                    ret.append(",");
                }
            }
            ret.append("}");
        }
    } else if (obj instanceof String) {
        ret.append("\"" + obj + "\"");
    } else {
        ret.append(obj);
    }
    return ret.toString();
}

From source file:com.baasbox.service.scripting.js.NashornMapper.java

License:Apache License

private JsonNode convertMirror(ScriptObjectMirror mirror) throws ScriptEvalException {
    if (mirror == null) {

        return NullNode.getInstance();
    } else if (ScriptObjectMirror.isUndefined(mirror)) {
        return MissingNode.getInstance();
    } else if (mirror.isFunction()) {
        return MissingNode.getInstance();
    } else if (mirror.isArray()) {
        Collection<Object> values = mirror.values();
        ArrayNode node = Json.mapper().createArrayNode();
        for (Object o : values) {
            JsonNode e = convertDeepJson(o);
            if (e.isMissingNode()) {
                continue;
            }/*from w w w . j a  v  a  2s  .  c om*/
            node.add(e);
        }
        return node;
    } else if (mirror.hasMember("toJSON")) {
        Object toJSON = mirror.callMember("toJSON");
        return convertDeepJson(toJSON);
    } else {
        ObjectNode obj = Json.mapper().createObjectNode();
        Set<Map.Entry<String, Object>> entries = mirror.entrySet();
        for (Map.Entry<String, Object> e : entries) {
            Object obv = e.getValue();
            JsonNode jsonNode = convertDeepJson(obv);
            if (jsonNode.isMissingNode()) {
                continue;
            }
            obj.put(e.getKey(), jsonNode);
        }
        return obj;
    }
}

From source file:com.microchip.mplab.nbide.embedded.arduino.importer.ArduinoBuilderRunner.java

License:Open Source License

private List<Path> findMainLibraryPaths() throws ScriptException, FileNotFoundException {
    LOGGER.info("Looking for main library paths");

    Path includesCachePath = Paths.get(preprocessDirPath.toAbsolutePath().toString(), "includes.cache");
    ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByExtension("js");
    ScriptObjectMirror mirror = (ScriptObjectMirror) scriptEngine
            .eval(new FileReader(includesCachePath.toString()));
    List<Path> libraryPaths = new ArrayList<>();
    mirror.entrySet().forEach(e -> {
        if (e.getValue() instanceof ScriptObjectMirror) {
            ScriptObjectMirror m = (ScriptObjectMirror) e.getValue();
            Object sourceFile = m.get("Sourcefile");
            if (sourceFile != null && !sourceFile.toString().trim().isEmpty()) {
                String entry = m.get("Includepath").toString();
                if (!entry.trim().isEmpty()) {
                    if (entry.endsWith(File.separator + "src")) {
                        entry = entry.substring(0, entry.length() - 4);
                    }//from   w ww  . jav a 2  s.  c  o  m
                    LOGGER.log(Level.INFO, "Found library path: {0}", entry);
                    libraryPaths.add(Paths.get(entry));
                }
            }
        }
    });

    if (libraryPaths.isEmpty()) {
        LOGGER.info("No main library dependencies found");
    }

    return libraryPaths;
}

From source file:com.n_micocci.concerto.filters.FilterNotesMap.java

License:Open Source License

public FilterNotesMap(ScriptObjectMirror notesMapJs) {
    notesMap = new HashMap<Integer, Integer>();
    for (Map.Entry<String, Object> noteEntry : notesMapJs.entrySet()) {
        int a, b;
        a = Integer.parseInt(noteEntry.getKey());
        b = (Integer) noteEntry.getValue();
        notesMap.put(a, b);//from w  w w  .  ja v a 2s  .co  m
    }
}

From source file:com.qwazr.connectors.LdapConnector.java

License:Apache License

public void createUser(LdapConnection connection, String dn, String clearPassword, ScriptObjectMirror attrs)
        throws LdapException {
    connection.bind();//from   w  w w  .j a v a  2s  .  c o m
    Entry entry = new DefaultEntry(dn + ", " + base_dn);
    if (clearPassword != null)
        entry.add("userPassword", getShaPassword(clearPassword));
    if (attrs != null) {
        for (Map.Entry<String, Object> attr : attrs.entrySet()) {
            String key = attr.getKey();
            Object value = attr.getValue();
            if (value instanceof String) {
                entry.add(key, (String) value);
            } else if (value instanceof ScriptObjectMirror) {
                ScriptObjectMirror som = (ScriptObjectMirror) value;
                if (som.isArray()) {
                    for (Object obj : som.values())
                        entry.add(key, obj.toString());
                } else
                    throw new LdapException("Unsupported hash: " + som);
            } else
                throw new LdapException("Unsupported type: " + value.getClass());
        }
    }
    connection.add(entry);
}

From source file:hmi.flipper.behaviourselection.template.value.AbstractValue.java

License:BSD License

private Object js2Object(Object jsResult) {
    if (jsResult instanceof ScriptObjectMirror) {
        ScriptObjectMirror jsObject = (ScriptObjectMirror) jsResult;
        if (jsObject.size() > 0 && jsObject.containsKey("0")) {
            List list = new DefaultList();
            for (int i = 0; i < jsObject.size(); i++) {
                Object toAdd = js2Object(jsObject.get("" + i));
                if (toAdd == null) {
                    break; // not a integer valued list
                }/*  w w  w  .j av a2s.c  o  m*/
                if (toAdd instanceof Double) {
                    list.addItemEnd((Double) toAdd);
                } else if (toAdd instanceof Integer) {
                    list.addItemEnd((Integer) toAdd);
                } else if (toAdd instanceof List) {
                    list.addItemEnd((List) toAdd);
                } else if (toAdd instanceof Record) {
                    list.addItemEnd((Record) toAdd);
                } else {
                    list.addItemEnd(toAdd.toString());
                }
            }
            if (list.size() == ((ScriptObjectMirror) jsObject).size()) {
                return list;
            }
        }
        Record record = new DefaultRecord();
        for (Entry<String, Object> ent : jsObject.entrySet()) {
            record.set(ent.getKey(), js2Object(ent.getValue()));
        }
        return record;
    } else if (jsResult instanceof Long || jsResult instanceof Double || jsResult instanceof Float) {
        if (jsResult instanceof Long) {
            return ((Long) jsResult).doubleValue();
        } else if (jsResult instanceof Float) {
            return ((Long) jsResult).floatValue();
        } else {
            return ((Double) jsResult);
        }
    } else if (jsResult instanceof Integer) {
        return (Integer) jsResult;
    } else if (jsResult instanceof String) {
        return (String) jsResult;
    }
    System.err.println("Could not parse JavaScript result to eligible DefaultRecord type! Type was: "
            + jsResult.getClass());
    return jsResult.toString();
}

From source file:io.crate.operation.language.JavaScriptUserDefinedFunction.java

private Object convertScriptResult(ScriptObjectMirror scriptObject) {
    switch (info.returnType().id()) {
    case ArrayType.ID:
        if (scriptObject.isArray()) {
            return scriptObject.values().toArray();
        }// w w w .j  a  v  a  2s.c o  m
        break;
    case ObjectType.ID:
        return scriptObject.entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    case GeoPointType.ID:
        if (scriptObject.isArray()) {
            return GeoPointType.INSTANCE.value(scriptObject.values().toArray());
        }
        break;
    case SetType.ID:
        return new HashSet<>(scriptObject.values());
    }
    throw new IllegalArgumentException(String.format(Locale.ENGLISH,
            "The return type of the function [%s] is not compatible with the type of the function evaluation result.",
            info.returnType()));
}

From source file:net.poemerchant.scraper.ShopScraper.java

License:Open Source License

private List<Map<String, Object>> parseItemJSArray(ScriptObjectMirror som) {
    List<Map<String, Object>> items = new ArrayList<Map<String, Object>>(som.entrySet().size());

    Object[] itemsArr = NashornUtils.somToArray(som);
    for (int i = 0; i < itemsArr.length; i++) {
        ScriptObjectMirror itemSom = (ScriptObjectMirror) itemsArr[i];
        Map<String, Object> itemMap = NashornUtils.somToMap(itemSom, null);
        items.add(itemMap);/*from w  ww . j a  v a  2 s. c  om*/
    }

    return items;
}

From source file:org.eclairjs.nashorn.Utils.java

License:Apache License

public static String toJSON(ScriptObjectMirror som) {
    som = (ScriptObjectMirror) ScriptObjectMirror.wrapAsJSONCompatible(som, null);
    Map hashmap = new HashMap<>();
    for (Map.Entry<String, Object> entry : som.entrySet()) {
        String key = entry.getKey().toString();
        Object value = entry.getValue();
        boolean ignore = false;
        if (value != null) {
            String clsName = value.getClass().getName().toString();
            switch (clsName) {
            case "java.lang.String":
            case "java.lang.Boolean":
            case "java.lang.Long":
            case "java.lang.Float":
            case "java.lang.Double":
                ignore = false;/*from   www. j  a v  a2 s .  c om*/
                break;
            default:
                ignore = true;

            }
        }
        if (!ignore)
            hashmap.put(key, value);
    }
    String j = JSONValue.toJSONString(hashmap);
    return j;
}

From source file:org.eclairjs.nashorn.Utils.java

License:Apache License

public static Map createJavaHashMap(Object map) {

    ScriptObjectMirror jsObject = null;
    if (map instanceof jdk.nashorn.internal.runtime.ScriptObject)
        jsObject = ScriptUtils.wrap((jdk.nashorn.internal.runtime.ScriptObject) map);
    else if (map instanceof ScriptObjectMirror)
        jsObject = (ScriptObjectMirror) map;
    else//from   w  w  w. j a  v a2 s .c om
        throw new RuntimeException("not a script object");

    Map hashmap = new HashMap<>();
    for (Map.Entry<String, Object> entry : jsObject.entrySet()) {
        String key = entry.getKey().toString();
        String value = entry.getValue().toString();

        hashmap.put(key, value);
    }
    return hashmap;
}