Example usage for jdk.nashorn.internal.runtime ScriptObject get

List of usage examples for jdk.nashorn.internal.runtime ScriptObject get

Introduction

In this page you can find the example usage for jdk.nashorn.internal.runtime ScriptObject get.

Prototype

@Override
    public Object get(final int key) 

Source Link

Usage

From source file:com.asual.lesscss.compiler.NashornCompiler.java

License:Apache License

private Exception parseLessException(Exception root) {
    logger.debug("Parsing LESS Exception", root);
    if (root instanceof ECMAException) {
        ECMAException e = (ECMAException) root;
        Object thrown = e.getThrown();
        String type = null;//from w w  w  .j  a  va2 s  .  c  o m
        String message = null;
        String filename = null;
        int line = -1;
        int column = -1;
        List<String> extractList = new ArrayList<String>();
        if (thrown instanceof ScriptObject) {
            ScriptObject so = (ScriptObject) e.getThrown();
            type = so.get("type").toString() + " Error";
            message = so.get("message").toString();
            filename = "";
            if (so.has("filename")) {
                filename = so.get("filename").toString();
            }
            if (so.has("line")) {
                line = ((Long) so.get("line")).intValue();
            }
            if (so.has("column")) {
                column = ((Double) so.get("column")).intValue();
            }
            if (so.has("extract")) {
                NativeArray extract = (NativeArray) so.get("extract");
                for (int i = 0; i < extract.size(); i++) {
                    if (extract.get(i) instanceof String) {
                        extractList.add(((String) extract.get(i)).replace("\t", " "));
                    }
                }
            }
        } else {
            type = thrown.getClass().getSimpleName() + " Error";
            message = e.getMessage().replaceFirst("[^:]+: ", "");
        }
        return new LessException(message, type, filename, line, column, extractList);
    }
    return root;
}

From source file:com.mckoi.mwpui.nodejs.nashorn.NashornJSSystem.java

License:Open Source License

@Override
public ByteBuffer getExternalArrayDataOf(GJSObject source) {

    ScriptObjectMirror nashorn_ob = (ScriptObjectMirror) source.internalGetNative();
    // We use the unwrapped nashorn object as a key,
    ScriptObject script_ob = (ScriptObject) instance_global.unwrap(nashorn_ob);

    Object value = script_ob.get(BYTEBUFFER_OBJECT_PROPERTY_NAME);
    if (value == null || Undefined.getUndefined().equals(value)) {
        return null;
    }/*w  ww.  ja  v a2s  .  co  m*/
    return (ByteBuffer) value;

}

From source file:com.mongodb.jvm.json.nashorn.BinaryTransformer.java

License:Apache License

public Object transform(Object object, JsonImplementation implementation) {
    if (object instanceof ScriptObject) {
        ScriptObject scriptObject = (ScriptObject) object;

        Object binary = scriptObject.get("$binary");
        if ((binary != null) && (binary.getClass() != Undefined.class)) {
            Object type = scriptObject.get("$type");
            byte typeNumber = ((type != null) && (type.getClass() != Undefined.class))
                    ? Byte.valueOf(type.toString(), 16)
                    : 0;/*from w  w w  .  ja  v a  2 s  .  c o m*/
            byte[] data = Base64.decodeFast(binary.toString());
            return new Binary(typeNumber, data);
        }
    }

    return null;
}

From source file:com.mongodb.jvm.json.nashorn.BsonTimestampTransformer.java

License:Apache License

public Object transform(Object object, JsonImplementation implementation) {
    if (object instanceof ScriptObject) {
        ScriptObject scriptObject = (ScriptObject) object;

        Object timestamp = scriptObject.get("$timestamp");
        if (timestamp instanceof ScriptObject) {
            ScriptObject timestampScriptObject = (ScriptObject) timestamp;

            Object time = timestampScriptObject.get("t");
            Object inc = timestampScriptObject.get("i");
            if (time instanceof NativeNumber)
                time = ((NativeNumber) time).getValue();
            if (inc instanceof NativeNumber)
                inc = ((NativeNumber) inc).getValue();
            if ((time instanceof Number) && (inc instanceof Number))
                return new BsonTimestamp(((Number) time).intValue(), ((Number) inc).intValue());
        }//  w  w  w.  j  ava  2 s  .  c o m
    }

    return null;
}

From source file:com.mongodb.jvm.json.nashorn.DBRefTransformer.java

License:Apache License

public Object transform(Object object, JsonImplementation implementation) {
    if (object instanceof ScriptObject) {
        ScriptObject scriptObject = (ScriptObject) object;

        Object ref = scriptObject.get("$ref");
        if ((ref != null) && (ref.getClass() != Undefined.class)) {
            BsonDocument dbRef = Bson.to(scriptObject);
            BsonValue id = dbRef.get("$id");
            if (id != null)
                // Note: the id must be in BSON types!
                return new DBRef(ref.toString(), id);
        }//w w w .j  a v  a 2  s .  c om
    }

    return null;
}

From source file:com.mongodb.jvm.json.nashorn.NativeRegExpTransformer.java

License:Apache License

public Object transform(Object object, JsonImplementation implementation) {
    if (object instanceof ScriptObject) {
        ScriptObject scriptObject = (ScriptObject) object;

        Object regex = scriptObject.get("$regex");
        if ((regex != null) && (regex.getClass() != Undefined.class)) {
            Object options = scriptObject.get("$options");
            if ((options != null) && (options.getClass() != Undefined.class))
                NativeRegExp.constructor(true, null, regex.toString(), options.toString());
            else//  ww w.ja  v a 2  s.  com
                NativeRegExp.constructor(true, null, regex.toString());
        }
    }

    return null;
}

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

License:Open Source License

@Override
public void serialize(ScriptObject value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonGenerationException {

    if (value instanceof NativeDate) {
        if (dateSerializer == null) {
            dateSerializer = (StdDelegatingSerializer) new StdDelegatingSerializer(ScriptObject.class,
                    new StdConverter<ScriptObject, String>() {
                        @Override
                        public String convert(ScriptObject value) {
                            return NativeDate.toJSON(value, null).toString();
                        }/*from   ww  w.  ja  v  a  2s. com*/
                    }).createContextual(provider, null);
        }
        dateSerializer.serialize(value, jgen, provider);
    } else if (value instanceof ScriptFunction) {
        if (functionSerializer == null) {
            functionSerializer = (StdDelegatingSerializer) new StdDelegatingSerializer(ScriptObject.class,
                    new StdConverter<ScriptObject, Object>() {
                        @Override
                        public Object convert(ScriptObject value) {
                            return null;
                        }
                    }).createContextual(provider, null);
        }
        functionSerializer.serialize(value, jgen, provider);
    } else if (value.isArray()) {
        if (arraySerializer == null) {
            arraySerializer = (StdDelegatingSerializer) new StdDelegatingSerializer(ScriptObject.class,
                    new StdConverter<ScriptObject, Collection<Object>>() {
                        @Override
                        public Collection<Object> convert(ScriptObject value) {
                            return value.values();
                        }
                    }).createContextual(provider, null);
        }
        arraySerializer.serialize(value, jgen, provider);
    } else {
        if (objectSerializer == null) {
            objectSerializer = (StdDelegatingSerializer) new StdDelegatingSerializer(ScriptObject.class,
                    new StdConverter<ScriptObject, Map<String, Object>>() {
                        @Override
                        public Map<String, Object> convert(ScriptObject value) {
                            Map<String, Object> convertedMap = new LinkedHashMap<String, Object>();
                            value.propertyIterator().forEachRemaining(k -> convertedMap.put(k, value.get(k)));
                            return convertedMap;
                        }
                    }).createContextual(provider, null);
        }
        objectSerializer.serialize(value, jgen, provider);
    }
}

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

License:Mozilla Public License

public void encode(Object object, JsonContext context) throws IOException {
    ScriptObject scriptObject = (ScriptObject) object;

    context.out.append('{');

    String[] keys = scriptObject.getOwnKeys(true);
    int length = keys.length;
    if (length > 0) {
        context.newline();//from  w w w  . j  a  v  a2 s.  c  om

        for (int i = 0; i < length; i++) {
            String key = keys[i];
            Object value = scriptObject.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('}');
}

From source file:com.threecrickets.scripturian.adapter.NashornAdapter.java

License:LGPL

@Override
public Object enter(String entryPointName, Executable executable, ExecutionContext executionContext,
        Object... arguments) throws NoSuchMethodException, ParsingException, ExecutionException {
    ScriptObject oldGlobal = Context.getGlobal();
    try {/*  ww  w. j a  v  a 2 s  . co m*/
        ScriptObject globalScope = getGlobalScope(executionContext);

        Object entryPoint = globalScope.get(entryPointName);
        if (!(entryPoint instanceof ScriptFunction))
            throw new NoSuchMethodException(entryPointName);

        try {
            ScriptFunction function = (ScriptFunction) entryPoint;
            Object r = ScriptRuntime.apply(function, null, arguments);
            if (r instanceof NativeArray)
                r = NativeJava.to(null, r, "java.util.List");
            return r;
        } catch (ClassNotFoundException x) {
            throw new ExecutionException(executable.getDocumentName(), x);
        } catch (Throwable x) {
            throw createExecutionException(x, executable.getDocumentName());
        } finally {
            context.getOut().flush();
            context.getErr().flush();
        }
    } finally {
        if (oldGlobal != null)
            Context.setGlobal(oldGlobal);
    }
}

From source file:io.lightlink.output.JSONResponseStream.java

License:Open Source License

@Override
public synchronized void writeFullObjectToArray(Object value) {
    beginIfNeeded();//  w  w w  .jav  a2 s.c om

    comma();

    if (value != null && value instanceof jdk.nashorn.internal.runtime.ScriptObject) {
        ScriptObject so = (ScriptObject) value;
        if (so.isArray()) {
            writeArrayStart();
            int length = ((Number) so.get("length")).intValue();
            for (int i = 0; i < length; i++) {
                writeFullObjectToArray(genericDateConvert(so.get(i)));
            }
            writeArrayEnd();
        } else {
            writeObjectStart();
            for (Object key : so.keySet()) {
                writeProperty("" + key, genericDateConvert(so.get(key)));
            }
            writeObjectEnd();
        }
        return;

    } else if (value != null && value instanceof jdk.nashorn.api.scripting.JSObject) {
        JSObject jsObject = (JSObject) value;
        if (jsObject.isArray()) {
            writeArrayStart();
            int length = ((Number) jsObject.getMember("length")).intValue();
            for (int i = 0; i < length; i++) {
                writeFullObjectToArray(genericDateConvert(jsObject.getSlot(i)));
            }
            writeArrayEnd();
        } else {
            writeObjectStart();
            for (String key : jsObject.keySet()) {
                writeProperty(key, genericDateConvert(jsObject.getMember(key)));
            }
            writeObjectEnd();
        }
        return;
    } else if (value != null && value instanceof jdk.nashorn.internal.objects.NativeArray) {
        NativeArray array = (NativeArray) value;
        value = array.asObjectArray();
    }

    List list = new ArrayList();
    value = handlePrimitiveArrays(value, list);
    if (value instanceof Map) {
        writeObjectStart();
        Map<Object, Object> map = (Map<Object, Object>) value;
        if (value instanceof BeanMap) {
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                if (!"class".equals(entry.getKey())) {
                    writeProperty(entry.getKey() + "", entry.getValue());
                }
            }
        } else {
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                writeProperty(entry.getKey() + "", entry.getValue());
            }
        }
        writeObjectEnd();
    } else if (value instanceof List) {
        writeArrayStart();
        for (Object o : (List) value) {
            writeFullObjectToArray(o);
        }
        writePropertyArrayEnd();
    } else if (value instanceof InputStream) {
        writeInputStream((InputStream) value);
    } else if (value instanceof Reader) {
        writeFromReader((Reader) value);
    } else if (value instanceof Object[]) {
        writeArrayStart();
        for (Object o : (Object[]) value) {
            writeFullObjectToArray(o);
        }
        writeArrayEnd();
    } else if (value instanceof Date) {
        String dateFormat;
        if (getRunnerContext() != null && getRunnerContext().getTypesFacade().getCustomDatePattern() != null) {
            TypesFacade tf = getRunnerContext().getTypesFacade();
            dateFormat = tf.getCustomDatePattern();
        } else
            dateFormat = DateConverter.UNIVERSAL_DATE_FORMAT;

        writeString(new SimpleDateFormat(dateFormat).format(value));

    } else if (value == null) {

        writeUnquoted("null");

    } else if (value instanceof Number || value instanceof Boolean) {

        writeUnquoted(value);

    } else {

        writeString(value.toString());

    }

    commaNeeded = true;

}