Java Javascript Mozilla Library getJsArray(Scriptable scope, Vector v)

Here you can find the source of getJsArray(Scriptable scope, Vector v)

Description

get Js Array

License

LGPL

Parameter

Parameter Description
scope a parameter
v a parameter

Declaration

public static Scriptable getJsArray(Scriptable scope, Vector<?> v) 

Method Source Code


//package com.java2s;
/*//from w ww. j av a2  s  .  co 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.Enumeration;

import java.util.List;

import java.util.Vector;

import org.mozilla.javascript.Context;

import org.mozilla.javascript.Scriptable;

public class Main {
    /**
     * 
     * @param scope
     * @param v
     * @return
     */
    public static Scriptable getJsArray(Scriptable scope, Vector<?> v) {
        List<Object> list = new ArrayList<Object>();
        for (int i = 0; i < v.size(); i++) {
            if (v.get(i) != null)
                list.add(Context.toObject(v.get(i), scope));
        }
        Context cx = Context.getCurrentContext();
        return cx.newArray(scope, list.toArray());
    }

    /**
     * 
     * @param scope
     * @param en
     * @return
     */
    public static Scriptable getJsArray(Scriptable scope, Enumeration<?> en) {
        List<Object> list = new ArrayList<Object>();
        while (en.hasMoreElements()) {
            list.add(en.nextElement());
        }
        Context cx = Context.getCurrentContext();
        return cx.newArray(scope, list.toArray());
    }

    /**
     * 
     * @param scope
     * @param arr
     * @return
     */
    public static Scriptable getJsArray(Scriptable scope, Object[] arr) {
        Context cx = Context.getCurrentContext();
        if (arr == null)
            return cx.newArray(scope, 0);
        int length = arr.length;
        Scriptable array = cx.newArray(scope, length);
        for (int i = 0; i < length; i++) {
            if (arr[i] != null)
                array.put(i, array, Context.toObject(arr[i], scope));
        }
        return array;
    }
}

Related

  1. getClassOrObjectProto(Scriptable scope, String className)
  2. getDirectColRefExpr(Node refNode, boolean mode)
  3. getFunctionArgsString(FunctionNode fn)
  4. getGlobalContextForValidation()
  5. getJavaObject(Scriptable scope, String name)
  6. getMapArgument(Object[] args, int pos)
  7. getObjectArgument(Object[] args, int pos, Object defaultValue)
  8. getProperty(Scriptable obj, final String prop)
  9. getPropertyName(AstNode propKeyNode)