use of org.mozilla.javascript.NativeError in project servoy-client by Servoy.
the class Utils method getScriptableString.
/**
* Returns a js/json string representation of the given {@link Scriptable}
* @param scriptable
* @param processed map to prevent loops in graph
* @return the scriptable as string
*/
private static CharSequence getScriptableString(Scriptable scriptable, Map<Scriptable, CharSequence> processed) {
Context.enter();
try {
if (scriptable instanceof Record || scriptable instanceof FoundSet)
return scriptable.toString();
if (scriptable instanceof XMLObject || scriptable instanceof NativeError)
return scriptable.toString();
CharSequence processedString = processed.get(scriptable);
if (processedString != null) {
return processedString;
}
if (processed.size() > 10)
return scriptable.toString();
if (// $NON-NLS-1$
scriptable instanceof NativeArray)
// $NON-NLS-1$
processed.put(scriptable, "Array[SelfRef]");
else
// $NON-NLS-1$
processed.put(scriptable, "Object[SelfRef]");
Object[] ids = scriptable.getIds();
if (ids != null && ids.length > 0) {
StringBuilder sb = new StringBuilder();
if (scriptable instanceof NativeArray)
sb.append('[');
else
sb.append('{');
for (Object object : ids) {
if (!(object instanceof Integer)) {
sb.append(object);
sb.append(':');
}
Object value = null;
if (object instanceof String) {
value = scriptable.get((String) object, scriptable);
} else if (object instanceof Number) {
value = scriptable.get(((Number) object).intValue(), scriptable);
}
if (!(value instanceof NativeJavaMethod)) {
if (value instanceof Scriptable) {
sb.append(getScriptableString((Scriptable) value, processed));
} else {
sb.append(value);
}
sb.append(',');
}
}
sb.setLength(sb.length() - 1);
if (scriptable instanceof NativeArray)
sb.append(']');
else
sb.append('}');
processed.put(scriptable, sb);
return sb;
}
Object defaultValue;
try {
defaultValue = scriptable.getDefaultValue(String.class);
} catch (Exception e) {
defaultValue = null;
}
if (defaultValue == null)
defaultValue = scriptable.toString();
processed.put(scriptable, defaultValue.toString());
return defaultValue.toString();
} finally {
Context.exit();
}
}
Aggregations