Home | History | Annotate | Download | only in interpreter
      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  * @author Damon Kohler (damonkohler (at) gmail.com)
     35  * @author Alexey Reznichenko (alexey.reznichenko (at) gmail.com)
     36  */
     37 public class Interpreter implements InterpreterPropertyNames {
     38 
     39   private String mExtension;
     40   private String mName;
     41   private String mNiceName;
     42   private String mInteractiveCommand;
     43   private String mScriptExecutionCommand;
     44   private File mBinary;
     45   private boolean mHasInteractiveMode;
     46   private final List<String> mArguments;
     47   private final Map<String, String> mEnvironment;
     48   private Language mLanguage;
     49 
     50   public Interpreter() {
     51     mArguments = new ArrayList<String>();
     52     mEnvironment = new HashMap<String, String>();
     53   }
     54 
     55   public static Interpreter buildFromMaps(Map<String, String> data,
     56       Map<String, String> environment_variables, Map<String, String> arguments) {
     57     String extension = data.get(EXTENSION);
     58     String name = data.get(NAME);
     59     String niceName = data.get(NICE_NAME);
     60     String binary = data.get(BINARY);
     61     String interactiveCommand = data.get(INTERACTIVE_COMMAND);
     62     String scriptCommand = data.get(SCRIPT_COMMAND);
     63     Boolean hasInteractiveMode;
     64     if (data.containsKey(HAS_INTERACTIVE_MODE)) {
     65       hasInteractiveMode = Boolean.parseBoolean(data.get(HAS_INTERACTIVE_MODE));
     66     } else {
     67       // Default to true so that older interpreter APKs that don't have this value define still
     68       // work.
     69       hasInteractiveMode = true;
     70     }
     71     Interpreter interpreter = new Interpreter();
     72     interpreter.setName(name);
     73     interpreter.setNiceName(niceName);
     74     interpreter.setExtension(extension);
     75     interpreter.setBinary(new File(binary));
     76     interpreter.setInteractiveCommand(interactiveCommand);
     77     interpreter.setScriptCommand(scriptCommand);
     78     interpreter.setHasInteractiveMode(hasInteractiveMode);
     79     interpreter.setLanguage(SupportedLanguages.getLanguageByExtension(extension));
     80     interpreter.putAllEnvironmentVariables(environment_variables);
     81     interpreter.addAllArguments(arguments.values());
     82     return interpreter;
     83   }
     84 
     85   // TODO(damonkohler): This should take a List<String> since order is important.
     86   private void addAllArguments(Collection<String> arguments) {
     87     mArguments.addAll(arguments);
     88   }
     89 
     90   List<String> getArguments() {
     91     return mArguments;
     92   }
     93 
     94   private void putAllEnvironmentVariables(Map<String, String> environmentVariables) {
     95     mEnvironment.putAll(environmentVariables);
     96   }
     97 
     98   public Map<String, String> getEnvironmentVariables() {
     99     return mEnvironment;
    100   }
    101 
    102   protected void setScriptCommand(String executeParameters) {
    103     mScriptExecutionCommand = executeParameters;
    104   }
    105 
    106   public String getScriptCommand() {
    107     return mScriptExecutionCommand;
    108   }
    109 
    110   protected void setInteractiveCommand(String interactiveCommand) {
    111     mInteractiveCommand = interactiveCommand;
    112   }
    113 
    114   public String getInteractiveCommand() {
    115     return mInteractiveCommand;
    116   }
    117 
    118   protected void setBinary(File binary) {
    119     if (!binary.exists()) {
    120       throw new RuntimeException("Binary " + binary + " does not exist!");
    121     }
    122     mBinary = binary;
    123   }
    124 
    125   public File getBinary() {
    126     return mBinary;
    127   }
    128 
    129   protected void setExtension(String extension) {
    130     mExtension = extension;
    131   }
    132 
    133   protected void setHasInteractiveMode(boolean hasInteractiveMode) {
    134     mHasInteractiveMode = hasInteractiveMode;
    135   }
    136 
    137   public boolean hasInteractiveMode() {
    138     return mHasInteractiveMode;
    139   }
    140 
    141   public String getExtension() {
    142     return mExtension;
    143   }
    144 
    145   protected void setName(String name) {
    146     mName = name;
    147   }
    148 
    149   public String getName() {
    150     return mName;
    151   }
    152 
    153   protected void setNiceName(String niceName) {
    154     mNiceName = niceName;
    155   }
    156 
    157   public String getNiceName() {
    158     return mNiceName;
    159   }
    160 
    161   public String getContentTemplate() {
    162     return mLanguage.getContentTemplate();
    163   }
    164 
    165   protected void setLanguage(Language language) {
    166     mLanguage = language;
    167   }
    168 
    169   public Language getLanguage() {
    170     return mLanguage;
    171   }
    172 
    173   public String getRpcText(String content, MethodDescriptor rpc, String[] values) {
    174     return mLanguage.getRpcText(content, rpc, values);
    175   }
    176 
    177   public boolean isInstalled() {
    178     return mBinary.exists();
    179   }
    180 
    181   public boolean isUninstallable() {
    182     return true;
    183   }
    184 }