Repository: commons-text
Updated Branches:
  refs/heads/master 6c52769fe -> 3363205b6


[TEXT-135] Add a script file string lookup.

Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/3363205b
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/3363205b
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/3363205b

Branch: refs/heads/master
Commit: 3363205b673093d52d51719d5f9371376dad991b
Parents: 6c52769
Author: Gary Gregory <[email protected]>
Authored: Thu Aug 23 12:10:42 2018 -0600
Committer: Gary Gregory <[email protected]>
Committed: Thu Aug 23 12:10:42 2018 -0600

----------------------------------------------------------------------
 src/changes/changes.xml                         |  1 +
 .../text/lookup/InterpolatorStringLookup.java   |  3 +
 .../commons/text/lookup/ScriptStringLookup.java | 86 ++++++++++++++++++++
 .../text/lookup/StringLookupFactory.java        | 20 +++++
 .../text/lookup/ScriptStringLookupTest.java     | 30 +++++++
 src/test/resources/document.properties          | 15 ++++
 6 files changed, 155 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-text/blob/3363205b/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7a86040..5a55406 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -51,6 +51,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action issue="TEXT-132" type="update" dev="ggregory">Update Apache 
Commons Lang from 3.7 to 3.8.</action>
     <action issue="TEXT-133" type="update" dev="ggregory">Add a XML file XPath 
string lookup.</action>
     <action issue="TEXT-134" type="update" dev="ggregory">Add a Properties 
file string lookup.</action>
+    <action issue="TEXT-135" type="update" dev="ggregory">Add a script file 
string lookup.</action>
   </release>
 
   <release version="1.4" date="2018-06-12" description="Release 1.4">

http://git-wip-us.apache.org/repos/asf/commons-text/blob/3363205b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java 
b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
index 95fac6b..10175ef 100644
--- a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
@@ -60,6 +60,7 @@ class InterpolatorStringLookup extends AbstractStringLookup {
      * <li>"localhost" for the {@link LocalHostStringLookup}.</li>
      * <li>"xml" for the {@link XmlStringLookup}.</li>
      * <li>"properties" for the {@link PropertiesStringLookup}.</li>
+     * <li>"script" for the {@link ScriptStringLookup}.</li>
      * </ul>
      */
     InterpolatorStringLookup() {
@@ -79,6 +80,7 @@ class InterpolatorStringLookup extends AbstractStringLookup {
      * <li>"localhost" for the {@link LocalHostStringLookup}.</li>
      * <li>"xml" for the {@link XmlStringLookup}.</li>
      * <li>"properties" for the {@link PropertiesStringLookup}.</li>
+     * <li>"script" for the {@link ScriptStringLookup}.</li>
      * </ul>
      *
      * @param <V>
@@ -126,6 +128,7 @@ class InterpolatorStringLookup extends AbstractStringLookup 
{
             this.stringLookupMap.put("localhost", 
LocalHostStringLookup.INSTANCE);
             this.stringLookupMap.put("xml", XmlStringLookup.INSTANCE);
             this.stringLookupMap.put("properties", 
PropertiesStringLookup.INSTANCE);
+            this.stringLookupMap.put("script", ScriptStringLookup.INSTANCE);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/commons-text/blob/3363205b/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java 
b/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java
new file mode 100644
index 0000000..7156a4c
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/lookup/ScriptStringLookup.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.commons.text.lookup;
+
+import java.util.Objects;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+
+/**
+ * Looks up keys from an XML document.
+ * <p>
+ * Looks up the value for a given key in the format "Document:Key".
+ * </p>
+ * <p>
+ * For example: "com/domain/document.properties:key".
+ * </p>
+ *
+ * @since 1.5
+ */
+final class ScriptStringLookup extends AbstractStringLookup {
+
+    /**
+     * Defines the singleton for this class.
+     */
+    static final ScriptStringLookup INSTANCE = new ScriptStringLookup();
+
+    /**
+     * No need to build instances for now.
+     */
+    private ScriptStringLookup() {
+        // empty
+    }
+
+    /**
+     * Looks up the value for the key in the format "DocumentPath:XPath".
+     * <p>
+     * For example: "com/domain/document.xml:/path/to/node".
+     * </p>
+     * 
+     * @param key
+     *            the key to be looked up, may be null
+     * @return The value associated with the key.
+     */
+    @Override
+    public String lookup(final String key) {
+        if (key == null) {
+            return null;
+        }
+        final String[] keys = key.split(":");
+        final int keyLen = keys.length;
+        if (keyLen != 2) {
+            throw IllegalArgumentExceptions
+                    .format("Bad Properties key format [%s]. Expected format 
is DocumentPath:Key.", key);
+        }
+        final String engineName = keys[0];
+        final String script = keys[1];
+        try {
+            final ScriptEngine scriptEngine = new 
ScriptEngineManager().getEngineByName(engineName);
+            if (scriptEngine == null) {
+                throw new IllegalArgumentException("No script engine named " + 
engineName);
+            }
+            final Object eval = scriptEngine.eval(script);
+            return Objects.toString(eval, null);
+        } catch (final Exception e) {
+            throw IllegalArgumentExceptions.format(e, "Error looking up 
Properties [%s] and Key [%s].", engineName,
+                    script);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/commons-text/blob/3363205b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java 
b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
index 6b13e38..16e9d03 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -71,6 +71,7 @@ public final class StringLookupFactory {
      * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link 
#localHostStringLookup()} for key names.</li>
      * <li>"xml" for the {@link XmlStringLookup}.</li>
      * <li>"properties" for the {@link PropertiesStringLookup}.</li>
+     * <li>"script" for the {@link ScriptStringLookup}.</li>
      * </ul>
      *
      * @return a new InterpolatorStringLookup.
@@ -92,6 +93,7 @@ public final class StringLookupFactory {
      * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link 
#localHostStringLookup()} for key names.</li>
      * <li>"xml" for the {@link XmlStringLookup}.</li>
      * <li>"properties" for the {@link PropertiesStringLookup}.</li>
+     * <li>"script" for the {@link ScriptStringLookup}.</li>
      * </ul>
      *
      * @param <V>
@@ -117,6 +119,7 @@ public final class StringLookupFactory {
      * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link 
#localHostStringLookup()} for key names.</li>
      * <li>"xml" for the {@link XmlStringLookup}.</li>
      * <li>"properties" for the {@link PropertiesStringLookup}.</li>
+     * <li>"script" for the {@link ScriptStringLookup}.</li>
      * </ul>
      *
      * @param defaultStringLookup
@@ -141,6 +144,7 @@ public final class StringLookupFactory {
      * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link 
#localHostStringLookup()} for key names.</li>
      * <li>"xml" for the {@link XmlStringLookup}.</li>
      * <li>"properties" for the {@link PropertiesStringLookup}.</li>
+     * <li>"script" for the {@link ScriptStringLookup}.</li>
      * </ul>
      *
      * @param stringLookupMap
@@ -258,4 +262,20 @@ public final class StringLookupFactory {
         return PropertiesStringLookup.INSTANCE;
     }
 
+    /**
+     * Returns the ScriptStringLookup singleton instance.
+     * <p>
+     * Looks up the value for the key in the format "ScriptEngineName:Script".
+     * </p>
+     * <p>
+     * For example: "javascript:\"HelloWorld\"".
+     * </p>
+     *
+     * @return the PropertiesStringLookup singleton instance.
+     * @since 1.5
+     */
+    public StringLookup scriptStringLookup() {
+        return ScriptStringLookup.INSTANCE;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/commons-text/blob/3363205b/src/test/java/org/apache/commons/text/lookup/ScriptStringLookupTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/commons/text/lookup/ScriptStringLookupTest.java 
b/src/test/java/org/apache/commons/text/lookup/ScriptStringLookupTest.java
new file mode 100644
index 0000000..9e67f86
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/lookup/ScriptStringLookupTest.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.commons.text.lookup;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class ScriptStringLookupTest {
+
+    @Test
+    public void testOne() {
+        Assertions.assertEquals("Hello World!", 
ScriptStringLookup.INSTANCE.lookup("javascript:\"Hello World!\""));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/commons-text/blob/3363205b/src/test/resources/document.properties
----------------------------------------------------------------------
diff --git a/src/test/resources/document.properties 
b/src/test/resources/document.properties
index 1016853..f411d5e 100644
--- a/src/test/resources/document.properties
+++ b/src/test/resources/document.properties
@@ -1 +1,16 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
 mykey = Hello World!
\ No newline at end of file

Reply via email to