1 /*
  2  * Copyright (c) 2020, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package pseudoScriptEngineCompilable;
 27 
 28 import javax.script.ScriptEngine;
 29 import javax.script.ScriptEngineFactory;
 30 
 31 import java.lang.StringBuilder;
 32 import java.util.Arrays;
 33 import java.util.List;
 34 
 35 
 36 public class RgfPseudoScriptEngineCompilableFactory implements ScriptEngineFactory {
 37     static final String       ENGINE_NAME       = "RgfPseudoScriptLanguageCompilable (SQTMC) 1.0.0";
 38     static final String       SHORT_ENGINE_NAME = "sqtmc";
 39     static final String       ENGINE_VERSION    = "100.20200228";
 40     static final List<String> EXTENSIONS        = Arrays.asList("sqtmc", "SQTMC");
 41     static final String       LANGUAGE_NAME     = "RgfPseudoScriptLanguageCompilable";
 42     static final String       LANGUAGE_VERSION  = "1.0.0.100.20200228";
 43     static final List<String> MIME_TYPES        = Arrays.asList("text/sqtmc", "application/x-sqtmc");
 44     static final String       THREADING         = "MULTITHREADED";
 45     static final List<String> ENGINE_NAMES      = Arrays.asList(SHORT_ENGINE_NAME, "RgfPseudoSLCompilable");
 46 
 47     public String getEngineName() {
 48         return ENGINE_NAME;
 49     }
 50 
 51     public String getEngineVersion() {
 52         return ENGINE_VERSION;
 53     }
 54 
 55     public List<String> getExtensions() {
 56         return EXTENSIONS;
 57     }
 58 
 59     public String getLanguageName() {
 60         return LANGUAGE_NAME;
 61     }
 62 
 63     public String getLanguageVersion() {
 64         return LANGUAGE_VERSION;
 65     }
 66 
 67     public String getName() {
 68         return ENGINE_NAME;
 69     }
 70 
 71     public String getMethodCallSyntax(String obj, String m, String... args) {
 72         return "obj~(m, ...) /* ooRexx style */ ";
 73     }
 74 
 75     public List<String> getMimeTypes() {
 76         return MIME_TYPES;
 77     }
 78 
 79     public List<String> getNames() {
 80         return ENGINE_NAMES;
 81     }
 82 
 83     public String getOutputStatement(String toDisplay) {
 84         String tmpDisplay = toStringLiteral(toDisplay);
 85         return "say " + tmpDisplay + " /* Rexx style (duplicate quotes within string) */ ";
 86     }
 87 
 88     String toStringLiteral(String toDisplay) {
 89         if (toDisplay == null) {
 90             return "\"\"";
 91         }
 92         return '"' + toDisplay.replace("\"","\"\"") + '"';
 93     }
 94 
 95     public Object getParameter(final String key) {
 96         switch (key) {
 97             case "THREADING":
 98                 return THREADING;
 99 
100             case ScriptEngine.NAME:
101                 return SHORT_ENGINE_NAME;
102 
103             case ScriptEngine.ENGINE:
104                 return ENGINE_NAME;
105 
106             case ScriptEngine.ENGINE_VERSION:
107                 return ENGINE_VERSION;
108 
109             case ScriptEngine.LANGUAGE:
110                 return LANGUAGE_NAME;
111 
112             case ScriptEngine.LANGUAGE_VERSION:
113                  return LANGUAGE_VERSION;
114 
115             default:
116                 return null;
117         }
118     }
119 
120     public String getProgram(String... statements) {
121         if (statements == null) {
122             return "";
123         }
124 
125         StringBuilder sb = new StringBuilder();
126         for (int i = 0; i < statements.length; i++) {
127             if (statements[i] == null) {
128                 sb.append("\tsay 'null'; /* Rexx style */ \n");
129             }
130             else {
131                 sb.append("\t" + statements[i] + ";\n");
132             }
133         }
134         return sb.toString();
135     }
136 
137     public ScriptEngine getScriptEngine() {
138         return new RgfPseudoScriptEngineCompilable();
139     }
140 }