You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2018/09/13 00:29:36 UTC

[text] [TEXT-140] Add a Base64 string lookup.

Repository: commons-text
Updated Branches:
  refs/heads/master 377ea047b -> 76b07d101


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

Branch: refs/heads/master
Commit: 76b07d101badf6e460664bd1e0f034ba6dd58121
Parents: 377ea04
Author: Gary Gregory <gg...@rocketsoftware.com>
Authored: Wed Sep 12 18:29:34 2018 -0600
Committer: Gary Gregory <gg...@rocketsoftware.com>
Committed: Wed Sep 12 18:29:34 2018 -0600

----------------------------------------------------------------------
 src/changes/changes.xml                         |  1 +
 .../commons/text/lookup/Base64StringLookup.java | 49 ++++++++++++++++++++
 .../text/lookup/InterpolatorStringLookup.java   |  2 +
 .../text/lookup/StringLookupFactory.java        | 15 ++++++
 .../text/lookup/Base64StringLookupTest.java     | 30 ++++++++++++
 5 files changed, 97 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-text/blob/76b07d10/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 8ff0a07..b58de61 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -54,6 +54,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action issue="TEXT-135" type="add" dev="ggregory">Add a script string lookup.</action>
     <action issue="TEXT-136" type="add" dev="ggregory">Add a file string lookup.</action>
     <action issue="TEXT-137" type="add" dev="ggregory">Add a URL string lookup.</action>
+    <action issue="TEXT-140" type="add" dev="ggregory">Add a Base64 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/76b07d10/src/main/java/org/apache/commons/text/lookup/Base64StringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/Base64StringLookup.java b/src/main/java/org/apache/commons/text/lookup/Base64StringLookup.java
new file mode 100644
index 0000000..39333b9
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/lookup/Base64StringLookup.java
@@ -0,0 +1,49 @@
+/*
+ * 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.Base64;
+
+/**
+ * Always returns null.
+ *
+ * @since 1.3
+ */
+final class Base64StringLookup extends AbstractStringLookup {
+
+    /**
+     * Defines the singleton for this class.
+     */
+    static final Base64StringLookup INSTANCE = new Base64StringLookup();
+
+    /**
+     * No need to build instances for now.
+     */
+    private Base64StringLookup() {
+        // empty
+    }
+
+    /**
+     * Always returns null.
+     */
+    @Override
+    public String lookup(final String key) {
+        return new String(Base64.getDecoder().decode(key));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/commons-text/blob/76b07d10/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 068ac0a..66134b3 100644
--- a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
@@ -65,6 +65,7 @@ class InterpolatorStringLookup extends AbstractStringLookup {
      * <li>"script" for the {@link ScriptStringLookup}.</li>
      * <li>"file" for the {@link FileStringLookup}.</li>
      * <li>"url" for the {@link UrlStringLookup}.</li>
+     * <li>"base64" for the {@link Base64StringLookup}.</li>
      * </ul>
      */
     InterpolatorStringLookup() {
@@ -87,6 +88,7 @@ class InterpolatorStringLookup extends AbstractStringLookup {
      * <li>"script" for the {@link ScriptStringLookup}.</li>
      * <li>"file" for the {@link FileStringLookup}.</li>
      * <li>"url" for the {@link UrlStringLookup}.</li>
+     * <li>"base64" for the {@link Base64StringLookup}.</li>
      * </ul>
      *
      * @param <V>

http://git-wip-us.apache.org/repos/asf/commons-text/blob/76b07d10/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 73a4398..b779cd1 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -51,6 +51,7 @@ public final class StringLookupFactory {
      * <li>"script" for the {@link ScriptStringLookup}.</li>
      * <li>"file" for the {@link FileStringLookup}.</li>
      * <li>"url" for the {@link UrlStringLookup}.</li>
+     * <li>"base64" for the {@link Base64StringLookup}.</li>
      * </ul>
      *
      * @param stringLookupMap
@@ -77,6 +78,16 @@ public final class StringLookupFactory {
      *
      * @return the DateStringLookup singleton instance.
      */
+    public StringLookup base64StringLookup() {
+        return Base64StringLookup.INSTANCE;
+    }
+
+    /**
+     * Returns the DateStringLookup singleton instance to format the current date with the format given in the key in a
+     * format compatible with {@link java.text.SimpleDateFormat}.
+     *
+     * @return the DateStringLookup singleton instance.
+     */
     public StringLookup dateStringLookup() {
         return DateStringLookup.INSTANCE;
     }
@@ -123,6 +134,7 @@ public final class StringLookupFactory {
      * <li>"script" for the {@link ScriptStringLookup}.</li>
      * <li>"file" for the {@link FileStringLookup}.</li>
      * <li>"url" for the {@link UrlStringLookup}.</li>
+     * <li>"base64" for the {@link Base64StringLookup}.</li>
      * </ul>
      *
      * @return a new InterpolatorStringLookup.
@@ -148,6 +160,7 @@ public final class StringLookupFactory {
      * <li>"script" for the {@link ScriptStringLookup}.</li>
      * <li>"file" for the {@link FileStringLookup}.</li>
      * <li>"url" for the {@link UrlStringLookup}.</li>
+     * <li>"base64" for the {@link Base64StringLookup}.</li>
      * </ul>
      *
      * @param stringLookupMap
@@ -180,6 +193,7 @@ public final class StringLookupFactory {
      * <li>"script" for the {@link ScriptStringLookup}.</li>
      * <li>"file" for the {@link FileStringLookup}.</li>
      * <li>"url" for the {@link UrlStringLookup}.</li>
+     * <li>"base64" for the {@link Base64StringLookup}.</li>
      * </ul>
      *
      * @param <V>
@@ -208,6 +222,7 @@ public final class StringLookupFactory {
      * <li>"script" for the {@link ScriptStringLookup}.</li>
      * <li>"file" for the {@link FileStringLookup}.</li>
      * <li>"url" for the {@link UrlStringLookup}.</li>
+     * <li>"base64" for the {@link Base64StringLookup}.</li>
      * </ul>
      *
      * @param defaultStringLookup

http://git-wip-us.apache.org/repos/asf/commons-text/blob/76b07d10/src/test/java/org/apache/commons/text/lookup/Base64StringLookupTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/lookup/Base64StringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/Base64StringLookupTest.java
new file mode 100644
index 0000000..19b61db
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/lookup/Base64StringLookupTest.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 Base64StringLookupTest {
+
+    @Test
+    public void test() {
+        Assertions.assertEquals("HelloWorld!", Base64StringLookup.INSTANCE.lookup("SGVsbG9Xb3JsZCE="));
+    }
+
+}