Java Javascript Mozilla Library scriptableObjectToString(Object scriptObject)

Here you can find the source of scriptableObjectToString(Object scriptObject)

Description

scriptable Object To String

License

Open Source License

Declaration

public static String scriptableObjectToString(Object scriptObject) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.NativeObject;

public class Main {
    public static String scriptableObjectToString(Object scriptObject) {
        String objectName = scriptObject.getClass().getSimpleName();

        if (objectName.equals("Double") || scriptObject.getClass().getSimpleName().equals("Integer")) {
            return scriptObject.toString();
        }/* www. ja  va  2 s  . co  m*/

        if (objectName.equals("NativeArray")) {
            return nativeArrayToString(scriptObject);
        }

        if (objectName.equals("NativeObject")) {
            return nativeObjectToString(scriptObject);
        }

        return javascriptScring(scriptObject.toString());
    }

    private static String nativeArrayToString(Object scriptObject) {
        List<Object> list = new ArrayList<Object>();
        NativeArray array = ((NativeArray) scriptObject);
        for (int i = 0; i < array.getLength(); i++) {
            Object object = array.get(i);
            list.add(scriptableObjectToString(object));
        }
        return list.toString();
    }

    private static String nativeObjectToString(Object scriptObject) {
        String ret = "{";
        NativeObject nativeObject = (NativeObject) scriptObject;
        Set<Object> keySet = nativeObject.keySet();
        for (Object object : keySet) {
            ret += scriptableObjectToString(object) + " : " + scriptableObjectToString(nativeObject.get(object))
                    + ", ";
        }
        ret += "}";
        return ret.replaceAll("(,) *}", "}");
    }

    private static String javascriptScring(String string) {
        return "'" + string + "'";
    }
}

Related

  1. parentOfType(AstNode node, Class type)
  2. prototypeCast(Scriptable s, Class type)
  3. removeLinkFromXhtml(Object xhtml, String link)
  4. runWithAllOptimizationLevels(final ContextAction action)
  5. runWithOptimizationLevel(final ContextAction action, final int optimizationLevel)
  6. stringToNumber(CharSequence string)
  7. stringValue(String name, Scriptable scope)
  8. toInt(Object obj, int defaultValue)
  9. toJavaBoolean(final Scriptable options, final String key)