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/09/21 21:57:34 UTC
[text] [TEXT-142] Add URL encoder and decoder string lookups.
Repository: commons-text
Updated Branches:
refs/heads/master 5e9ebeea8 -> 65ecfab29
[TEXT-142] Add URL encoder and decoder string lookups.
Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/65ecfab2
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/65ecfab2
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/65ecfab2
Branch: refs/heads/master
Commit: 65ecfab2954c4e9e4cf1b96bc60397826f0d268d
Parents: 5e9ebee
Author: Gary Gregory <ga...@gmail.com>
Authored: Fri Sep 21 15:57:31 2018 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Fri Sep 21 15:57:31 2018 -0600
----------------------------------------------------------------------
src/changes/changes.xml | 3 +-
.../text/lookup/UrlDecoderStringLookup.java | 55 ++++++++++++++++++++
.../text/lookup/UrlEncoderStringLookup.java | 54 +++++++++++++++++++
.../text/lookup/UrlDecoderStringLookupTest.java | 43 +++++++++++++++
.../text/lookup/UrlEncoderStringLookupTest.java | 33 ++++++++++++
5 files changed, 187 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-text/blob/65ecfab2/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 219fc9c..790d400 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -55,7 +55,8 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="TEXT-136" type="add" dev="ggregory">Add a file string lookup.</action>
<action issue="TEXT-137" type="add" dev="ggregory">Add a URL string lookup.</action>
<action issue="TEXT-140" type="add" dev="ggregory">Add a Base64 string lookup.</action>
- <action issue="TEXT-141" type="add" dev="ggregory">Add org.apache.commons.text.lookup.StringLookupFactory.resourceBundleStringLookup(String).</action>
+ <action issue="TEXT-141" type="add" dev="ggregory">Add org.apache.commons.text.lookup.StringLookupFactory.resourceBundleStringLookup(String).</action>
+ <action issue="TEXT-142" type="add" dev="ggregory">Add URL encoder and decoder string lookups.</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/65ecfab2/src/main/java/org/apache/commons/text/lookup/UrlDecoderStringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/UrlDecoderStringLookup.java b/src/main/java/org/apache/commons/text/lookup/UrlDecoderStringLookup.java
new file mode 100644
index 0000000..ddd494d
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/lookup/UrlDecoderStringLookup.java
@@ -0,0 +1,55 @@
+/*
+ * 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.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * Decodes URL Strings using the UTF-8 encoding.
+ *
+ * @see URLEncoder
+ * @since 1.5
+ */
+final class UrlDecoderStringLookup extends AbstractStringLookup {
+
+ /**
+ * Defines the singleton for this class.
+ */
+ static final UrlDecoderStringLookup INSTANCE = new UrlDecoderStringLookup();
+
+ /**
+ * No need to build instances for now.
+ */
+ private UrlDecoderStringLookup() {
+ // empty
+ }
+
+ @Override
+ public String lookup(final String key) {
+ final String enc = StandardCharsets.UTF_8.name();
+ try {
+ return new String(URLDecoder.decode(key, enc));
+ } catch (UnsupportedEncodingException e) {
+ throw IllegalArgumentExceptions.format(e, "%s: source=%s, encoding=%s", e, key, enc);
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/65ecfab2/src/main/java/org/apache/commons/text/lookup/UrlEncoderStringLookup.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/lookup/UrlEncoderStringLookup.java b/src/main/java/org/apache/commons/text/lookup/UrlEncoderStringLookup.java
new file mode 100644
index 0000000..20f9600
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/lookup/UrlEncoderStringLookup.java
@@ -0,0 +1,54 @@
+/*
+ * 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.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * Encodes URL Strings using the UTF-8 encoding.
+ *
+ * @see URLEncoder
+ * @since 1.5
+ */
+final class UrlEncoderStringLookup extends AbstractStringLookup {
+
+ /**
+ * Defines the singleton for this class.
+ */
+ static final UrlEncoderStringLookup INSTANCE = new UrlEncoderStringLookup();
+
+ /**
+ * No need to build instances for now.
+ */
+ private UrlEncoderStringLookup() {
+ // empty
+ }
+
+ @Override
+ public String lookup(final String key) {
+ final String enc = StandardCharsets.UTF_8.name();
+ try {
+ return new String(URLEncoder.encode(key, enc));
+ } catch (UnsupportedEncodingException e) {
+ throw IllegalArgumentExceptions.format(e, "%s: source=%s, encoding=%s", e, key, enc);
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/65ecfab2/src/test/java/org/apache/commons/text/lookup/UrlDecoderStringLookupTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/lookup/UrlDecoderStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/UrlDecoderStringLookupTest.java
new file mode 100644
index 0000000..9fc628c
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/lookup/UrlDecoderStringLookupTest.java
@@ -0,0 +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;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link UrlDecoderStringLookup}.
+ */
+public class UrlDecoderStringLookupTest {
+
+ @Test
+ public void testAllPercent() {
+ Assertions.assertEquals("Hello World!", UrlDecoderStringLookup.INSTANCE.lookup("Hello%20World%21"));
+ }
+
+ @Test
+ public void testExclamation() {
+ Assertions.assertEquals("Hello World!", UrlDecoderStringLookup.INSTANCE.lookup("Hello%20World!"));
+ }
+
+ @Test
+ public void testPlus() {
+ Assertions.assertEquals("Hello World!", UrlDecoderStringLookup.INSTANCE.lookup("Hello+World!"));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/65ecfab2/src/test/java/org/apache/commons/text/lookup/UrlEncoderStringLookupTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/lookup/UrlEncoderStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/UrlEncoderStringLookupTest.java
new file mode 100644
index 0000000..c73c591
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/lookup/UrlEncoderStringLookupTest.java
@@ -0,0 +1,33 @@
+/*
+ * 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;
+
+/**
+ * Tests {@link UrlEncoderStringLookup}.
+ */
+public class UrlEncoderStringLookupTest {
+
+ @Test
+ public void test() {
+ Assertions.assertEquals("Hello+World%21", UrlEncoderStringLookup.INSTANCE.lookup("Hello World!"));
+ }
+
+}