View Javadoc
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    *      https://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   * <p>
43   * Public access is through {@link StringLookupFactory}.
44   * </p>
45   *
46   * @see StringLookupFactory
47   * @since 1.5
48   */
49  final class ScriptStringLookup extends AbstractStringLookup {
50  
51      /**
52       * Defines the singleton for this class.
53       */
54      static final ScriptStringLookup INSTANCE = new ScriptStringLookup();
55  
56      /**
57       * No need to build instances for now.
58       */
59      private ScriptStringLookup() {
60          // empty
61      }
62  
63      /**
64       * Execute the script with the engine name in the format "EngineName:Script". Extra colons will be ignored.
65       * <p>
66       * For example: {@code "javascript:3 + 4"}.
67       * </p>
68       *
69       * @param key the engine:script to execute, may be null.
70       * @return The value returned by the execution.
71       */
72      @Override
73      public String lookup(final String key) {
74          if (key == null) {
75              return null;
76          }
77          final String[] keys = key.split(SPLIT_STR, 2);
78          final int keyLen = keys.length;
79          if (keyLen != 2) {
80              throw IllegalArgumentExceptions.format("Bad script key format [%s]; expected format is EngineName:Script.",
81                  key);
82          }
83          final String engineName = keys[0];
84          final String script = keys[1];
85          try {
86              final ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName(engineName);
87              if (scriptEngine == null) {
88                  throw new IllegalArgumentException("No script engine named " + engineName);
89              }
90              return Objects.toString(scriptEngine.eval(script), null);
91          } catch (final Exception e) {
92              throw IllegalArgumentExceptions.format(e, "Error in script engine [%s] evaluating script [%s].", engineName,
93                  script);
94          }
95      }
96  
97  }