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 2022/05/31 12:23:09 UTC

[commons-text] 01/03: Alias lookup keys in a property files instead of hard-coding it

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git

commit 7e23858d9babf6eef02d9ef9451dcaa7a2500c9b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue May 31 08:17:03 2022 -0400

    Alias lookup keys in a property files instead of hard-coding it
---
 .../commons/text/lookup/DefaultStringLookup.java   | 10 ++++++
 .../commons/text/lookup/StringLookupFactory.java   | 39 +++++++++++++++++-----
 .../apache/commons/text/lookup/defaults.properties | 38 +++++++++++++++++++++
 .../apache/commons/text/lookup/defaults.properties | 38 +++++++++++++++++++++
 4 files changed, 117 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/commons/text/lookup/DefaultStringLookup.java b/src/main/java/org/apache/commons/text/lookup/DefaultStringLookup.java
index 628ec493..520ed384 100644
--- a/src/main/java/org/apache/commons/text/lookup/DefaultStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/DefaultStringLookup.java
@@ -16,6 +16,8 @@
  */
 package org.apache.commons.text.lookup;
 
+import java.util.stream.Stream;
+
 /**
  * An enumeration defining {@link StringLookup} objects available through {@link StringLookupFactory}.
  * <p>
@@ -115,6 +117,14 @@ public enum DefaultStringLookup {
      */
     XML(StringLookupFactory.KEY_XML, StringLookupFactory.INSTANCE.xmlStringLookup());
 
+    /**
+     * Prints out to the console the mapping from enum keys to enum name.
+     * @param args ignored.
+     */
+    public static void main(String[] args) {
+        Stream.of(values()).forEach(e -> System.out.println(e.getKey() + "=" + e.name()));
+    }
+
     /** The prefix under which the associated lookup object is registered. */
     private final String key;
 
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 bcc9a02a..e2d717da 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -17,9 +17,14 @@
 
 package org.apache.commons.text.lookup;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UncheckedIOException;
 import java.nio.charset.StandardCharsets;
 import java.util.Base64;
 import java.util.Map;
+import java.util.Objects;
+import java.util.Properties;
 import java.util.function.BiFunction;
 import java.util.function.Function;
 
@@ -178,6 +183,16 @@ import org.apache.commons.text.StringSubstitutor;
  */
 public final class StringLookupFactory {
 
+    /**
+     * Default properties file classpath location.
+     */
+    private static final String DEFAULT_RESOURCE = "org/apache/commons/text/lookup/defaults.properties";
+
+    /**
+     * Default mapping.
+     */
+    private static final Properties DEFAULTS = loadProperties();
+
     /**
      * Defines the singleton for this class.
      */
@@ -391,6 +406,16 @@ public final class StringLookupFactory {
         ConstantStringLookup.clear();
     }
 
+    private static Properties loadProperties() {
+        final Properties properties = new Properties();
+        try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(DEFAULT_RESOURCE)) {
+            properties.load(inputStream);
+        } catch (final IOException e) {
+            throw new UncheckedIOException(e);
+        }
+        return properties;
+    }
+
     /**
      * No need to build instances for now.
      */
@@ -399,19 +424,17 @@ public final class StringLookupFactory {
     }
 
     /**
-     * Adds the {@link StringLookupFactory default lookups}.
+     * Adds the {@link StringLookupFactory default lookups} as defined in {@value #DEFAULT_RESOURCE}.
      *
-     * @param stringLookupMap the map of string lookups.
+     * @param stringLookupMap the map of string lookups to edit.
      * @since 1.5
      */
     public void addDefaultStringLookups(final Map<String, StringLookup> stringLookupMap) {
         if (stringLookupMap != null) {
-            // "base64" is deprecated in favor of KEY_BASE64_DECODER.
-            stringLookupMap.put("base64", StringLookupFactory.INSTANCE_BASE64_DECODER);
-            for (final DefaultStringLookup stringLookup : DefaultStringLookup.values()) {
-                stringLookupMap.put(InterpolatorStringLookup.toKey(stringLookup.getKey()),
-                    stringLookup.getStringLookup());
-            }
+            DEFAULTS.forEach((k, v) -> {
+                final DefaultStringLookup stringLookup = DefaultStringLookup.valueOf(Objects.toString(v));
+                stringLookupMap.put(InterpolatorStringLookup.toKey(Objects.toString(k)), stringLookup.getStringLookup());
+            });
         }
     }
 
diff --git a/src/main/resources/org/apache/commons/text/lookup/defaults.properties b/src/main/resources/org/apache/commons/text/lookup/defaults.properties
new file mode 100644
index 00000000..ef2568bc
--- /dev/null
+++ b/src/main/resources/org/apache/commons/text/lookup/defaults.properties
@@ -0,0 +1,38 @@
+# 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.
+
+# Note:
+# base64 and base64Decoder are aliases.
+# base64 is deprecated
+
+base64=BASE64_DECODER
+base64Decoder=BASE64_DECODER
+
+base64Encoder=BASE64_ENCODER
+const=CONST
+date=DATE
+dns=DNS
+env=ENVIRONMENT
+file=FILE
+java=JAVA
+localhost=LOCAL_HOST
+properties=PROPERTIES
+resourceBundle=RESOURCE_BUNDLE
+script=SCRIPT
+sys=SYSTEM_PROPERTIES
+url=URL
+urlDecoder=URL_DECODER
+urlEncoder=URL_ENCODER
+xml=XML
diff --git a/src/test/resources/org/apache/commons/text/lookup/defaults.properties b/src/test/resources/org/apache/commons/text/lookup/defaults.properties
new file mode 100644
index 00000000..ef2568bc
--- /dev/null
+++ b/src/test/resources/org/apache/commons/text/lookup/defaults.properties
@@ -0,0 +1,38 @@
+# 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.
+
+# Note:
+# base64 and base64Decoder are aliases.
+# base64 is deprecated
+
+base64=BASE64_DECODER
+base64Decoder=BASE64_DECODER
+
+base64Encoder=BASE64_ENCODER
+const=CONST
+date=DATE
+dns=DNS
+env=ENVIRONMENT
+file=FILE
+java=JAVA
+localhost=LOCAL_HOST
+properties=PROPERTIES
+resourceBundle=RESOURCE_BUNDLE
+script=SCRIPT
+sys=SYSTEM_PROPERTIES
+url=URL
+urlDecoder=URL_DECODER
+urlEncoder=URL_ENCODER
+xml=XML