Java Code Examples for org.mozilla.javascript.Scriptable#getIds()

The following examples show how to use org.mozilla.javascript.Scriptable#getIds() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RhinoWorkerUtils.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static Map<String, Object> toMap(Scriptable obj) {
    Map<String, Object> map = new LinkedHashMap<String, Object>();

    for (Object id : obj.getIds()) {
        String key;
        Object value;
        if (id instanceof String) {
            key = (String) id;
            value = obj.get(key, obj);
        } else if (id instanceof Integer) {
            key = id.toString();
            value = obj.get((Integer) id, obj);
        } else {
            throw new IllegalArgumentException(String.format("Unexpected key type: %s (value: %s)", id.getClass().getName(), id));
        }

        map.put(key, toJavaValue(value));
    }

    return map;
}
 
Example 2
Source File: RhinoWorkerUtils.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static Map<String, Object> toMap(Scriptable obj) {
    Map<String, Object> map = new LinkedHashMap<String, Object>();

    for (Object id : obj.getIds()) {
        String key;
        Object value;
        if (id instanceof String) {
            key = (String) id;
            value = obj.get(key, obj);
        } else if (id instanceof Integer) {
            key = id.toString();
            value = obj.get((Integer) id, obj);
        } else {
            throw new IllegalArgumentException(String.format("Unexpected key type: %s (value: %s)", id.getClass().getName(), id));
        }

        map.put(key, toJavaValue(value));
    }

    return map;
}
 
Example 3
Source File: RhinoWorkerUtils.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static Map<String, Object> toMap(Scriptable obj) {
    Map<String, Object> map = new LinkedHashMap<String, Object>();

    for (Object id : obj.getIds()) {
        String key;
        Object value;
        if (id instanceof String) {
            key = (String) id;
            value = obj.get(key, obj);
        } else if (id instanceof Integer) {
            key = id.toString();
            value = obj.get((Integer) id, obj);
        } else {
            throw new IllegalArgumentException(String.format("Unexpected key type: %s (value: %s)", id.getClass().getName(), id));
        }

        map.put(key, toJavaValue(value));
    }

    return map;
}
 
Example 4
Source File: RhinoWorkerUtils.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static Map<String, Object> toMap(Scriptable obj) {
    Map<String, Object> map = new LinkedHashMap<String, Object>();

    for (Object id : obj.getIds()) {
        String key;
        Object value;
        if (id instanceof String) {
            key = (String) id;
            value = obj.get(key, obj);
        } else if (id instanceof Integer) {
            key = id.toString();
            value = obj.get((Integer) id, obj);
        } else {
            throw new IllegalArgumentException(String.format("Unexpected key type: %s (value: %s)", id.getClass().getName(), id));
        }

        map.put(key, toJavaValue(value));
    }

    return map;
}
 
Example 5
Source File: JsFunction.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * debug utility to display a JS object
 * @param buffer whil append the representation here
 * @param object special treatment if a javascript object
 */
private void append(StringBuffer buffer, Object object) {
    if (object instanceof Scriptable) {
        Scriptable scriptable = (Scriptable) object;
        buffer.append("{");
        boolean first = true;
        for (Object id : scriptable.getIds()) {
            if (first) {
                first = false;
            } else {
                buffer.append(", ");    
            }
            if (id instanceof String) {
                buffer.append(id).append(": ");
                append(buffer, scriptable.get((String)id, jsScriptEngine.getScope()));
            } else {
                buffer.append("[").append(id).append("]: ");
                append(buffer, scriptable.get((Integer)id, jsScriptEngine.getScope()));
            }
        }
        buffer.append("}");

    } else {
        buffer.append(object);
    }
}
 
Example 6
Source File: JsFunction.java    From spork with Apache License 2.0 6 votes vote down vote up
private Object jsToPigMap(Scriptable object, Schema schema, int depth) {
    debugConvertJSToPig(depth, "Map", object, schema);
    Map<String, Object> map = new HashMap<String, Object>();
    Object[] ids = object.getIds();
    for (Object id : ids) {
        if (id instanceof String) {
            String name = (String) id;
            Object value = object.get(name, object);
            if (value instanceof NativeJavaObject) {
                value = ((NativeJavaObject)value).unwrap();
            } else if (value instanceof Undefined) {
                value = null;
            }
            map.put(name, value);
        }
    }
    debugReturn(depth, map);
    return map;
}
 
Example 7
Source File: JavascriptTestUtilities.java    From cxf with Apache License 2.0 6 votes vote down vote up
public static String scriptableToString(Scriptable scriptable) {
    StringBuilder builder = new StringBuilder();
    for (Object propid : scriptable.getIds()) {
        String propIdString = Context.toString(propid);
        int propIntKey = -1;
        try {
            propIntKey = Integer.parseInt(propIdString);
        } catch (NumberFormatException nfe) {
            // dummy.
        }
        String propValue;
        if (propIntKey >= 0) {
            propValue = Context.toString(scriptable.get(propIntKey, scriptable));
        } else {
            propValue = Context.toString(scriptable.get(propIdString, scriptable));
        }
        builder.append(propIdString);
        builder.append(": ");
        builder.append(propValue);
        builder.append("; ");
    }
    return builder.toString();
}
 
Example 8
Source File: Context.java    From JVoiceXML with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void dumpScope(final Scriptable scope, String pad) {
    java.lang.Object[] ids = scope.getIds();
    for (Object id : ids) {
        Object o = null;
        if (id instanceof String) {
            o = scope.get((String) id, scope);
            LOGGER.debug(pad + id + "=" + o + " ("
                    + o.getClass().getCanonicalName() + ")");
        } else {
            o = scope.get((int) id, scope);
            LOGGER.debug(pad + id + "=" + o + " ("
                    + o.getClass().getCanonicalName() + ")");
        }

        if (o instanceof Scriptable)
            dumpScope((Scriptable) o, pad + " ");
        else if (o instanceof NativeObject) {
            for (Map.Entry<Object, Object> item : ((NativeObject) o)
                    .entrySet()) {
                LOGGER.debug(pad + " " + item.getKey() + "="
                        + item.getValue() + " ("
                        + item.getValue().getClass().getCanonicalName()
                        + ")");
            }
        }
    }
}
 
Example 9
Source File: Utils.java    From JVoiceXML with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void dumpScope(final Scriptable scope, String pad) {
    java.lang.Object[] ids = scope.getIds();
    for (Object id : ids) {
        Object o = null;
        if (id instanceof String) {
            o = scope.get((String) id, scope);
            Logger.getRootLogger().debug(
                    pad + id + "=" + o + " ("
                            + o.getClass().getCanonicalName() + ")");
        } else {
            o = scope.get((int) id, scope);
            Logger.getRootLogger().debug(
                    pad + id + "=" + o + " ("
                            + o.getClass().getCanonicalName() + ")");
        }

        if (o instanceof Scriptable)
            dumpScope((Scriptable) o, pad + " ");
        else if (o instanceof NativeObject) {
            for (Map.Entry<Object, Object> item : ((NativeObject) o)
                    .entrySet()) {
                Logger.getRootLogger().debug(
                        pad
                                + " "
                                + item.getKey()
                                + "="
                                + item.getValue()
                                + " ("
                                + item.getValue().getClass()
                                        .getCanonicalName() + ")");
            }
        }
    }
}
 
Example 10
Source File: EcmaScriptDataModel.java    From JVoiceXML with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int copyValues(final DataModel model) throws SemanticError {
    if (topmostScope == null) {
        return ERROR_SCOPE_NOT_FOUND;
    }
    
    // Find all scopes in the right order.
    Scriptable current = topmostScope;
    List<Scriptable> scopeStack = new java.util.LinkedList<Scriptable>();
    while (current != null) {
        scopeStack.add(0, current);
        current = current.getParentScope();
    }
    
    // Copy all values per scope.
    for (Scriptable scriptable : scopeStack) {
        final Scope scope = scopes.get(scriptable);
        model.createScope(scope);
        final Object[] ids = scriptable.getIds();
        for (Object id : ids) {
            final String name = id.toString();
            final Object value = readVariable(name, scope, Object.class);
            model.createVariable(name, value, scope);
        }
    }
    return 0;
}
 
Example 11
Source File: JsFunction.java    From spork with Apache License 2.0 5 votes vote down vote up
private DataBag jsToPigBag(Scriptable array, Schema schema, int depth) throws FrontendException, ExecException {
    debugConvertJSToPig(depth, "Bag", array, schema);
    if (schema.size() == 1 && schema.getField(0).type == DataType.TUPLE) {
        schema = schema.getField(0).schema;
    }
    List<Tuple> bag = new ArrayList<Tuple>();
    for (Object id : array.getIds()) {
        Scriptable arrayValue = (Scriptable)array.get(((Integer)id).intValue(), null);
        bag.add(jsToPigTuple(arrayValue, schema, depth + 1));
    }
    DataBag result = BagFactory.getInstance().newDefaultBag(bag);
    debugReturn(depth, result);
    return result;
}
 
Example 12
Source File: ProviderFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Object[] compileScript(Context cx, String scriptStr, Scriptable scriptScope, File f) {
    int opt = cx.getOptimizationLevel();
    cx.setOptimizationLevel(-1);
    Script script = cx.compileString(scriptStr, f.getName(), 1, null);
    script.exec(cx, scriptScope);
    Object[] ids = scriptScope.getIds();
    cx.setOptimizationLevel(opt);
    script = cx.compileString(scriptStr, f.getName(), 1, null);
    script.exec(cx, scriptScope);
    return ids;
}