1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  *
  26  *
  27  * This is script engine factory for dummyNashorn engine.
  28  */
  29 package jdk.dummyNashorn.api.scripting;
  30 import javax.script.*;
  31 import java.util.*;
  32 
  33 public class DummyNashornJSEngineFactory implements ScriptEngineFactory {
  34     public String getEngineName() {
  35         return "dummyNashorn";
  36     }
  37 
  38     public String getEngineVersion() {
  39         return "-1.0";
  40     }
  41 
  42     public List<String> getExtensions() {
  43         return extensions;
  44     }
  45 
  46     public String getLanguageName() {
  47         return "dummyNashorn";
  48     }
  49 
  50     public String getLanguageVersion() {
  51         return "-1.0";
  52     }
  53 
  54     public String getMethodCallSyntax(String obj, String m, String... args) {
  55         StringBuffer buf = new StringBuffer();
  56         buf.append("call " + m + " ");
  57         buf.append(" on " + obj + " with ");
  58         for (int i = 0; i < args.length; i++) {
  59             buf.append(args[i] + ", ");
  60         }
  61         buf.append(";");
  62         return buf.toString();
  63     }
  64 
  65     public List<String> getMimeTypes() {
  66         return mimeTypes;
  67     }
  68 
  69     public List<String> getNames() {
  70         return names;
  71     }
  72 
  73     public String getOutputStatement(String str) {
  74         return "output " + str;
  75     }
  76 
  77     public String getParameter(String key) {
  78         if (key.equals(ScriptEngine.ENGINE)) {
  79             return getEngineName();
  80         } else if (key.equals(ScriptEngine.ENGINE_VERSION)) {
  81             return getEngineVersion();
  82         } else if (key.equals(ScriptEngine.NAME)) {
  83             return getEngineName();
  84         } else if (key.equals(ScriptEngine.LANGUAGE)) {
  85             return getLanguageName();
  86         } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) {
  87             return getLanguageVersion();
  88         } else {
  89             return null;
  90         }
  91     }
  92 
  93     public String getProgram(String... statements) {
  94         StringBuffer buf = new StringBuffer();
  95         for (int i = 0; i < statements.length; i++) {
  96             buf.append(statements[i]);
  97         }
  98         return buf.toString();
  99     }
 100 
 101     public ScriptEngine getScriptEngine() {
 102         return new DummyNashornJSEngine();
 103     }
 104 
 105     private static List<String> names;
 106     private static List<String> extensions;
 107     private static List<String> mimeTypes;
 108     static {
 109         names = new ArrayList<String>(1);
 110         names.add("dummyNashorn");
 111         names.add("js");
 112         names = Collections.unmodifiableList(names);
 113         extensions = names;
 114         mimeTypes = new ArrayList<String>(0);
 115         mimeTypes = Collections.unmodifiableList(mimeTypes);
 116     }
 117 }