001/*
002 * Copyright (C) 2006 Sun Microsystems, Inc. All rights reserved. 
003 * Use is subject to license terms.
004 *
005 * Redistribution and use in source and binary forms, with or without modification, are 
006 * permitted provided that the following conditions are met: Redistributions of source code 
007 * must retain the above copyright notice, this list of conditions and the following disclaimer.
008 * Redistributions in binary form must reproduce the above copyright notice, this list of 
009 * conditions and the following disclaimer in the documentation and/or other materials 
010 * provided with the distribution. Neither the name of the Sun Microsystems nor the names of 
011 * is contributors may be used to endorse or promote products derived from this software 
012 * without specific prior written permission. 
013
014 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
015 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
016 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 
017 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
018 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
019 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
020 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
021 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
022 * POSSIBILITY OF SUCH DAMAGE.
023 */
024
025/*
026 * JavaScriptEngineFactory.java
027 * @author A. Sundararajan
028 */
029
030package org.kepler.scriptengine.java;
031
032import java.util.ArrayList;
033import java.util.Collections;
034import java.util.List;
035
036import javax.script.ScriptEngine;
037import javax.script.ScriptEngineFactory;
038
039/**
040 * This is script engine factory for "Java" script engine.
041 */
042public class JavaScriptEngineFactory implements ScriptEngineFactory {
043    @Override
044        public String getEngineName() { 
045        return "java";
046    }
047
048    @Override
049        public String getEngineVersion() {
050        return "1.6";
051    }
052
053    @Override
054        public List<String> getExtensions() {
055        return extensions;
056    }
057
058    @Override
059        public String getLanguageName() {
060        return "java";
061    }
062
063    @Override
064        public String getLanguageVersion() {
065        return "1.6";
066    }
067
068    @Override
069        public String getMethodCallSyntax(String obj, String m, String... args) {
070        StringBuilder buf = new StringBuilder();
071        buf.append(obj);
072        buf.append(".");
073        buf.append(m);
074        buf.append("(");
075        if (args.length != 0) {
076            int i = 0;
077            for (; i < args.length - 1; i++) {
078                buf.append(args[i] + ", ");
079            }
080            buf.append(args[i]);
081        }        
082        buf.append(")");
083        return buf.toString();
084    }
085
086    @Override
087        public List<String> getMimeTypes() {
088        return mimeTypes;
089    }
090
091    @Override
092        public List<String> getNames() {
093        return names;
094    }
095
096    @Override
097        public String getOutputStatement(String toDisplay) {
098        StringBuilder buf = new StringBuilder();
099        buf.append("System.out.print(\"");
100        int len = toDisplay.length();
101        for (int i = 0; i < len; i++) {
102            char ch = toDisplay.charAt(i);
103            switch (ch) {
104            case '"':
105                buf.append("\\\"");
106                break;
107            case '\\':
108                buf.append("\\\\");
109                break;
110            default:
111                buf.append(ch);
112                break;
113            }
114        }
115        buf.append("\");");
116        return buf.toString();
117    }
118
119    @Override
120        public String getParameter(String key) {
121        if (key.equals(ScriptEngine.ENGINE)) {
122            return getEngineName();
123        } else if (key.equals(ScriptEngine.ENGINE_VERSION)) {
124            return getEngineVersion();
125        } else if (key.equals(ScriptEngine.NAME)) {
126            return getEngineName();
127        } else if (key.equals(ScriptEngine.LANGUAGE)) {
128            return getLanguageName();
129        } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) {
130            return getLanguageVersion();
131        } else if (key.equals("THREADING")) {
132            return "MULTITHREADED";
133        } else {
134            return null;
135        }
136    } 
137
138    @Override
139        public String getProgram(String... statements) {
140        // we generate a Main class with main method
141        // that contains all the given statements
142
143        StringBuilder buf = new StringBuilder();
144        buf.append("class ");
145        buf.append(getClassName());
146        buf.append(" {\n");
147        buf.append("    public static void main(String[] args) {\n");
148        if (statements.length != 0) {
149            for (int i = 0; i < statements.length; i++) {
150                buf.append("        ");
151                buf.append(statements[i]);
152                buf.append(";\n");
153            }
154        }
155        buf.append("    }\n");
156        buf.append("}\n");
157        return buf.toString();
158    }
159
160    @Override
161        public ScriptEngine getScriptEngine() {
162        JavaScriptEngine engine = new JavaScriptEngine();
163        engine.setFactory(this);
164        return engine;
165    }
166
167
168    // used to generate a unique class name in getProgram
169    private String getClassName() {
170        return "com_sun_script_java_Main$" + getNextClassNumber();
171    }
172
173    private static synchronized long getNextClassNumber() {
174        return nextClassNum++;
175    }
176
177    private static long nextClassNum = 0L;
178    private static List<String> names;
179    private static List<String> extensions;
180    private static List<String> mimeTypes;
181    static {
182        names = new ArrayList<String>(1);
183        names.add("java");
184        names = Collections.unmodifiableList(names);
185        extensions = names;
186        mimeTypes = new ArrayList<String>(0);
187        mimeTypes = Collections.unmodifiableList(mimeTypes);
188    }
189}