1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.googlecode.android_scripting.language; 18 19 import com.googlecode.android_scripting.Log; 20 21 import java.util.HashMap; 22 import java.util.Map; 23 24 public class SupportedLanguages { 25 26 private static enum KnownLanguage { 27 // SHELL(".sh", ShellLanguage.class), // We don't really support Shell language 28 HTML(".html", HtmlLanguage.class), BEANSHELL(".bsh", BeanShellLanguage.class), JAVASCRIPT( 29 ".js", JavaScriptLanguage.class), LUA(".lua", LuaLanguage.class), PERL(".pl", 30 PerlLanguage.class), PYTHON(".py", PythonLanguage.class), RUBY(".rb", RubyLanguage.class), 31 TCL(".tcl", TclLanguage.class), PHP(".php", PhpLanguage.class), SLEEP(".sl", 32 SleepLanguage.class), SQUIRREL(".nut", SquirrelLanguage.class); 33 34 private final String mmExtension; 35 private final Class<? extends Language> mmClass; 36 KnownLanguage(String ext, Class<? extends Language> clazz)37 private KnownLanguage(String ext, Class<? extends Language> clazz) { 38 mmExtension = ext; 39 mmClass = clazz; 40 } 41 getExtension()42 private String getExtension() { 43 return mmExtension; 44 } 45 getLanguageClass()46 private Class<? extends Language> getLanguageClass() { 47 return mmClass; 48 } 49 } 50 51 private static Map<String, Class<? extends Language>> sSupportedLanguages; 52 53 static { 54 sSupportedLanguages = new HashMap<String, Class<? extends Language>>(); 55 for (KnownLanguage language : KnownLanguage.values()) { language.getExtension()56 sSupportedLanguages.put(language.getExtension(), language.getLanguageClass()); 57 } 58 } 59 getLanguageByExtension(String extension)60 public static Language getLanguageByExtension(String extension) { 61 extension = extension.toLowerCase(); 62 if (!extension.startsWith(".")) { 63 throw new RuntimeException("Extension does not start with a dot: " + extension); 64 } 65 Language lang = null; 66 67 Class<? extends Language> clazz = sSupportedLanguages.get(extension); 68 if (clazz == null) { 69 clazz = Language.class; // revert to default language. 70 } 71 if (clazz != null) { 72 try { 73 lang = clazz.newInstance(); 74 } catch (IllegalAccessException e) { 75 Log.e(e); 76 } catch (InstantiationException e) { 77 Log.e(e); 78 } 79 } 80 return lang; 81 } 82 checkLanguageSupported(String name)83 public static boolean checkLanguageSupported(String name) { 84 String extension = name.toLowerCase(); 85 int index = extension.lastIndexOf('.'); 86 if (index < 0) { 87 extension = "." + extension; 88 } else if (index > 0) { 89 extension = extension.substring(index); 90 } 91 return sSupportedLanguages.containsKey(extension); 92 } 93 } 94