Repository: commons-text Updated Branches: refs/heads/master 0b5520502 -> a53c75b0d
[TEXT-137] Add a URL 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/a53c75b0 Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/a53c75b0 Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/a53c75b0 Branch: refs/heads/master Commit: a53c75b0db7425f801861aea5034bf3383dc7995 Parents: 0b55205 Author: Gary Gregory <garydgreg...@gmail.com> Authored: Thu Aug 23 13:31:44 2018 -0600 Committer: Gary Gregory <garydgreg...@gmail.com> Committed: Thu Aug 23 13:31:44 2018 -0600 ---------------------------------------------------------------------- src/changes/changes.xml | 1 + .../text/lookup/InterpolatorStringLookup.java | 5 ++ .../text/lookup/StringLookupFactory.java | 24 +++++ .../commons/text/lookup/UrlStringLookup.java | 93 ++++++++++++++++++++ .../text/lookup/UrlStringLookupTest.java | 46 ++++++++++ 5 files changed, 169 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-text/blob/a53c75b0/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index fffc34a..65744c0 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -53,6 +53,7 @@ The <action> type attribute can be add,update,fix,remove. <action issue="TEXT-134" type="update" dev="ggregory">Add a Properties file string lookup.</action> <action issue="TEXT-135" type="update" dev="ggregory">Add a script file string lookup.</action> <action issue="TEXT-136" type="update" dev="ggregory">Add a file string lookup.</action> + <action issue="TEXT-137" type="update" dev="ggregory">Add a URL 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/a53c75b0/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 ffd011e..c7ac486 100644 --- a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java @@ -35,6 +35,7 @@ import java.util.Map.Entry; * <li>"xml" for the {@link XmlStringLookup}.</li> * <li>"properties" for the {@link PropertiesStringLookup}.</li> * <li>"file" for the {@link FileStringLookup}.</li> + * <li>"url" for the {@link UrlStringLookup}.</li> * </ul> */ class InterpolatorStringLookup extends AbstractStringLookup { @@ -62,6 +63,8 @@ class InterpolatorStringLookup extends AbstractStringLookup { * <li>"xml" for the {@link XmlStringLookup}.</li> * <li>"properties" for the {@link PropertiesStringLookup}.</li> * <li>"script" for the {@link ScriptStringLookup}.</li> + * <li>"file" for the {@link FileStringLookup}.</li> + * <li>"url" for the {@link UrlStringLookup}.</li> * </ul> */ InterpolatorStringLookup() { @@ -83,6 +86,7 @@ class InterpolatorStringLookup extends AbstractStringLookup { * <li>"properties" for the {@link PropertiesStringLookup}.</li> * <li>"script" for the {@link ScriptStringLookup}.</li> * <li>"file" for the {@link FileStringLookup}.</li> + * <li>"url" for the {@link UrlStringLookup}.</li> * </ul> * * @param <V> @@ -132,6 +136,7 @@ class InterpolatorStringLookup extends AbstractStringLookup { this.stringLookupMap.put("properties", PropertiesStringLookup.INSTANCE); this.stringLookupMap.put("script", ScriptStringLookup.INSTANCE); this.stringLookupMap.put("file", FileStringLookup.INSTANCE); + this.stringLookupMap.put("url", UrlStringLookup.INSTANCE); } } http://git-wip-us.apache.org/repos/asf/commons-text/blob/a53c75b0/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 2917e92..7c30910 100644 --- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java +++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java @@ -73,6 +73,7 @@ public final class StringLookupFactory { * <li>"properties" for the {@link PropertiesStringLookup}.</li> * <li>"script" for the {@link ScriptStringLookup}.</li> * <li>"file" for the {@link FileStringLookup}.</li> + * <li>"url" for the {@link UrlStringLookup}.</li> * </ul> * * @return a new InterpolatorStringLookup. @@ -96,6 +97,7 @@ public final class StringLookupFactory { * <li>"properties" for the {@link PropertiesStringLookup}.</li> * <li>"script" for the {@link ScriptStringLookup}.</li> * <li>"file" for the {@link FileStringLookup}.</li> + * <li>"url" for the {@link UrlStringLookup}.</li> * </ul> * * @param <V> @@ -123,6 +125,7 @@ public final class StringLookupFactory { * <li>"properties" for the {@link PropertiesStringLookup}.</li> * <li>"script" for the {@link ScriptStringLookup}.</li> * <li>"file" for the {@link FileStringLookup}.</li> + * <li>"url" for the {@link UrlStringLookup}.</li> * </ul> * * @param defaultStringLookup @@ -149,6 +152,7 @@ public final class StringLookupFactory { * <li>"properties" for the {@link PropertiesStringLookup}.</li> * <li>"script" for the {@link ScriptStringLookup}.</li> * <li>"file" for the {@link FileStringLookup}.</li> + * <li>"url" for the {@link UrlStringLookup}.</li> * </ul> * * @param stringLookupMap @@ -298,4 +302,24 @@ public final class StringLookupFactory { return FileStringLookup.INSTANCE; } + /** + * Returns the UrlStringLookup singleton instance. + * <p> + * Looks up the value for the key in the format "CharsetName:URL". + * </p> + * <p> + * For example, using the HTTP scheme: "UTF-8:http://www.google.com" + * </p> + * <p> + * For example, using the file scheme: + * "UTF-8:file:///C:/somehome/commons/commons-text/src/test/resources/document.properties" + * </p> + * + * @return the UrlStringLookup singleton instance. + * @since 1.5 + */ + public StringLookup urlStringLookup() { + return UrlStringLookup.INSTANCE; + } + } http://git-wip-us.apache.org/repos/asf/commons-text/blob/a53c75b0/src/main/java/org/apache/commons/text/lookup/UrlStringLookup.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/lookup/UrlStringLookup.java b/src/main/java/org/apache/commons/text/lookup/UrlStringLookup.java new file mode 100644 index 0000000..6f6c448 --- /dev/null +++ b/src/main/java/org/apache/commons/text/lookup/UrlStringLookup.java @@ -0,0 +1,93 @@ +/* + * 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.io.BufferedInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringWriter; +import java.net.URL; +import java.nio.file.Files; + +/** + * Looks up keys from an XML document. + * <p> + * Looks up the value for a given key in the format "Charset:URL". + * </p> + * <p> + * For example: "UTF-8:com/domain/document.properties". + * </p> + * + * @since 1.5 + */ +final class UrlStringLookup extends AbstractStringLookup { + + /** + * Defines the singleton for this class. + */ + static final UrlStringLookup INSTANCE = new UrlStringLookup(); + + /** + * No need to build instances for now. + */ + private UrlStringLookup() { + // 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 URL key format [%s]; expected format is DocumentPath:Key.", + key); + } + final String charsetName = keys[0]; + final String urlStr = substringAfter(key, SPLIT_CH); + try { + final URL url = new URL(urlStr); + final StringWriter writer = new StringWriter(8192); + char[] buffer = new char[8192]; + try (InputStreamReader reader = new InputStreamReader(new BufferedInputStream(url.openStream()), + charsetName)) { + int n; + while (-1 != (n = reader.read(buffer))) { + writer.write(buffer, 0, n); + } + } + return writer.toString(); + } catch (final Exception e) { + throw IllegalArgumentExceptions.format(e, "Error looking up URL [%s] with Charset [%s].", urlStr, + charsetName); + } + } + +} http://git-wip-us.apache.org/repos/asf/commons-text/blob/a53c75b0/src/test/java/org/apache/commons/text/lookup/UrlStringLookupTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/lookup/UrlStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/UrlStringLookupTest.java new file mode 100644 index 0000000..b5d3db1 --- /dev/null +++ b/src/test/java/org/apache/commons/text/lookup/UrlStringLookupTest.java @@ -0,0 +1,46 @@ +/* + * 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.net.URI; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class UrlStringLookupTest { + + @Test + public void testFileScheme() throws Exception { + final Path path = Paths.get("src/test/resources/document.properties"); + final URI uri = path.toUri(); + System.out.println(uri); + final byte[] expectedBytes = Files.readAllBytes(path); + String expectedString = new String(expectedBytes, StandardCharsets.UTF_8); + Assertions.assertEquals(expectedString, UrlStringLookup.INSTANCE.lookup("UTF-8:" + uri.toString())); + } + + @Test + public void testHttpScheme() throws Exception { + Assertions.assertNotNull(UrlStringLookup.INSTANCE.lookup("UTF-8:http://www.google.com")); + } + +}