Search in sources :

Example 1 with NativeError

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();
    }
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) XMLObject(org.mozilla.javascript.xml.XMLObject) NativeError(org.mozilla.javascript.NativeError) Scriptable(org.mozilla.javascript.Scriptable) IScriptable(com.servoy.j2db.scripting.IScriptable) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) ZipException(java.util.zip.ZipException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) BigInteger(java.math.BigInteger) NativeJavaMethod(org.mozilla.javascript.NativeJavaMethod) Record(com.servoy.j2db.dataprocessing.Record) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) JSONObject(org.json.JSONObject) XMLObject(org.mozilla.javascript.xml.XMLObject)

Aggregations

FoundSet (com.servoy.j2db.dataprocessing.FoundSet)1 Record (com.servoy.j2db.dataprocessing.Record)1 IScriptable (com.servoy.j2db.scripting.IScriptable)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 BigInteger (java.math.BigInteger)1 MalformedURLException (java.net.MalformedURLException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ZipException (java.util.zip.ZipException)1 JSONObject (org.json.JSONObject)1 NativeArray (org.mozilla.javascript.NativeArray)1 NativeError (org.mozilla.javascript.NativeError)1 NativeJavaMethod (org.mozilla.javascript.NativeJavaMethod)1 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)1 Scriptable (org.mozilla.javascript.Scriptable)1 XMLObject (org.mozilla.javascript.xml.XMLObject)1