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/08/28 22:22:38 UTC
[text] [TEXT-134] Add a Properties file string lookup. Allow for C:
and D: and so on in file names.
Repository: commons-text
Updated Branches:
refs/heads/master cab116505 -> 170b565c6
[TEXT-134] Add a Properties file string lookup. Allow for C: and D: and
so on in file names.
Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/170b565c
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/170b565c
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/170b565c
Branch: refs/heads/master
Commit: 170b565c67e00504d4a86d50eb9f50f48d49833c
Parents: cab1165
Author: Gary Gregory <ga...@gmail.com>
Authored: Tue Aug 28 16:22:34 2018 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Tue Aug 28 16:22:34 2018 -0600
----------------------------------------------------------------------
.../text/lookup/AbstractStringLookup.java | 14 ++++++++++
.../text/lookup/PropertiesStringLookup.java | 16 +++++++----
.../text/lookup/PropertiesStringLookupTest.java | 29 ++++++++++++++++++--
3 files changed, 51 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-text/blob/170b565c/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java b/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java
index 1d41a42..f265bf7 100644
--- a/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java
@@ -40,4 +40,18 @@ abstract class AbstractStringLookup implements StringLookup {
final int indexOf = value.indexOf(ch);
return indexOf > -1 ? value.substring(indexOf + 1) : "";
}
+
+ /**
+ * Returns the substring after the first occurrence of <code>str</code> in <code>value</code>.
+ *
+ * @param value
+ * The source string.
+ * @param str
+ * The string to search.
+ * @return a new string.
+ */
+ protected String substringAfter(final String value, final String str) {
+ final int indexOf = value.indexOf(str);
+ return indexOf > -1 ? value.substring(indexOf + str.length()) : "";
+ }
}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/170b565c/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java b/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java
index cffc6e1..6c6acc9 100644
--- a/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java
@@ -24,7 +24,10 @@ import java.util.Properties;
/**
* Looks up keys from an XML document.
* <p>
- * Looks up the value for a given key in the format "Document:Key".
+ * Looks up the value for a given key in the format "Document::Key".
+ * </p>
+ * <p>
+ * Note the use of "::" instead of ":" to allow for "C:" drive letters in paths.
* </p>
* <p>
* For example: "com/domain/document.properties:key".
@@ -49,7 +52,10 @@ final class PropertiesStringLookup extends AbstractStringLookup {
/**
* Looks up the value for the key in the format "DocumentPath:XPath".
* <p>
- * For example: "com/domain/document.xml:/path/to/node".
+ * For example: "com/domain/document.xml::/path/to/node".
+ * </p>
+ * <p>
+ * Note the use of "::" instead of ":" to allow for "C:" drive letters in paths.
* </p>
*
* @param key
@@ -61,14 +67,14 @@ final class PropertiesStringLookup extends AbstractStringLookup {
if (key == null) {
return null;
}
- final String[] keys = key.split(SPLIT_STR);
+ 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);
+ .format("Bad properties key format [%s]; expected format is DocumentPath::Key.", key);
}
final String documentPath = keys[0];
- final String propertyKey = substringAfter(key, SPLIT_CH);
+ final String propertyKey = substringAfter(key, "::");
try {
final Properties properties = new Properties();
properties.load(Files.newInputStream(Paths.get(documentPath)));
http://git-wip-us.apache.org/repos/asf/commons-text/blob/170b565c/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java
index 09c5a28..959faf4 100644
--- a/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java
@@ -28,7 +28,7 @@ public class PropertiesStringLookupTest {
private static final String DOC_PATH = "src/test/resources/document.properties";
private static final String KEY = "mykey";
- private static final String KEY_PATH = DOC_PATH + ":" + KEY;
+ private static final String KEY_PATH = DOC_PATH + "::" + KEY;
@Test
public void testOne() {
@@ -43,13 +43,36 @@ public class PropertiesStringLookupTest {
}
@Test
+ public void testInterpolatorNestedColon() {
+ final StringSubstitutor stringSubstitutor = new StringSubstitutor(
+ StringLookupFactory.INSTANCE.interpolatorStringLookup());
+ // Need to handle "C:" in the sys prop user.dir.
+ Assertions.assertEquals(
+ "${properties:" + System.getProperty("user.dir") + "/src/test/resources/document.properties::mykey}",
+ stringSubstitutor.replace("$${properties:${sys:user.dir}/" + KEY_PATH + "}"));
+ }
+
+ @Test
public void testInterpolatorWithParameterizedKey() {
final Map<String, String> map = new HashMap<>();
map.put("KeyIsHere", KEY);
final StringSubstitutor stringSubstitutor = new StringSubstitutor(
StringLookupFactory.INSTANCE.interpolatorStringLookup(map));
- final String replaced = stringSubstitutor.replace("$${properties:" + DOC_PATH + ":${KeyIsHere}}");
- Assertions.assertEquals("${properties:" + DOC_PATH + ":mykey}", replaced);
+ final String replaced = stringSubstitutor.replace("$${properties:" + DOC_PATH + "::${KeyIsHere}}");
+ Assertions.assertEquals("${properties:" + DOC_PATH + "::mykey}", replaced);
+ Assertions.assertEquals("Hello World!", stringSubstitutor.replace(replaced));
+ }
+
+ @Test
+ public void testInterpolatorWithParameterizedKey2() {
+ final Map<String, String> map = new HashMap<>();
+ map.put("KeyIsHere", KEY);
+ final StringSubstitutor stringSubstitutor = new StringSubstitutor(
+ StringLookupFactory.INSTANCE.interpolatorStringLookup(map));
+ final String replaced = stringSubstitutor
+ .replace("$${properties:${sys:user.dir}/" + DOC_PATH + "::${KeyIsHere}}");
+ Assertions.assertEquals("${properties:" + System.getProperty("user.dir") + "/" + DOC_PATH + "::mykey}",
+ replaced);
Assertions.assertEquals("Hello World!", stringSubstitutor.replace(replaced));
}