Skip to content

Commit

Permalink
Make default string lookups configurable via system property. Remove …
Browse files Browse the repository at this point in the history
…dns, url, and script lookups from defaults.
  • Loading branch information
darkma773r committed Jul 17, 2022
1 parent 0cd901c commit 5f168cf
Show file tree
Hide file tree
Showing 9 changed files with 390 additions and 209 deletions.
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="TEXT-185" type="add" dev="ggregory" due-to="Larry West, Gary Gregory">Release Notes page hasn't been updated for 1.9 release yet.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add StrBuilder.isNotEmpty().</action>
<!-- UPDATE -->
<action type="update" dev="mattjuntunen">Make default string lookups configurable via system property. Remove dns, url, and script lookups from defaults. If these lookups are required for use in StringSubstitutor.createInterpolator(), they must be enabled via system property. See StringLookupFactory for details.</action>
<action type="update" dev="kinow" due-to="Dependabot">Bump actions/setup-java from v1.4.0 to 3 #147, #156, #155, #172, #215, #314.</action>
<action type="update" dev="kinow" due-to="Dependabot">Bump actions/checkout from v1 to 3 #138, #146, #165, #183, #274, #279, #304.</action>
<action type="update" dev="kinow" due-to="Dependabot">Bump actions/cache from v2 to v3.0.5 #205, #217, #234, #339.</action>
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/org/apache/commons/text/StringSubstitutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,26 +141,24 @@
*
* <pre>
* final StringSubstitutor interpolator = StringSubstitutor.createInterpolator();
* interpolator.setEnableSubstitutionInVariables(true); // Allows for nested $'s.
* final String text = interpolator.replace("Base64 Decoder: ${base64Decoder:SGVsbG9Xb3JsZCE=}\n"
* final String text = interpolator.replace(
* "Base64 Decoder: ${base64Decoder:SGVsbG9Xb3JsZCE=}\n"
* + "Base64 Encoder: ${base64Encoder:HelloWorld!}\n"
* + "Java Constant: ${const:java.awt.event.KeyEvent.VK_ESCAPE}\n"
* + "Date: ${date:yyyy-MM-dd}\n" + "DNS: ${dns:address|apache.org}\n"
* + "Date: ${date:yyyy-MM-dd}\n"
* + "Environment Variable: ${env:USERNAME}\n"
* + "File Content: ${file:UTF-8:src/test/resources/document.properties}\n"
* + "Java: ${java:version}\n" + "Localhost: ${localhost:canonical-name}\n"
* + "Java: ${java:version}\n"
* + "Localhost: ${localhost:canonical-name}\n"
* + "Properties File: ${properties:src/test/resources/document.properties::mykey}\n"
* + "Resource Bundle: ${resourceBundle:org.apache.commons.text.example.testResourceBundleLookup:mykey}\n"
* + "Script: ${script:javascript:3 + 4}\n" + "System Property: ${sys:user.dir}\n"
* + "System Property: ${sys:user.dir}\n"
* + "URL Decoder: ${urlDecoder:Hello%20World%21}\n"
* + "URL Encoder: ${urlEncoder:Hello World!}\n"
* + "URL Content (HTTP): ${url:UTF-8:http://www.apache.org}\n"
* + "URL Content (HTTPS): ${url:UTF-8:https://www.apache.org}\n"
* + "URL Content (File): ${url:UTF-8:file:///${sys:user.dir}/src/test/resources/document.properties}\n"
* + "XML XPath: ${xml:src/test/resources/document.xml:/root/path/to/node}\n");
* </pre>
* <p>
* For documentation of each lookup, see {@link StringLookupFactory}.
* For documentation and a full list of available lookups, see {@link StringLookupFactory}.
* </p>
*
* <h2>Using Recursive Variable Replacement</h2>
Expand Down Expand Up @@ -292,7 +290,7 @@ public String toString() {
*
* <pre>
* StringSubstitutor.createInterpolator().replace(
* "OS name: ${sys:os.name}, " + "3 + 4 = ${script:javascript:3 + 4}");
* "OS name: ${sys:os.name}, user: ${env:USER}");
* </pre>
*
* @return a new instance using the interpolator string lookup.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package org.apache.commons.text.lookup;

import java.util.stream.Stream;

/**
* An enumeration defining {@link StringLookup} objects available through {@link StringLookupFactory}.
* <p>
Expand Down Expand Up @@ -117,14 +115,6 @@ 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(final 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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;

Expand All @@ -40,10 +39,6 @@ class InterpolatorStringLookup extends AbstractStringLookup {
/** Constant for the prefix separator. */
private static final char PREFIX_SEPARATOR = ':';

static String toKey(final String key) {
return key.toLowerCase(Locale.ROOT);
}

/** The default string lookup. */
private final StringLookup defaultStringLookup;

Expand Down Expand Up @@ -72,7 +67,7 @@ static String toKey(final String key) {
this.defaultStringLookup = defaultStringLookup;
this.stringLookupMap = new HashMap<>(stringLookupMap.size());
for (final Entry<String, StringLookup> entry : stringLookupMap.entrySet()) {
this.stringLookupMap.put(toKey(entry.getKey()), entry.getValue());
this.stringLookupMap.put(StringLookupFactory.toKey(entry.getKey()), entry.getValue());
}
if (addDefaultLookups) {
StringLookupFactory.INSTANCE.addDefaultStringLookups(this.stringLookupMap);
Expand Down Expand Up @@ -127,7 +122,7 @@ public String lookup(String key) {

final int prefixPos = key.indexOf(PREFIX_SEPARATOR);
if (prefixPos >= 0) {
final String prefix = toKey(key.substring(0, prefixPos));
final String prefix = StringLookupFactory.toKey(key.substring(0, prefixPos));
final String name = key.substring(prefixPos + 1);
final StringLookup lookup = stringLookupMap.get(prefix);
String value = null;
Expand Down
Loading

0 comments on commit 5f168cf

Please sign in to comment.