1 /* 2 * Copyright (C) 2016 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.googlecode.android_scripting.interpreter; 18 19 import com.googlecode.android_scripting.language.Language; 20 import com.googlecode.android_scripting.language.SupportedLanguages; 21 import com.googlecode.android_scripting.rpc.MethodDescriptor; 22 23 import java.io.File; 24 import java.util.ArrayList; 25 import java.util.Collection; 26 import java.util.HashMap; 27 import java.util.List; 28 import java.util.Map; 29 30 /** 31 * Combines all the execution-related specs of a particular interpreter installed in the system. 32 * This class is instantiated through a map received from a concrete InterpreterProfider. 33 * 34 */ 35 public class Interpreter implements InterpreterPropertyNames { 36 37 private String mExtension; 38 private String mName; 39 private String mNiceName; 40 private String mInteractiveCommand; 41 private String mScriptExecutionCommand; 42 private File mBinary; 43 private boolean mHasInteractiveMode; 44 private final List<String> mArguments; 45 private final Map<String, String> mEnvironment; 46 private Language mLanguage; 47 Interpreter()48 public Interpreter() { 49 mArguments = new ArrayList<String>(); 50 mEnvironment = new HashMap<String, String>(); 51 } 52 buildFromMaps(Map<String, String> data, Map<String, String> environment_variables, Map<String, String> arguments)53 public static Interpreter buildFromMaps(Map<String, String> data, 54 Map<String, String> environment_variables, Map<String, String> arguments) { 55 String extension = data.get(EXTENSION); 56 String name = data.get(NAME); 57 String niceName = data.get(NICE_NAME); 58 String binary = data.get(BINARY); 59 String interactiveCommand = data.get(INTERACTIVE_COMMAND); 60 String scriptCommand = data.get(SCRIPT_COMMAND); 61 Boolean hasInteractiveMode; 62 if (data.containsKey(HAS_INTERACTIVE_MODE)) { 63 hasInteractiveMode = Boolean.parseBoolean(data.get(HAS_INTERACTIVE_MODE)); 64 } else { 65 // Default to true so that older interpreter APKs that don't have this value define still 66 // work. 67 hasInteractiveMode = true; 68 } 69 Interpreter interpreter = new Interpreter(); 70 interpreter.setName(name); 71 interpreter.setNiceName(niceName); 72 interpreter.setExtension(extension); 73 interpreter.setBinary(new File(binary)); 74 interpreter.setInteractiveCommand(interactiveCommand); 75 interpreter.setScriptCommand(scriptCommand); 76 interpreter.setHasInteractiveMode(hasInteractiveMode); 77 interpreter.setLanguage(SupportedLanguages.getLanguageByExtension(extension)); 78 interpreter.putAllEnvironmentVariables(environment_variables); 79 interpreter.addAllArguments(arguments.values()); 80 return interpreter; 81 } 82 83 // TODO(damonkohler): This should take a List<String> since order is important. addAllArguments(Collection<String> arguments)84 private void addAllArguments(Collection<String> arguments) { 85 mArguments.addAll(arguments); 86 } 87 getArguments()88 List<String> getArguments() { 89 return mArguments; 90 } 91 putAllEnvironmentVariables(Map<String, String> environmentVariables)92 private void putAllEnvironmentVariables(Map<String, String> environmentVariables) { 93 mEnvironment.putAll(environmentVariables); 94 } 95 getEnvironmentVariables()96 public Map<String, String> getEnvironmentVariables() { 97 return mEnvironment; 98 } 99 setScriptCommand(String executeParameters)100 protected void setScriptCommand(String executeParameters) { 101 mScriptExecutionCommand = executeParameters; 102 } 103 getScriptCommand()104 public String getScriptCommand() { 105 return mScriptExecutionCommand; 106 } 107 setInteractiveCommand(String interactiveCommand)108 protected void setInteractiveCommand(String interactiveCommand) { 109 mInteractiveCommand = interactiveCommand; 110 } 111 getInteractiveCommand()112 public String getInteractiveCommand() { 113 return mInteractiveCommand; 114 } 115 setBinary(File binary)116 protected void setBinary(File binary) { 117 if (!binary.exists()) { 118 throw new RuntimeException("Binary " + binary + " does not exist!"); 119 } 120 mBinary = binary; 121 } 122 getBinary()123 public File getBinary() { 124 return mBinary; 125 } 126 setExtension(String extension)127 protected void setExtension(String extension) { 128 mExtension = extension; 129 } 130 setHasInteractiveMode(boolean hasInteractiveMode)131 protected void setHasInteractiveMode(boolean hasInteractiveMode) { 132 mHasInteractiveMode = hasInteractiveMode; 133 } 134 hasInteractiveMode()135 public boolean hasInteractiveMode() { 136 return mHasInteractiveMode; 137 } 138 getExtension()139 public String getExtension() { 140 return mExtension; 141 } 142 setName(String name)143 protected void setName(String name) { 144 mName = name; 145 } 146 getName()147 public String getName() { 148 return mName; 149 } 150 setNiceName(String niceName)151 protected void setNiceName(String niceName) { 152 mNiceName = niceName; 153 } 154 getNiceName()155 public String getNiceName() { 156 return mNiceName; 157 } 158 getContentTemplate()159 public String getContentTemplate() { 160 return mLanguage.getContentTemplate(); 161 } 162 setLanguage(Language language)163 protected void setLanguage(Language language) { 164 mLanguage = language; 165 } 166 getLanguage()167 public Language getLanguage() { 168 return mLanguage; 169 } 170 getRpcText(String content, MethodDescriptor rpc, String[] values)171 public String getRpcText(String content, MethodDescriptor rpc, String[] values) { 172 return mLanguage.getRpcText(content, rpc, values); 173 } 174 isInstalled()175 public boolean isInstalled() { 176 return mBinary.exists(); 177 } 178 isUninstallable()179 public boolean isUninstallable() { 180 return true; 181 } 182 }