Example usage for jdk.nashorn.internal.objects NativeRegExp source

List of usage examples for jdk.nashorn.internal.objects NativeRegExp source

Introduction

In this page you can find the example usage for jdk.nashorn.internal.objects NativeRegExp source.

Prototype

@Getter(attributes = Attribute.NON_ENUMERABLE_CONSTANT)
public static Object source(final Object self) 

Source Link

Document

ECMA 15.10.7.1 source

Usage

From source file:org.siphon.common.js.JsTypeUtil.java

License:Open Source License

private static Object jsObjectToJava(NativeRegExp arg) {
    NativeRegExp regexp = (NativeRegExp) arg;
    // ??flag??//from w  ww .j  a  va 2  s . co m
    return Pattern.compile((String) NativeRegExp.source(regexp));
}

From source file:org.siphon.jsmongo.MongoExecutor.java

License:Open Source License

private BsonValue jsValueToBson(Object arg) throws SqlExecutorException {

    if (JsTypeUtil.isNull(arg)) {
        return BsonNull.VALUE;
    }//from   w  ww .  j  a v  a2  s  . c  om

    String type = null;
    Object value = null;

    if (arg instanceof ScriptObjectMirror) {
        if (jsTypeUtil.isNativeDate(arg))
            return jsSimpleValueToBson(((ScriptObjectMirror) arg).to(NativeDate.class));

        ScriptObjectMirror atm = (ScriptObjectMirror) arg;

        if (atm.isArray()) {
            return nativeArrayToBson(atm.to(NativeArray.class));
        }

        if (!atm.containsKey("_d2js_type")) {
            return jsObjectToBsonDocument(atm);
        } else {
            type = atm.get("_d2js_type").toString();
            value = atm.get("value");
        }
    } else if (arg instanceof ScriptObject) {
        if (arg instanceof NativeDate)
            return jsSimpleValueToBson((NativeDate) arg);

        if (arg instanceof NativeArray) {
            return nativeArrayToBson((NativeArray) arg);
        }

        if (arg instanceof NativeRegExp) {
            return new BsonRegularExpression((String) NativeRegExp.source((NativeRegExp) arg));
        }

        if (arg instanceof ScriptFunction) {
            return new BsonString(((ScriptFunction) arg).toSource());
        }

        if (arg instanceof NativeString) {
            return jsSimpleValueToBson(NativeString.toString(arg));
        }

        ScriptObject obj = (ScriptObject) arg;
        if (!obj.containsKey("_d2js_type")) {
            return jsObjectToBsonDocument(obj);
        } else {
            type = obj.get("_d2js_type").toString();
            value = obj.get("value");
        }
    } else {
        return jsSimpleValueToBson(arg);
    }

    if ("STRING".equals(type)) {
        return new BsonString(value.toString());
    } else if ("DECIMAL".equals(type)) {
        if (value instanceof Double) {
            return new BsonDouble((double) value);
        } else {
            return new BsonDouble(Double.parseDouble(value + ""));
        }
    } else if ("INT".equals(type)) {
        if (value instanceof Double) {
            if (((Double) value).isNaN()) {
                return new BsonDouble(Double.NaN);
            } else {
                return new BsonInt32(((Double) value).intValue());
            }
        } else {
            return new BsonInt32(new Integer(value + ""));
        }
    } else if ("BOOLEAN".equals(type)) {
        return new BsonBoolean(JsTypeUtil.isTrue(arg));
    } else if ("DOUBLE".equals(type) || "FLOAT".equals(type)) {
        if (value instanceof Double) {
            return new BsonDouble((Double) value);
        } else {
            return new BsonDouble(new Double(value + ""));
        }
    } else if ("DATE".equals(type)) {
        long t = parseDate(value);
        return new BsonDateTime(t);
    } else if ("OBJECTID".equals(type)) {
        return new BsonObjectId(new ObjectId((String) value));
    } else if ("TIME".equals(type)) {
        return new BsonDateTime(parseTime(value));
    } else if ("BINARY".equals(type)) {
        return new BsonBinary(parseBinary(value));
    } else if ("CLOB".equals(type)) {
        return new BsonString(value.toString());
    } else if ("LONG".equals(type)) {
        if (value instanceof Double) {
            if (((Double) value).isNaN()) {
                return new BsonDouble(Double.NaN);
            } else {
                return new BsonInt64(((Double) value).longValue());
            }
        } else {
            return new BsonInt64(new Long(value + ""));
        }
    } else if ("REGEX".equals(type)) {
        return new BsonRegularExpression(value.toString());
    } else {
        throw new SqlExecutorException("unknown object type " + type + "  " + value);
    }
    // else if ("OUTCURSOR".equals(type)) {
    // } else if ("ARRAY".equals(type)) {
    // } else if ("JSON".equals(type) || "JSONB".equals(type)){
    // } else {
    // }
}