Java Javascript Mozilla Library nativeArrayToString(Object scriptObject)

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

Description

native Array To String

License

Open Source License

Declaration

private static String nativeArrayToString(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 {
    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));
        }//  w w w  .  j  a v a2  s  .co  m
        return list.toString();
    }

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

        if (objectName.equals("Double") || scriptObject.getClass().getSimpleName().equals("Integer")) {
            return scriptObject.toString();
        }

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

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

        return javascriptScring(scriptObject.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. jsToJava(Object jsObject)
  2. jsToJava(Object obj)
  3. listToNativeArray(List list)
  4. mapJSToJava(final Object jsObject)
  5. mapToNativeObject(Map map)
  6. newObject()
  7. numberArg(Object[] args, int pos)
  8. objArg(Object[] args, int pos, Class type, boolean required)
  9. objectToXMLString(Object xml)