jdk/src/share/classes/com/sun/script/javascript/RhinoScriptEngineFactory.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7800 cf50efba2e1d
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7800
diff changeset
     2
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.script.javascript;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import javax.script.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.org.mozilla.javascript.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.script.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Factory to create RhinoScriptEngine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Mike Grogan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class RhinoScriptEngineFactory extends ScriptEngineFactoryBase {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public RhinoScriptEngineFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public List<String> getExtensions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        return extensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public List<String> getMimeTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        return mimeTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public List<String> getNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public Object getParameter(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (key.equals(ScriptEngine.NAME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            return "javascript";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        } else if (key.equals(ScriptEngine.ENGINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            return "Mozilla Rhino";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        } else if (key.equals(ScriptEngine.ENGINE_VERSION)) {
7800
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    61
            return "1.7 release 3 PRERELEASE";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        } else if (key.equals(ScriptEngine.LANGUAGE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            return "ECMAScript";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) {
7800
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
    65
            return "1.8";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        } else if (key.equals("THREADING")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return "MULTITHREADED";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            throw new IllegalArgumentException("Invalid key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public ScriptEngine getScriptEngine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        RhinoScriptEngine ret = new RhinoScriptEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        ret.setEngineFactory(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public String getMethodCallSyntax(String obj, String method, String... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        String ret = obj + "." + method + "(";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int len = args.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            ret += ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            ret += args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (i != len - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                ret += ",";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                ret += ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public String getOutputStatement(String toDisplay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int len = toDisplay.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        buf.append("print(\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            char ch = toDisplay.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            case '"':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                buf.append("\\\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            case '\\':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                buf.append("\\\\");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                buf.append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        buf.append("\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public String getProgram(String... statements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int len = statements.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        String ret = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            ret += statements[i] + ";";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
7800
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
   131
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        RhinoScriptEngineFactory fact = new RhinoScriptEngineFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        System.out.println(fact.getParameter(ScriptEngine.ENGINE_VERSION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
7800
cf50efba2e1d 6980447: Rhino JavaScript engine code in jdk-7 has to updated with the latest code from Rhino 1.7R3.
sundar
parents: 5506
diff changeset
   136
    */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private static List<String> names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private static List<String> mimeTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private static List<String> extensions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        names = new ArrayList<String>(6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        names.add("js");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        names.add("rhino");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        names.add("JavaScript");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        names.add("javascript");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        names.add("ECMAScript");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        names.add("ecmascript");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        names = Collections.unmodifiableList(names);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        mimeTypes = new ArrayList<String>(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        mimeTypes.add("application/javascript");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        mimeTypes.add("application/ecmascript");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        mimeTypes.add("text/javascript");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        mimeTypes.add("text/ecmascript");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        mimeTypes = Collections.unmodifiableList(mimeTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        extensions = new ArrayList<String>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        extensions.add("js");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        extensions = Collections.unmodifiableList(extensions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}