Search in sources :

Example 1 with NativeDate

use of org.mozilla.javascript.NativeDate in project servoy-client by Servoy.

the class RhinoDefaultConversionsTest method testSimpleValuesDefaultConversions.

@Test
public void testSimpleValuesDefaultConversions() {
    Object rhinoVal, javaVal;
    Date date = new Date();
    // DATE -----------------------
    // toRhino
    rhinoVal = RhinoConversion.defaultToRhino(date, objectPD, component, someRhinoScope);
    someRhinoScope.put("a", someRhinoScope, rhinoVal);
    assertSame("Date does not get translated to native rhino date by conversion as Rhino will do that automatically later", date, rhinoVal);
    rhinoVal = ScriptRuntime.toObject(rhinoContext, someRhinoScope, rhinoVal);
    assertSame("Now date - gets translated to native rhino date by internal Rhino code", NativeDate.class, rhinoVal.getClass());
    assertEquals("NativeDate and Date should represent the same moment in time", Double.valueOf(date.getTime()), rhinoContext.evaluateString(someRhinoScope, "a.getTime()", "dummy js file name from junit tests", 0, null));
    // fromRhino
    javaVal = RhinoConversion.defaultFromRhino(rhinoVal, null);
    assertSame("Converted to and from date is again a java date", Date.class, javaVal.getClass());
    assertEquals("Converted to and from date is equal to original", date, javaVal);
    // Nulls / undefined  -----------------------
    // toRhino
    assertNull("Null to rhino null", RhinoConversion.defaultToRhino(null, objectPD, component, someRhinoScope));
    assertNull("JSONObject.NULL to rhino null", RhinoConversion.defaultToRhino(JSONObject.NULL, objectPD, component, someRhinoScope));
    // fromRhino
    assertNull("Scriptable.NOT_FOUND to java null", RhinoConversion.defaultFromRhino(Scriptable.NOT_FOUND, null));
    assertNull("Undefined.instance to java null", RhinoConversion.defaultFromRhino(Undefined.instance, null));
    assertNull("Undefined.SCRIPTABLE_UNDEFINED to java null", RhinoConversion.defaultFromRhino(Undefined.SCRIPTABLE_UNDEFINED, null));
}
Also used : NativeObject(org.mozilla.javascript.NativeObject) JSONObject(org.json.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) Date(java.util.Date) NativeDate(org.mozilla.javascript.NativeDate) Test(org.junit.Test)

Example 2 with NativeDate

use of org.mozilla.javascript.NativeDate in project servoy-client by Servoy.

the class RhinoConversion method defaultFromRhino.

/**
 * Default conversion used to convert from Rhino property types that do not explicitly implement component <-> Rhino conversions. <BR/><BR/>
 * Values of types that don't implement the sablo <-> rhino conversions are by default accessible directly.
 */
@SuppressWarnings("unchecked")
public static // PropertyDescription / IWebObjectContext ... can be made available here if needed
Object defaultFromRhino(// PropertyDescription / IWebObjectContext ... can be made available here if needed
Object propertyValue, // PropertyDescription / IWebObjectContext ... can be made available here if needed
Object oldValue) {
    // convert simple values to json values
    if (isUndefinedOrNotFound(propertyValue)) {
        return null;
    }
    if (propertyValue instanceof NativeDate) {
        return ((NativeDate) propertyValue).unwrap();
    }
    if (propertyValue instanceof NativeObject) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> oldMap = (oldValue instanceof Map) ? (Map<String, Object>) oldValue : null;
        NativeObject no = (NativeObject) propertyValue;
        Object[] ids = no.getIds();
        for (Object id2 : ids) {
            Object value = null;
            if (id2 instanceof String) {
                value = no.get((String) id2, no);
            } else if (id2 instanceof Symbol) {
                value = no.get((Symbol) id2, no);
            } else if (id2 instanceof Number) {
                value = no.get(((Number) id2).intValue(), no);
            }
            String id = String.valueOf(id2);
            map.put(id, defaultFromRhino(value, oldMap != null ? oldMap.get(id) : null));
        }
        return map;
    }
    if (propertyValue instanceof NativeArray) {
        List<Object> list = new ArrayList<Object>();
        List<Object> oldList = (oldValue instanceof List) ? (List<Object>) oldValue : null;
        final NativeArray no = (NativeArray) propertyValue;
        final long naLength = no.getLength();
        for (int id = 0; id < naLength; id++) {
            list.add(defaultFromRhino(no.get(id, no), oldList != null && oldList.size() > id ? oldList.get(id) : null));
        }
        return list;
    }
    if (propertyValue instanceof FormScope && ((FormScope) propertyValue).getFormController() != null)
        return ((FormScope) propertyValue).getFormController().getName();
    if (propertyValue instanceof IFormController)
        return ((IFormController) propertyValue).getName();
    if (propertyValue instanceof JSDataSet) {
        return ((JSDataSet) propertyValue).getDataSet();
    }
    if (propertyValue instanceof RhinoMapOrArrayWrapper) {
        return ((RhinoMapOrArrayWrapper) propertyValue).getWrappedValue();
    }
    if (propertyValue instanceof CharSequence) {
        return propertyValue.toString();
    }
    return propertyValue;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) HashMap(java.util.HashMap) Symbol(org.mozilla.javascript.Symbol) ArrayList(java.util.ArrayList) JSDataSet(com.servoy.j2db.dataprocessing.JSDataSet) FormScope(com.servoy.j2db.scripting.FormScope) NativeObject(org.mozilla.javascript.NativeObject) NativeDate(org.mozilla.javascript.NativeDate) NativeObject(org.mozilla.javascript.NativeObject) JSONObject(org.json.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) ArrayList(java.util.ArrayList) List(java.util.List) IFormController(com.servoy.j2db.IFormController) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JSONObject (org.json.JSONObject)2 NativeDate (org.mozilla.javascript.NativeDate)2 NativeObject (org.mozilla.javascript.NativeObject)2 ScriptableObject (org.mozilla.javascript.ScriptableObject)2 IFormController (com.servoy.j2db.IFormController)1 JSDataSet (com.servoy.j2db.dataprocessing.JSDataSet)1 FormScope (com.servoy.j2db.scripting.FormScope)1 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Test (org.junit.Test)1 NativeArray (org.mozilla.javascript.NativeArray)1 Symbol (org.mozilla.javascript.Symbol)1