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));
}
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;
}
Aggregations