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

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

Introduction

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

Prototype

@Override
    public boolean isArray() 

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   w w w . j ava  2 s .c o  m
        } 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 av  a2s.  com
            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.intuit.karate.core.ScriptBridge.java

License:Open Source License

public Object append(Object... items) {
    List out = new ArrayList();
    if (items == null) {
        return out;
    }//w  ww.  j av  a2s. co  m
    for (Object item : items) {
        if (item == null) {
            continue;
        }
        if (item instanceof ScriptObjectMirror) { // no need when graal
            ScriptObjectMirror som = (ScriptObjectMirror) item;
            if (som.isArray()) {
                out.addAll(som.values());
            } else {
                out.add(som);
            }
        } else if (item instanceof Collection) {
            out.addAll((Collection) item);
        } else {
            out.add(item);
        }
    }
    return out;
}

From source file:com.pivotal.cf.mobile.ats.json.ScriptObjectMirrorSerializer.java

License:Open Source License

@Override
public void serialize(ScriptObjectMirror value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonGenerationException {
    if (value.isArray()) {
        if (arraySerializer == null) {
            arraySerializer = (StdDelegatingSerializer) new StdDelegatingSerializer(ScriptObjectMirror.class,
                    new StdConverter<ScriptObjectMirror, Collection<Object>>() {

                        @Override
                        public Collection<Object> convert(ScriptObjectMirror value) {
                            return value.values();
                        }/*from   w  w  w. j  ava 2  s . c o m*/

                    }).createContextual(provider, null);

        }
        arraySerializer.serialize(value, jgen, provider);
    } else {
        if (objectSerializer == null) {
            objectSerializer = (StdDelegatingSerializer) new StdDelegatingSerializer(ScriptObjectMirror.class,
                    new StdConverter<ScriptObjectMirror, Map<String, Object>>() {

                        @Override
                        public Map<String, Object> convert(ScriptObjectMirror value) {
                            return value;
                        }

                    }).createContextual(provider, null);

        }
        objectSerializer.serialize(value, jgen, provider);
    }
}

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 va 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:com.qwazr.library.ldap.LdapConnector.java

License:Apache License

public void createUser(final LdapConnection connection, final String dn, final String passwordAttribute,
        final String clearPassword, final Map<String, Object> attrs) throws LdapException {
    connection.bind();/*from www .j a  v a 2  s  .co m*/
    final Entry entry = new DefaultEntry(dn + ", " + baseDn);
    if (clearPassword != null)
        entry.add(passwordAttribute, getShaPassword(clearPassword));
    if (attrs != null) {
        for (Map.Entry<String, Object> attr : attrs.entrySet()) {
            final String key = attr.getKey();
            final Object value = attr.getValue();
            if (value instanceof String) {
                entry.add(key, (String) value);
            } else if (value instanceof ScriptObjectMirror) {
                final 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:com.qwazr.utils.ScriptUtils.java

License:Apache License

public static <T> Map<String, T> toMap(ScriptObjectMirror som, Class<T> type) throws ScriptException {
    if (som == null)
        return null;
    if (som.isArray())
        throw new ScriptException("The JS object is an array");

    Map<String, T> map = new LinkedHashMap<String, T>();
    if (som.isEmpty())
        return map;
    som.forEach((s, o) -> map.put(s, ((ScriptObjectMirror) o).to(type)));
    return map;//from   ww  w  .j  ava 2 s  . c o  m
}

From source file:com.qwazr.utils.ScriptUtils.java

License:Apache License

public static <T> T[] toArray(ScriptObjectMirror som, Class<T> type) throws ScriptException {
    if (som == null)
        return null;
    if (!som.isArray())
        throw new ScriptException("The JS object is not an array");
    T[] array = (T[]) new Object[som.size()];
    final AtomicInteger i = new AtomicInteger(0);
    som.values().forEach(o -> array[i.getAndIncrement()] = ((ScriptObjectMirror) o).to(type));
    return array;
}

From source file:com.qwazr.utils.ScriptUtils.java

License:Apache License

public static void fillStringCollection(ScriptObjectMirror som, Collection<String> collection)
        throws ScriptException {
    if (som == null)
        return;/*w  w  w  .j a  v  a  2  s . c  om*/
    if (!som.isArray())
        throw new ScriptException("The JS object is not an array");
    som.values().forEach(o -> collection.add(o.toString()));
}

From source file:com.threecrickets.jvm.json.nashorn.ScriptObjectMirrorEncoder.java

License:Mozilla Public License

public void encode(Object object, JsonContext context) throws IOException {
    ScriptObjectMirror scriptObjectMirror = (ScriptObjectMirror) object;

    Object wrapped = ScriptObjectMirror.unwrap(scriptObjectMirror, Context.getGlobal());
    if (!(wrapped instanceof ScriptObjectMirror)) {
        context.encode(wrapped);//from   w w w  .j  a  v  a2 s  .  c  o m
        return;
    }

    if (scriptObjectMirror.isArray()) {
        context.out.append('[');

        int length = scriptObjectMirror.size();
        if (length > 0) {
            context.newline();

            for (int i = 0; i < length; i++) {
                Object value = scriptObjectMirror.getSlot(i);

                context.indentNested();
                context.nest().encode(value);

                if (i < length - 1)
                    context.comma();
            }

            context.newline();
            context.indent();
        }

        context.out.append(']');
    } else {
        context.out.append('{');

        String[] keys = scriptObjectMirror.getOwnKeys(true);
        int length = keys.length;
        if (length > 0) {
            context.newline();

            for (int i = 0; i < length; i++) {
                String key = keys[i];
                Object value = scriptObjectMirror.get(key);

                context.indentNested();
                context.quoted(key);
                context.colon();
                context.nest().encode(value);

                if (i < length - 1)
                    context.comma();
            }

            context.newline();
            context.indent();
        }

        context.out.append('}');
    }
}