Repository: commons-text
Updated Branches:
  refs/heads/master 1ed39a318 -> 89f1ccb28


[TEXT-136] Add a 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/89f1ccb2
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/89f1ccb2
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/89f1ccb2

Branch: refs/heads/master
Commit: 89f1ccb28613c54bd9fed865c68ac5dd771c8f9d
Parents: 1ed39a3
Author: Gary Gregory <garydgreg...@gmail.com>
Authored: Thu Aug 23 12:33:01 2018 -0600
Committer: Gary Gregory <garydgreg...@gmail.com>
Committed: Thu Aug 23 12:33:01 2018 -0600

----------------------------------------------------------------------
 src/changes/changes.xml                         |  1 +
 .../commons/text/lookup/FileStringLookup.java   | 80 ++++++++++++++++++++
 .../text/lookup/InterpolatorStringLookup.java   |  3 +
 .../text/lookup/StringLookupFactory.java        | 22 +++++-
 .../text/lookup/FileStringLookupTest.java       | 37 +++++++++
 5 files changed, 142 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-text/blob/89f1ccb2/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5a55406..fffc34a 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -52,6 +52,7 @@ The <action> type attribute can be add,update,fix,remove.
     <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>
+    <action issue="TEXT-136" type="update" dev="ggregory">Add a 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/89f1ccb2/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java 
b/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java
new file mode 100644
index 0000000..3afbf85
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java
@@ -0,0 +1,80 @@
+/*
+ * 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.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+/**
+ * Looks up keys from an XML document.
+ * <p>
+ * Looks up the value for a given key in the format "Charset:Path".
+ * </p>
+ * <p>
+ * For example: "UTF-8:com/domain/document.properties".
+ * </p>
+ *
+ * @since 1.5
+ */
+final class FileStringLookup extends AbstractStringLookup {
+
+    /**
+     * Defines the singleton for this class.
+     */
+    static final FileStringLookup INSTANCE = new FileStringLookup();
+
+    /**
+     * No need to build instances for now.
+     */
+    private FileStringLookup() {
+        // 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 charsetName = keys[0];
+        final String fileName = keys[1];
+        try {
+            return new String(Files.readAllBytes(Paths.get(fileName)), 
charsetName);
+        } catch (final Exception e) {
+            throw IllegalArgumentExceptions.format(e, "Error looking up File 
[%s] with Charset [%s].", fileName,
+                    charsetName);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/commons-text/blob/89f1ccb2/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 10175ef..ffd011e 100644
--- a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
@@ -34,6 +34,7 @@ import java.util.Map.Entry;
  * <li>"localhost" for the {@link LocalHostStringLookup}.</li>
  * <li>"xml" for the {@link XmlStringLookup}.</li>
  * <li>"properties" for the {@link PropertiesStringLookup}.</li>
+ * <li>"file" for the {@link FileStringLookup}.</li>
  * </ul>
  */
 class InterpolatorStringLookup extends AbstractStringLookup {
@@ -81,6 +82,7 @@ class InterpolatorStringLookup extends AbstractStringLookup {
      * <li>"xml" for the {@link XmlStringLookup}.</li>
      * <li>"properties" for the {@link PropertiesStringLookup}.</li>
      * <li>"script" for the {@link ScriptStringLookup}.</li>
+     * <li>"file" for the {@link FileStringLookup}.</li>
      * </ul>
      *
      * @param <V>
@@ -129,6 +131,7 @@ class InterpolatorStringLookup extends AbstractStringLookup 
{
             this.stringLookupMap.put("xml", XmlStringLookup.INSTANCE);
             this.stringLookupMap.put("properties", 
PropertiesStringLookup.INSTANCE);
             this.stringLookupMap.put("script", ScriptStringLookup.INSTANCE);
+            this.stringLookupMap.put("file", FileStringLookup.INSTANCE);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/commons-text/blob/89f1ccb2/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 16e9d03..2917e92 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -72,6 +72,7 @@ public final class StringLookupFactory {
      * <li>"xml" for the {@link XmlStringLookup}.</li>
      * <li>"properties" for the {@link PropertiesStringLookup}.</li>
      * <li>"script" for the {@link ScriptStringLookup}.</li>
+     * <li>"file" for the {@link FileStringLookup}.</li>
      * </ul>
      *
      * @return a new InterpolatorStringLookup.
@@ -94,6 +95,7 @@ public final class StringLookupFactory {
      * <li>"xml" for the {@link XmlStringLookup}.</li>
      * <li>"properties" for the {@link PropertiesStringLookup}.</li>
      * <li>"script" for the {@link ScriptStringLookup}.</li>
+     * <li>"file" for the {@link FileStringLookup}.</li>
      * </ul>
      *
      * @param <V>
@@ -120,6 +122,7 @@ public final class StringLookupFactory {
      * <li>"xml" for the {@link XmlStringLookup}.</li>
      * <li>"properties" for the {@link PropertiesStringLookup}.</li>
      * <li>"script" for the {@link ScriptStringLookup}.</li>
+     * <li>"file" for the {@link FileStringLookup}.</li>
      * </ul>
      *
      * @param defaultStringLookup
@@ -145,6 +148,7 @@ public final class StringLookupFactory {
      * <li>"xml" for the {@link XmlStringLookup}.</li>
      * <li>"properties" for the {@link PropertiesStringLookup}.</li>
      * <li>"script" for the {@link ScriptStringLookup}.</li>
+     * <li>"file" for the {@link FileStringLookup}.</li>
      * </ul>
      *
      * @param stringLookupMap
@@ -271,11 +275,27 @@ public final class StringLookupFactory {
      * For example: "javascript:\"HelloWorld\"".
      * </p>
      *
-     * @return the PropertiesStringLookup singleton instance.
+     * @return the ScriptStringLookup singleton instance.
      * @since 1.5
      */
     public StringLookup scriptStringLookup() {
         return ScriptStringLookup.INSTANCE;
     }
 
+    /**
+     * Returns the FileStringLookup singleton instance.
+     * <p>
+     * Looks up the value for the key in the format "CharsetName:Path".
+     * </p>
+     * <p>
+     * For example: "UTF-8:com/domain/document.properties".
+     * </p>
+     *
+     * @return the FileStringLookup singleton instance.
+     * @since 1.5
+     */
+    public StringLookup fileStringLookup() {
+        return FileStringLookup.INSTANCE;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/commons-text/blob/89f1ccb2/src/test/java/org/apache/commons/text/lookup/FileStringLookupTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/commons/text/lookup/FileStringLookupTest.java 
b/src/test/java/org/apache/commons/text/lookup/FileStringLookupTest.java
new file mode 100644
index 0000000..e2bb555
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/lookup/FileStringLookupTest.java
@@ -0,0 +1,37 @@
+/*
+ * 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.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class FileStringLookupTest {
+
+    @Test
+    public void testOne() throws Exception {
+        final byte[] expectedBytes = 
Files.readAllBytes(Paths.get("src/test/resources/document.properties"));
+        String expectedString = new String(expectedBytes, 
StandardCharsets.UTF_8);
+        Assertions.assertEquals(expectedString,
+                
FileStringLookup.INSTANCE.lookup("UTF-8:src/test/resources/document.properties"));
+    }
+
+}

Reply via email to