Java Javascript Mozilla Library unwrapNativeArray(final NativeArray na)

Here you can find the source of unwrapNativeArray(final NativeArray na)

Description

unwrap Native Array

License

LGPL

Parameter

Parameter Description
na a parameter

Declaration

public static List<Object> unwrapNativeArray(final NativeArray na) 

Method Source Code


//package com.java2s;
/*//  w  w w. j av  a2s  . c o  m
  ==============================================================================
    
   This file is part of the MOA Lightweight Web Runner
   Copyright 2008 by kRAkEn/gORe's Jucetice Application Development
    
  ------------------------------------------------------------------------------
    
   MOA can be redistributed and/or modified under the terms of the
   GNU Lesser General Public License, as published by the Free Software Foundation;
   version 2 of the License only.
    
   MOA is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with MOA; if not, visit www.gnu.org/licenses or write to the
   Free Software Foundation, Inc., 59 Temple Place, Suite 330,
   Boston, MA 02111-1307 USA
    
  ==============================================================================
*/

import java.util.ArrayList;
import java.util.Arrays;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mozilla.javascript.Callable;

import org.mozilla.javascript.NativeArray;

import org.mozilla.javascript.ScriptableObject;

public class Main {
    /**
     * 
     * @param na
     * @return
     */
    public static List<Object> unwrapNativeArray(final NativeArray na) {
        return new ArrayList<Object>() {
            {
                for (int i = 0; i < na.getLength(); ++i) {
                    add(unwrapNative(na.get(i, null)));
                }
            }
        };
    }

    /**
     * 
     * @param obj
     * @return
     */
    protected static Object unwrapNative(final Object obj) {
        if (obj instanceof NativeArray) {
            return unwrapNativeArray((NativeArray) obj);
        } else if (obj instanceof Callable) {
            return obj;
        } else if (obj instanceof ScriptableObject) {
            final ScriptableObject sObj = (ScriptableObject) obj;
            final List<Object> sObjIds = Arrays.asList(sObj.getAllIds());
            if (sObjIds.contains("keys")) { // a prototype enumerable/hash
                return unwrapObject(sObj);
            } else if (sObjIds.contains("flatten")) { // a prototype enumerable/array
                return unwrapPrototypeArray(sObj);
            } else {
                return unwrapObject(sObj);
            }
        } else {
            return obj;
        }
    }

    /**
     * 
     * @param sObj
     * @return
     */
    public static Map<String, Object> unwrapObject(final ScriptableObject sObj) {
        return new HashMap<String, Object>() {
            {
                for (Object id : sObj.getAllIds()) {
                    put(id.toString(), unwrapNative(sObj.get(id.toString(), null)));
                }
            }
        };
    }

    /**
     * 
     * @param na
     * @return
     */
    public static List<Object> unwrapPrototypeArray(final ScriptableObject sObj) {
        return new ArrayList<Object>() {
            {
                final List<Object> sObjIds = Arrays.asList(sObj.getAllIds());
                for (int i = 0; sObjIds.contains(i); ++i) {
                    add(unwrapNative(sObj.get(i, null)));
                }
            }
        };
    }
}

Related

  1. toRegExp(String source, String optionsString)
  2. toString(Object val)
  3. trace(Object object)
  4. traceHelper(Function function, Object... args)
  5. unwrap(Object obj)
  6. wrapJavaObj(Object javaObj, Scriptable scope)