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 16:35:17 UTC
[text] TEXT-133" type="update" dev="ggregory">Add a XML XPath string
lookup. New feature also means that the next version will be 1.5, not 1.4.1.
Repository: commons-text
Updated Branches:
refs/heads/master a91ee4b62 -> 2aa5ab960
TEXT-133" type="update" dev="ggregory">Add a XML XPath string lookup.
New feature also means that the next version will be 1.5, not 1.4.1.
Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/2aa5ab96
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/2aa5ab96
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/2aa5ab96
Branch: refs/heads/master
Commit: 2aa5ab960c0e3845060cba2cc26ffce06b8912f7
Parents: a91ee4b
Author: Gary Gregory <ga...@gmail.com>
Authored: Thu Aug 23 10:35:13 2018 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Thu Aug 23 10:35:13 2018 -0600
----------------------------------------------------------------------
pom.xml | 4 +-
src/changes/changes.xml | 1 +
.../text/lookup/InterpolatorStringLookup.java | 7 ++
.../text/lookup/StringLookupFactory.java | 21 +++++
.../commons/text/lookup/XmlStringLookup.java | 84 ++++++++++++++++++++
.../text/lookup/XmlStringLookupTest.java | 32 ++++++++
src/test/resources/document.xml | 24 ++++++
7 files changed, 171 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-text/blob/2aa5ab96/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 610db23..913fa8a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -23,7 +23,7 @@
<version>47</version>
</parent>
<artifactId>commons-text</artifactId>
- <version>1.4.1-SNAPSHOT</version>
+ <version>1.5-SNAPSHOT</version>
<name>Apache Commons Text</name>
<description>Apache Commons Text is a library focused on algorithms working on strings.</description>
<url>http://commons.apache.org/proper/commons-text</url>
@@ -37,7 +37,7 @@
<commons.componentid>text</commons.componentid>
<commons.module.name>org.apache.commons.text</commons.module.name>
- <commons.release.version>1.4.1</commons.release.version>
+ <commons.release.version>1.5</commons.release.version>
<commons.release.desc>(Java 8+)</commons.release.desc>
<commons.jira.id>TEXT</commons.jira.id>
http://git-wip-us.apache.org/repos/asf/commons-text/blob/2aa5ab96/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 98d787f..774dfd3 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -49,6 +49,7 @@ 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>
</release>
<release version="1.4" date="2018-06-12" description="Release 1.4">
http://git-wip-us.apache.org/repos/asf/commons-text/blob/2aa5ab96/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 f1579d8..f176424 100644
--- a/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/InterpolatorStringLookup.java
@@ -31,6 +31,8 @@ import java.util.Map.Entry;
* <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
* <li>"java" for the {@link JavaPlatformStringLookup}.</li>
* <li>"date" for the {@link DateStringLookup}.</li>
+ * <li>"localhost" for the {@link LocalHostStringLookup}.</li>
+ * <li>"xml" for the {@link XmlStringLookup}.</li>
* </ul>
*/
class InterpolatorStringLookup extends AbstractStringLookup {
@@ -54,6 +56,8 @@ class InterpolatorStringLookup extends AbstractStringLookup {
* <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
* <li>"java" for the {@link JavaPlatformStringLookup}.</li>
* <li>"date" for the {@link DateStringLookup}.</li>
+ * <li>"localhost" for the {@link LocalHostStringLookup}.</li>
+ * <li>"xml" for the {@link XmlStringLookup}.</li>
* </ul>
*/
InterpolatorStringLookup() {
@@ -70,6 +74,8 @@ class InterpolatorStringLookup extends AbstractStringLookup {
* <li>"env" for the {@link EnvironmentVariableStringLookup}.</li>
* <li>"java" for the {@link JavaPlatformStringLookup}.</li>
* <li>"date" for the {@link DateStringLookup}.</li>
+ * <li>"localhost" for the {@link LocalHostStringLookup}.</li>
+ * <li>"xml" for the {@link XmlStringLookup}.</li>
* </ul>
*
* @param <V>
@@ -115,6 +121,7 @@ class InterpolatorStringLookup extends AbstractStringLookup {
this.stringLookupMap.put("java", JavaPlatformStringLookup.INSTANCE);
this.stringLookupMap.put("date", DateStringLookup.INSTANCE);
this.stringLookupMap.put("localhost", LocalHostStringLookup.INSTANCE);
+ this.stringLookupMap.put("xml", XmlStringLookup.INSTANCE);
}
}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/2aa5ab96/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 5d59d24..b8b2fc2 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -69,6 +69,7 @@ public final class StringLookupFactory {
* <li>"java" for the {@link JavaPlatformStringLookup}.</li>
* <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>
* </ul>
*
* @return a new InterpolatorStringLookup.
@@ -88,6 +89,7 @@ public final class StringLookupFactory {
* <li>"java" for the {@link JavaPlatformStringLookup}.</li>
* <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>
* </ul>
*
* @param <V>
@@ -111,6 +113,7 @@ public final class StringLookupFactory {
* <li>"java" for the {@link JavaPlatformStringLookup}.</li>
* <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>
* </ul>
*
* @param defaultStringLookup
@@ -133,6 +136,7 @@ public final class StringLookupFactory {
* <li>"java" for the {@link JavaPlatformStringLookup}.</li>
* <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>
* </ul>
*
* @param stringLookupMap
@@ -217,4 +221,21 @@ public final class StringLookupFactory {
public StringLookup systemPropertyStringLookup() {
return SystemPropertyStringLookup.INSTANCE;
}
+
+ /**
+ * Returns the ResourceBundleStringLookup singleton instance.
+ * <p>
+ * Looks up the value for the key in the format "DocumentPath:XPath".
+ * </p>
+ * <p>
+ * For example: "com/domain/document.xml:/path/to/node".
+ * </p>
+ *
+ * @return the XmlStringLookup singleton instance.
+ * @since 1.5
+ */
+ public StringLookup xmlStringLookup() {
+ return XmlStringLookup.INSTANCE;
+ }
+
}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/2aa5ab96/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
new file mode 100644
index 0000000..0675461
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java
@@ -0,0 +1,84 @@
+/*
+ * 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.ResourceBundle;
+
+import javax.xml.xpath.XPathFactory;
+
+import org.xml.sax.InputSource;
+
+/**
+ * Looks up keys from an XML document.
+ * <p>
+ * Looks up the value for a given key in the format "Document:XPath".
+ * </p>
+ * <p>
+ * For example: "com/domain/document.xml:/path/to/node".
+ * </p>
+ *
+ * @since 1.5
+ */
+final class XmlStringLookup extends AbstractStringLookup {
+
+ /**
+ * Defines the singleton for this class.
+ */
+ static final XmlStringLookup INSTANCE = new XmlStringLookup();
+
+ /**
+ * No need to build instances for now.
+ */
+ private XmlStringLookup() {
+ // 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 XML key format [%s]. Expected format is DocumentPath:XPath.",
+ key);
+ }
+ final String documentPath = keys[0];
+ final String xpath = keys[1];
+ try {
+ return XPathFactory.newInstance().newXPath().evaluate(xpath,
+ new InputSource(Files.newInputStream(Paths.get(documentPath))));
+ } catch (final Exception e) {
+ throw IllegalArgumentExceptions.format(e, "Error looking up XML [%s] and XPath [%s].", documentPath, xpath);
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/2aa5ab96/src/test/java/org/apache/commons/text/lookup/XmlStringLookupTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/lookup/XmlStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/XmlStringLookupTest.java
new file mode 100644
index 0000000..cdbf9d3
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/lookup/XmlStringLookupTest.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 XmlStringLookupTest {
+
+ @Test
+ public void testOne() {
+ final String docName = "src/test/resources/document.xml";
+ final String xpath = "/root/path/to/node";
+ Assertions.assertEquals("Hello World!", XmlStringLookup.INSTANCE.lookup(docName + ":" + xpath));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/2aa5ab96/src/test/resources/document.xml
----------------------------------------------------------------------
diff --git a/src/test/resources/document.xml b/src/test/resources/document.xml
new file mode 100644
index 0000000..f1310f4
--- /dev/null
+++ b/src/test/resources/document.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0"?>
+<!--
+ 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.
+ -->
+<root>
+ <path>
+ <to>
+ <node>Hello World!</node>
+ </to>
+ </path>
+</root>