Java Javascript Mozilla Library jsToJava(Object jsObject)

Here you can find the source of jsToJava(Object jsObject)

Description

js To Java

License

Apache License

Declaration

public static Object jsToJava(Object jsObject) 

Method Source Code


//package com.java2s;
/*//www  . j  a v a  2s.c  o m
 * Copyright (c) 2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.HashMap;
import java.util.Map;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.NativeJavaObject;
import org.mozilla.javascript.NativeObject;

public class Main {
    public static Object jsToJava(Object jsObject) {
        if (jsObject == null)
            return null;
        if (jsObject == org.mozilla.javascript.Context.getUndefinedValue())
            return null;
        if (jsObject instanceof String)
            return jsObject;
        if (jsObject instanceof Boolean)
            return jsObject;
        if (jsObject instanceof Integer)
            return jsObject;
        if (jsObject instanceof Long)
            return jsObject;
        if (jsObject instanceof Float)
            return jsObject;
        if (jsObject instanceof Double)
            return jsObject;
        if (jsObject instanceof NativeArray)
            return convertArray((NativeArray) jsObject);
        if (jsObject instanceof NativeObject)
            return convertObject((NativeObject) jsObject);
        if (jsObject instanceof NativeJavaObject)
            return ((NativeJavaObject) jsObject).unwrap();
        return jsObject;
    }

    private static Object[] convertArray(NativeArray jsArray) {
        Object[] ids = jsArray.getIds();
        Object[] result = new Object[ids.length];
        for (int i = 0; i < ids.length; i++) {
            Object id = ids[i];
            int index = (Integer) id;
            Object jsValue = jsArray.get(index, jsArray);
            result[i] = jsToJava(jsValue);
        }
        return result;
    }

    private static Object convertObject(NativeObject jsObject) {
        Object[] ids = jsObject.getIds();
        Map result = new HashMap(ids.length);
        for (Object id : ids) {
            if (id instanceof String) {
                Object jsValue = jsObject.get((String) id, jsObject);
                result.put(id, jsToJava(jsValue));
            } else if (id instanceof Integer) {
                Object jsValue = jsObject.get((Integer) id, jsObject);
                result.put(id, jsToJava(jsValue));
            } else
                throw new AssertionError();
        }
        return result;
    }
}

Related

  1. isSimplePropertyGet(PropertyGet pg, String expectedObj, String expectedField)
  2. isStaticProperty(Scriptable property)
  3. isValid(Object[] args)
  4. isVariable(Name name)
  5. javaToJS(Object o, Scriptable scope)
  6. jsToJava(Object obj)
  7. listToNativeArray(List list)
  8. mapJSToJava(final Object jsObject)
  9. mapToNativeObject(Map map)