ScriptStringLookup.java

  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. package org.apache.commons.text.lookup;

  18. import java.util.Objects;

  19. import javax.script.ScriptEngine;
  20. import javax.script.ScriptEngineManager;

  21. import org.apache.commons.text.StringSubstitutor;

  22. /**
  23.  * Executes the script with the given engine name.
  24.  * <p>
  25.  * Execute the script with the engine name in the format "EngineName:Script".
  26.  * </p>
  27.  * <p>
  28.  * For example: {@code "javascript:3 + 4"}.
  29.  * </p>
  30.  * <p>
  31.  * Using a {@link StringSubstitutor}:
  32.  * </p>
  33.  *
  34.  * <pre>
  35.  * StringSubstitutor.createInterpolator().replace("${script:javascript:3 + 4}"));
  36.  * </pre>
  37.  *
  38.  * @since 1.5
  39.  */
  40. final class ScriptStringLookup extends AbstractStringLookup {

  41.     /**
  42.      * Defines the singleton for this class.
  43.      */
  44.     static final ScriptStringLookup INSTANCE = new ScriptStringLookup();

  45.     /**
  46.      * No need to build instances for now.
  47.      */
  48.     private ScriptStringLookup() {
  49.         // empty
  50.     }

  51.     /**
  52.      * Execute the script with the engine name in the format "EngineName:Script". Extra colons will be ignored.
  53.      * <p>
  54.      * For example: {@code "javascript:3 + 4"}.
  55.      * </p>
  56.      *
  57.      * @param key the engine:script to execute, may be null
  58.      * @return The value returned by the execution.
  59.      */
  60.     @Override
  61.     public String lookup(final String key) {
  62.         if (key == null) {
  63.             return null;
  64.         }
  65.         final String[] keys = key.split(SPLIT_STR, 2);
  66.         final int keyLen = keys.length;
  67.         if (keyLen != 2) {
  68.             throw IllegalArgumentExceptions.format("Bad script key format [%s]; expected format is EngineName:Script.",
  69.                 key);
  70.         }
  71.         final String engineName = keys[0];
  72.         final String script = keys[1];
  73.         try {
  74.             final ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName(engineName);
  75.             if (scriptEngine == null) {
  76.                 throw new IllegalArgumentException("No script engine named " + engineName);
  77.             }
  78.             return Objects.toString(scriptEngine.eval(script), null);
  79.         } catch (final Exception e) {
  80.             throw IllegalArgumentExceptions.format(e, "Error in script engine [%s] evaluating script [%s].", engineName,
  81.                 script);
  82.         }
  83.     }

  84. }