Repository: commons-text Updated Branches: refs/heads/master 2aa5ab960 -> 6c52769fe
[TEXT-134] Add a Properties 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/6c52769f Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/6c52769f Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/6c52769f Branch: refs/heads/master Commit: 6c52769fedf8e5ec38d2e92d4d45e1a488c6f130 Parents: 2aa5ab9 Author: Gary Gregory <[email protected]> Authored: Thu Aug 23 10:58:36 2018 -0600 Committer: Gary Gregory <[email protected]> Committed: Thu Aug 23 10:58:36 2018 -0600 ---------------------------------------------------------------------- src/changes/changes.xml | 3 +- .../text/lookup/InterpolatorStringLookup.java | 4 + .../text/lookup/PropertiesStringLookup.java | 82 ++++++++++++++++++++ .../text/lookup/StringLookupFactory.java | 22 +++++- .../text/lookup/PropertiesStringLookupTest.java | 32 ++++++++ src/test/resources/document.properties | 1 + 6 files changed, 142 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c52769f/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 774dfd3..7a86040 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -49,7 +49,8 @@ The <action> type attribute can be add,update,fix,remove. <action issue="TEXT-130" type="fix" dev="chtompki" due-to="Jan Martin Keil">Fixes JaroWinklerDistance: Wrong results due to precision of transpositions</action> <action issue="TEXT-131" type="fix" dev="chtompki" due-to="Jan Martin Keil">JaroWinklerDistance: Calculation deviates from definition</action> <action issue="TEXT-132" type="update" dev="ggregory">Update Apache Commons Lang from 3.7 to 3.8.</action> - <action issue="TEXT-133" type="update" dev="ggregory">Add a XML XPath string lookup.</action> + <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> </release> <release version="1.4" date="2018-06-12" description="Release 1.4"> http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c52769f/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 f176424..95fac6b 100644 --- a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java @@ -33,6 +33,7 @@ import java.util.Map.Entry; * <li>"date" for the {@link DateStringLookup}.</li> * <li>"localhost" for the {@link LocalHostStringLookup}.</li> * <li>"xml" for the {@link XmlStringLookup}.</li> + * <li>"properties" for the {@link PropertiesStringLookup}.</li> * </ul> */ class InterpolatorStringLookup extends AbstractStringLookup { @@ -58,6 +59,7 @@ class InterpolatorStringLookup extends AbstractStringLookup { * <li>"date" for the {@link DateStringLookup}.</li> * <li>"localhost" for the {@link LocalHostStringLookup}.</li> * <li>"xml" for the {@link XmlStringLookup}.</li> + * <li>"properties" for the {@link PropertiesStringLookup}.</li> * </ul> */ InterpolatorStringLookup() { @@ -76,6 +78,7 @@ class InterpolatorStringLookup extends AbstractStringLookup { * <li>"date" for the {@link DateStringLookup}.</li> * <li>"localhost" for the {@link LocalHostStringLookup}.</li> * <li>"xml" for the {@link XmlStringLookup}.</li> + * <li>"properties" for the {@link PropertiesStringLookup}.</li> * </ul> * * @param <V> @@ -122,6 +125,7 @@ class InterpolatorStringLookup extends AbstractStringLookup { this.stringLookupMap.put("date", DateStringLookup.INSTANCE); this.stringLookupMap.put("localhost", LocalHostStringLookup.INSTANCE); this.stringLookupMap.put("xml", XmlStringLookup.INSTANCE); + this.stringLookupMap.put("properties", PropertiesStringLookup.INSTANCE); } } http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c52769f/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 new file mode 100644 index 0000000..909943a --- /dev/null +++ b/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java @@ -0,0 +1,82 @@ +/* + * 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.file.Files; +import java.nio.file.Paths; +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". + * </p> + * <p> + * For example: "com/domain/document.properties:key". + * </p> + * + * @since 1.5 + */ +final class PropertiesStringLookup extends AbstractStringLookup { + + /** + * Defines the singleton for this class. + */ + static final PropertiesStringLookup INSTANCE = new PropertiesStringLookup(); + + /** + * No need to build instances for now. + */ + private PropertiesStringLookup() { + // 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 documentPath = keys[0]; + final String propertyKey = keys[1]; + try { + final Properties properties = new Properties(); + properties.load(Files.newInputStream(Paths.get(documentPath))); + return properties.getProperty(propertyKey); + } catch (final Exception e) { + throw IllegalArgumentExceptions.format(e, "Error looking up Properties [%s] and Key [%s].", documentPath, + propertyKey); + } + } + +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c52769f/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 b8b2fc2..6b13e38 100644 --- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java +++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java @@ -70,6 +70,7 @@ public final class StringLookupFactory { * <li>"date" for the {@link DateStringLookup}.</li> * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li> * <li>"xml" for the {@link XmlStringLookup}.</li> + * <li>"properties" for the {@link PropertiesStringLookup}.</li> * </ul> * * @return a new InterpolatorStringLookup. @@ -90,6 +91,7 @@ public final class StringLookupFactory { * <li>"date" for the {@link DateStringLookup}.</li> * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li> * <li>"xml" for the {@link XmlStringLookup}.</li> + * <li>"properties" for the {@link PropertiesStringLookup}.</li> * </ul> * * @param <V> @@ -114,6 +116,7 @@ public final class StringLookupFactory { * <li>"date" for the {@link DateStringLookup}.</li> * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li> * <li>"xml" for the {@link XmlStringLookup}.</li> + * <li>"properties" for the {@link PropertiesStringLookup}.</li> * </ul> * * @param defaultStringLookup @@ -137,6 +140,7 @@ public final class StringLookupFactory { * <li>"date" for the {@link DateStringLookup}.</li> * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li> * <li>"xml" for the {@link XmlStringLookup}.</li> + * <li>"properties" for the {@link PropertiesStringLookup}.</li> * </ul> * * @param stringLookupMap @@ -223,7 +227,7 @@ public final class StringLookupFactory { } /** - * Returns the ResourceBundleStringLookup singleton instance. + * Returns the XmlStringLookup singleton instance. * <p> * Looks up the value for the key in the format "DocumentPath:XPath". * </p> @@ -238,4 +242,20 @@ public final class StringLookupFactory { return XmlStringLookup.INSTANCE; } + /** + * Returns the PropertiesStringLookup singleton instance. + * <p> + * Looks up the value for the key in the format "DocumentPath:Key". + * </p> + * <p> + * For example: "com/domain/document.properties:Key". + * </p> + * + * @return the PropertiesStringLookup singleton instance. + * @since 1.5 + */ + public StringLookup propertiesStringLookup() { + return PropertiesStringLookup.INSTANCE; + } + } http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c52769f/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 new file mode 100644 index 0000000..c018e50 --- /dev/null +++ b/src/test/java/org/apache/commons/text/lookup/PropertiesStringLookupTest.java @@ -0,0 +1,32 @@ +/* + * 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 PropertiesStringLookupTest { + + @Test + public void testOne() { + final String docName = "src/test/resources/document.properties"; + final String xpath = "mykey"; + Assertions.assertEquals("Hello World!", PropertiesStringLookup.INSTANCE.lookup(docName + ":" + xpath)); + } + +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/6c52769f/src/test/resources/document.properties ---------------------------------------------------------------------- diff --git a/src/test/resources/document.properties b/src/test/resources/document.properties new file mode 100644 index 0000000..1016853 --- /dev/null +++ b/src/test/resources/document.properties @@ -0,0 +1 @@ +mykey = Hello World! \ No newline at end of file
