1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache license, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the license for the specific language governing permissions and 15 * limitations under the license. 16 */ 17 18 package org.apache.commons.text.lookup; 19 20 import java.util.Objects; 21 22 import javax.script.ScriptEngine; 23 import javax.script.ScriptEngineManager; 24 25 import org.apache.commons.text.StringSubstitutor; 26 27 /** 28 * Executes the script with the given engine name. 29 * <p> 30 * Execute the script with the engine name in the format "EngineName:Script". 31 * </p> 32 * <p> 33 * For example: {@code "javascript:3 + 4"}. 34 * </p> 35 * <p> 36 * Using a {@link StringSubstitutor}: 37 * </p> 38 * 39 * <pre> 40 * StringSubstitutor.createInterpolator().replace("${script:javascript:3 + 4}")); 41 * </pre> 42 * 43 * @since 1.5 44 */ 45 final class ScriptStringLookup extends AbstractStringLookup { 46 47 /** 48 * Defines the singleton for this class. 49 */ 50 static final ScriptStringLookup INSTANCE = new ScriptStringLookup(); 51 52 /** 53 * No need to build instances for now. 54 */ 55 private ScriptStringLookup() { 56 // empty 57 } 58 59 /** 60 * Execute the script with the engine name in the format "EngineName:Script". Extra colons will be ignored. 61 * <p> 62 * For example: {@code "javascript:3 + 4"}. 63 * </p> 64 * 65 * @param key the engine:script to execute, may be null 66 * @return The value returned by the execution. 67 */ 68 @Override 69 public String lookup(final String key) { 70 if (key == null) { 71 return null; 72 } 73 final String[] keys = key.split(SPLIT_STR, 2); 74 final int keyLen = keys.length; 75 if (keyLen != 2) { 76 throw IllegalArgumentExceptions.format("Bad script key format [%s]; expected format is EngineName:Script.", 77 key); 78 } 79 final String engineName = keys[0]; 80 final String script = keys[1]; 81 try { 82 final ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName(engineName); 83 if (scriptEngine == null) { 84 throw new IllegalArgumentException("No script engine named " + engineName); 85 } 86 return Objects.toString(scriptEngine.eval(script), null); 87 } catch (final Exception e) { 88 throw IllegalArgumentExceptions.format(e, "Error in script engine [%s] evaluating script [%s].", engineName, 89 script); 90 } 91 } 92 93 }