Java Javascript Mozilla Library mapJSToJava(final Object jsObject)

Here you can find the source of mapJSToJava(final Object jsObject)

Description

map JS To Java

License

LGPL

Declaration

public static Object mapJSToJava(final Object jsObject) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import org.mozilla.javascript.NativeArray;

public class Main {
    public static Object mapJSToJava(final Object jsObject) {
        // Most Native* types from Rhino are automatically converted, or
        // something. Arrays, however, definately are not. This code will map
        // JS arrays into Java ones. It tries to use sensible types, as well.

        if (jsObject instanceof NativeArray) {
            final NativeArray ary = (NativeArray) jsObject;
            final int aryLen = (int) ary.getLength();

            final Object[] aryO = new Object[aryLen];
            final String[] aryS = new String[aryLen];
            final boolean[] aryB = new boolean[aryLen];
            final double[] aryN = new double[aryLen];
            boolean isStringArray = true;
            boolean isBooleanArray = true;
            boolean isNumberArray = true;

            for (int i = 0; i < aryLen; i++) {
                final Object item = ary.get(i, ary);

                aryO[i] = mapJSToJava(item);

                if (isStringArray) {
                    if (item instanceof String) {
                        aryS[i] = (String) item;
                    } else {
                        isStringArray = false;
                    }/*www .ja  v a2 s  .c  om*/
                }
                if (isBooleanArray) {
                    if (item instanceof Boolean) {
                        aryB[i] = ((Boolean) item).booleanValue();
                    } else {
                        isBooleanArray = false;
                    }
                }
                if (isNumberArray) {
                    if (item instanceof Number) {
                        aryN[i] = ((Number) item).doubleValue();
                    } else {
                        isNumberArray = false;
                    }
                }
            }

            if (isStringArray) {
                return aryS;
            }
            if (isBooleanArray) {
                return aryB;
            }
            if (isNumberArray) {
                return aryN;
            }
            return aryO;
        }
        return jsObject;
    }
}

Related

  1. isVariable(Name name)
  2. javaToJS(Object o, Scriptable scope)
  3. jsToJava(Object jsObject)
  4. jsToJava(Object obj)
  5. listToNativeArray(List list)
  6. mapToNativeObject(Map map)
  7. nativeArrayToString(Object scriptObject)
  8. newObject()
  9. numberArg(Object[] args, int pos)