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/23 19:04:54 UTC

[text] [TEXT-136] Add a file string lookup. Allow for ':' in the file name.

Repository: commons-text
Updated Branches:
  refs/heads/master 146bf5c9d -> 0b5520502


[TEXT-136] Add a file string lookup. Allow for ':' in the file name.

Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/0b552050
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/0b552050
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/0b552050

Branch: refs/heads/master
Commit: 0b55205025fcec1bf5b6317e79dba63a7bed33b3
Parents: 146bf5c
Author: Gary Gregory <ga...@gmail.com>
Authored: Thu Aug 23 13:04:50 2018 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Thu Aug 23 13:04:50 2018 -0600

----------------------------------------------------------------------
 .../text/lookup/AbstractStringLookup.java       | 70 ++++++++++++--------
 .../commons/text/lookup/FileStringLookup.java   | 10 +--
 .../text/lookup/PropertiesStringLookup.java     |  6 +-
 .../commons/text/lookup/XmlStringLookup.java    |  4 +-
 4 files changed, 53 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-text/blob/0b552050/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 3cbbaa3..aa1f933 100644
--- a/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/AbstractStringLookup.java
@@ -1,27 +1,43 @@
-/*
- * 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;
-
-/**
- * A default lookup for others to extend in this package.
- *
- * @since 1.3
- */
-abstract class AbstractStringLookup implements StringLookup {
-    // nothing yet
-}
+/*
+ * 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;
+
+/**
+ * A default lookup for others to extend in this package.
+ *
+ * @since 1.3
+ */
+abstract class AbstractStringLookup implements StringLookup {
+
+    protected static final char SPLIT_CH = ':';
+    protected static final String SPLIT_STR = String.valueOf(SPLIT_CH);
+
+    /**
+     * Returns the substring after the first occurrence of <code>ch</code> in <code>value</code>.
+     * 
+     * @param value
+     *            The source string.
+     * @param ch
+     *            The character to search.
+     * @return a new string.
+     */
+    protected String substringAfter(final String value, final char ch) {
+        final int indexOf = value.indexOf(ch);
+        return indexOf > -1 ? value.substring(indexOf + 1) : "";
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-text/blob/0b552050/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java b/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java
index 66db525..1b565fa 100644
--- a/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/FileStringLookup.java
@@ -36,8 +36,8 @@ final class FileStringLookup extends AbstractStringLookup {
     /**
      * Defines the singleton for this class.
      */
-    static final FileStringLookup INSTANCE = new FileStringLookup();
-
+    static final AbstractStringLookup INSTANCE = new FileStringLookup();
+    
     /**
      * No need to build instances for now.
      */
@@ -60,14 +60,14 @@ final class FileStringLookup extends AbstractStringLookup {
         if (key == null) {
             return null;
         }
-        final String[] keys = key.split(":");
+        final String[] keys = key.split(String.valueOf(SPLIT_CH));
         final int keyLen = keys.length;
-        if (keyLen != 2) {
+        if (keyLen < 2) {
             throw IllegalArgumentExceptions.format("Bad file key format [%s], expected format is DocumentPath:Key.",
                     key);
         }
         final String charsetName = keys[0];
-        final String fileName = keys[1];
+        final String fileName = substringAfter(key, SPLIT_CH);
         try {
             return new String(Files.readAllBytes(Paths.get(fileName)), charsetName);
         } catch (final Exception e) {

http://git-wip-us.apache.org/repos/asf/commons-text/blob/0b552050/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 c94019d..1e8baca 100644
--- a/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/PropertiesStringLookup.java
@@ -61,14 +61,14 @@ final class PropertiesStringLookup extends AbstractStringLookup {
         if (key == null) {
             return null;
         }
-        final String[] keys = key.split(":");
+        final String[] keys = key.split(SPLIT_STR);
         final int keyLen = keys.length;
-        if (keyLen != 2) {
+        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];
+        final String propertyKey = substringAfter(key, SPLIT_CH);
         try {
             final Properties properties = new Properties();
             properties.load(Files.newInputStream(Paths.get(documentPath)));

http://git-wip-us.apache.org/repos/asf/commons-text/blob/0b552050/src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java b/src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java
index 88e7a8b..53c4c5a 100644
--- a/src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java
@@ -64,14 +64,14 @@ final class XmlStringLookup extends AbstractStringLookup {
         if (key == null) {
             return null;
         }
-        final String[] keys = key.split(":");
+        final String[] keys = key.split(SPLIT_STR);
         final int keyLen = keys.length;
         if (keyLen != 2) {
             throw IllegalArgumentExceptions.format("Bad XML key format [%s]; expected format is DocumentPath:XPath.",
                     key);
         }
         final String documentPath = keys[0];
-        final String xpath = keys[1];
+        final String xpath = substringAfter(key, SPLIT_CH);
         try {
             return XPathFactory.newInstance().newXPath().evaluate(xpath,
                     new InputSource(Files.newInputStream(Paths.get(documentPath))));