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

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

Introduction

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

Prototype

@Override
    public String getClassName() 

Source Link

Usage

From source file:com.bytelightning.opensource.pokerface.ScriptResponseProducer.java

License:Open Source License

/**
 * Helper to convert an object returned as an Http Header value from a JavaScript endpoint and convert it into a usable String value for an <code>HttpResponse</code>
 * Specifically Date objects (java or ecma) are converted to a GMT string, and all other objects are converted by invoking their <code>toString</code> method.
 *///  w  w w. j av  a2s  . c o m
private static String ConvertHeaderToString(Object value) {
    if (value instanceof Number)
        return ((Number) value).toString();
    else if (value instanceof Date)
        return Utils.GetHTTPDateFormater().format((Date) value);
    else if (value instanceof ScriptObjectMirror) {
        ScriptObjectMirror som = (ScriptObjectMirror) value;
        String ecmaClass = som.getClassName();
        if ("Date".equals(ecmaClass)) {
            Number num = (Number) som.callMember("getTime");
            Date d = new Date(num.longValue());
            return Utils.GetHTTPDateFormater().format(d);
        }
        return null;
    } else
        return value.toString();
}

From source file:org.nuxeo.automation.scripting.internals.ScriptObjectMirrors.java

License:Apache License

public static Object unwrap(ScriptObjectMirror jso) {
    if (jso.isArray()) {
        return unwrapList(jso);
    } else if (JAVASCRIPT_MAP_CLASS_TYPE.equals(jso.getClassName())) {
        return unwrapMap(jso);
    } else if (JAVASCRIPT_DATE_CLASS_TYPE.equals(jso.getClassName())) {
        return unwrapDate(jso);
    } else if (JAVASCRIPT_GLOBAL_CLASS_TYPE.equals(jso.getClassName())) {
        return null;
    } else if (JAVASCRIPT_FUNCTION_CLASS_TYPE.equals(jso.getClassName())) {
        return null;
    } else {//from w ww .java 2s .c o  m
        throw new UnsupportedOperationException(jso.getClassName() + " is not supported!");
    }
}

From source file:org.nuxeo.automation.scripting.internals.ScriptObjectMirrors.java

License:Apache License

public static Map<String, Object> unwrapMap(ScriptObjectMirror jso) {
    if (!JAVASCRIPT_MAP_CLASS_TYPE.equals(jso.getClassName())) {
        throw new IllegalArgumentException("JavaScript input is not an Object!");
    }//w  ww . j  a  v a2  s .c  o m
    Map<String, Object> result = new HashMap<>();
    for (String k : jso.keySet()) {
        Object o = jso.get(k);
        if (o instanceof ScriptObjectMirror) {
            result.put(k, unwrap((ScriptObjectMirror) o));
        } else {
            result.put(k, DocumentScriptingWrapper.unwrap(o));
        }
    }
    return result;
}

From source file:org.nuxeo.automation.scripting.internals.ScriptObjectMirrors.java

License:Apache License

/**
 * @since 8.4/* ww w  .  jav a  2 s  .c o  m*/
 */
public static Calendar unwrapDate(ScriptObjectMirror jso) {
    if (!JAVASCRIPT_DATE_CLASS_TYPE.equals(jso.getClassName())) {
        throw new IllegalArgumentException("JavaScript input is not a Date!");
    }
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(((Double) jso.callMember("getTime")).longValue());
    return cal;
}

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

License:Open Source License

public static Long parseTime(Object value) throws UnsupportedConversionException {
    if (value instanceof Double) {
        return ((Double) value).longValue();
    } else if (value instanceof String) {
        try {/*from   w w  w.j a v a 2s  .c o m*/
            return sdfTime.parse((String) value).getTime();
        } catch (ParseException e) {
            throw new UnsupportedConversionException("unmatched datetime format " + value, e);
        }
    } else if (value instanceof NativeDate) {
        return getTime((NativeDate) value);
    } else if (value instanceof ZonedDateTime) {
        return ((ZonedDateTime) value).toInstant().toEpochMilli();
    } else if (value instanceof ScriptObjectMirror) {
        ScriptObjectMirror m = (ScriptObjectMirror) value;
        Object o = m.to(Object.class);
        if (o instanceof NativeDate) {
            return getTime(m.to(NativeDate.class));
        } else if (o instanceof ZonedDateTime) {
            return ((ZonedDateTime) o).toInstant().toEpochMilli();
        } else {
            throw new UnsupportedConversionException("unknown date format " + value + " " + m.getClassName(),
                    null);
        }
    } else {
        throw new UnsupportedConversionException("unknown date format " + value + " " + value.getClass(), null);
    }
}

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

License:Open Source License

public static Long parseDate(Object value) throws UnsupportedConversionException {
    if (JsTypeUtil.isNull(value)) {
        return null;
    }//w ww.  j  a  v  a  2s.com
    if (value instanceof Double) {
        return ((Double) value).longValue();
    } else if (value instanceof String) {
        try {
            return sdfDate.parse((String) value).getTime();
        } catch (ParseException e) {
            throw new UnsupportedConversionException("unmatched datetime format " + value, e);
        }
    } else if (value instanceof NativeDate) {
        return getTime((NativeDate) value);
    } else if (value instanceof ScriptObjectMirror
            && ((ScriptObjectMirror) value).to(Object.class) instanceof NativeDate) {
        ScriptObjectMirror m = (ScriptObjectMirror) value;
        Object o = m.to(Object.class);
        if (o instanceof NativeDate) {
            return getTime(m.to(NativeDate.class));
        } else if (o instanceof ZonedDateTime) {
            return ((ZonedDateTime) o).toInstant().toEpochMilli();
        } else {
            throw new UnsupportedConversionException("unknown date format " + value + " " + m.getClassName(),
                    null);
        }
    } else if (value instanceof ZonedDateTime) {
        return ((ZonedDateTime) value).toInstant().toEpochMilli();
    } else {
        throw new UnsupportedConversionException("unknown date format " + value + " " + value.getClass(), null);
    }
}

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

License:Open Source License

private Long parseTime(Object value) throws SqlExecutorException {
    if (value instanceof Double) {
        return ((Double) value).longValue();
    } else if (value instanceof String) {
        try {/* ww  w  . j a v a 2  s. co m*/
            return sdfTime.parse((String) value).getTime();
        } catch (ParseException e) {
            throw new SqlExecutorException("unmatched datetime format " + value, e);
        }
    } else if (value instanceof NativeDate) {
        return jsTypeUtil.getTime((NativeDate) value);
    } else if (value instanceof ScriptObjectMirror) {
        ScriptObjectMirror m = (ScriptObjectMirror) value;
        if (m.to(Object.class) instanceof NativeDate) {
            return jsTypeUtil.getTime(m.to(NativeDate.class));
        } else {
            throw new SqlExecutorException("unknown date format " + value + " " + m.getClassName());
        }
    } else {
        throw new SqlExecutorException("unknown date format " + value + " " + value.getClass());
    }
}

From source file:Runner.AdaptorEmail.java

public void send(ScriptObjectMirror mirror) {

    System.out.println(mirror.getClassName() + ": " + Arrays.toString(mirror.getOwnKeys(true)));
    String to = "";
    String cc = "";
    String subject = "";
    String body = "";
    String htmlEmail = "";
    to = (String) mirror.get("to");
    cc = (String) mirror.get("cc");
    subject = (String) mirror.get("subject");
    body = (String) mirror.get("body");
    htmlEmail = (String) mirror.get("htmlEmail");

    if (to.isEmpty() || subject.isEmpty() || body.isEmpty() || !htmlEmail.equalsIgnoreCase("true")) {
        throw new IllegalArgumentException("email.send received bad parameters");
    }//ww  w.ja v a 2s  .  co  m
    Log.info("AdaptorEmail: " + to + " || " + cc + " || " + subject + " || " + body + " || " + htmlEmail);
}