Java Javascript Mozilla Library from(Scriptable scriptable)

Here you can find the source of from(Scriptable scriptable)

Description

from

License

Mozilla Public License

Declaration

public static Object from(Scriptable scriptable) 

Method Source Code

//package com.java2s;
/**//w  ww .  ja  v  a 2 s  .  c  o  m
 * Copyright 2010-2013 Three Crickets LLC.
 * <p>
 * The contents of this file are subject to the terms of the Mozilla Public
 * License version 1.1: http://www.mozilla.org/MPL/MPL-1.1.html
 * <p>
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly from Three Crickets
 * at http://threecrickets.com/
 */

import java.util.Date;

import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.regexp.NativeRegExp;

public class Main {
    public static Object from(Scriptable scriptable) {
        String className = scriptable.getClassName();
        if (className.equals("Date")) {
            // Convert NativeDate to Date

            // (The NativeDate class is private in Rhino, but we can access
            // it like a regular object.)

            Object time = ScriptableObject.callMethod(scriptable, "getTime", null);
            if (time instanceof Number)
                return new Date(((Number) time).longValue());
        } else if (className.equals("String")) {
            // Unpack NativeString

            return scriptable.toString();
        }

        return null;
    }

    public static String[] from(NativeRegExp regExp) {
        Object source = ScriptableObject.getProperty(regExp, "source");

        Object isGlobal = ScriptableObject.getProperty(regExp, "global");
        Object isIgnoreCase = ScriptableObject.getProperty(regExp, "ignoreCase");
        Object isMultiLine = ScriptableObject.getProperty(regExp, "multiline");

        // Note: JVM pattern does not support a "g" flag. Also, compiling
        // the pattern here is a waste of time.
        //
        // int flags = 0;
        // if( ( isIgnoreCase instanceof Boolean ) && ( ( (Boolean)
        // isIgnoreCase ).booleanValue() ) )
        // flags |= Pattern.CASE_INSENSITIVE;
        // if( ( isMultiLine instanceof Boolean ) && ( ( (Boolean)
        // isMultiLine ).booleanValue() ) )
        // flags |= Pattern.MULTILINE;
        // return Pattern.compile( source.toString(), flags );

        String options = "";
        if ((isGlobal instanceof Boolean) && (((Boolean) isGlobal).booleanValue()))
            options += "g";
        if ((isIgnoreCase instanceof Boolean) && (((Boolean) isIgnoreCase).booleanValue()))
            options += "i";
        if ((isMultiLine instanceof Boolean) && (((Boolean) isMultiLine).booleanValue()))
            options += "m";

        return new String[] { source.toString(), options };
    }
}

Related

  1. createNativeArray(int length, Scriptable scope)
  2. createSecurityController()
  3. ensureValid(Object obj)
  4. enterContext()
  5. evaluateJSExpression(String expr, Context cx, Scriptable scope)
  6. functionArg(Object[] args, int pos, boolean required)
  7. getArray(Scriptable scope, NativeArray array)
  8. getClassOrObjectProto(Scriptable scope, String className)
  9. getDirectColRefExpr(Node refNode, boolean mode)