You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ch...@apache.org on 2017/01/14 15:04:46 UTC
[1/4] [text] [TEXT-56] Move CvsTranslators out of StringEscapeUtils
and make them DRY
Repository: commons-text
Updated Branches:
refs/heads/master 1414ae2dd -> ea145ef5c
[TEXT-56] Move CvsTranslators out of StringEscapeUtils and make them DRY
Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/ac536cda
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/ac536cda
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/ac536cda
Branch: refs/heads/master
Commit: ac536cda62397e5720e5c8cd1db7799680f7d5bc
Parents: a28e32e
Author: Jaroslaw Strzelecki <ja...@users.noreply.github.com>
Authored: Fri Jan 6 21:56:00 2017 +0100
Committer: Jaroslaw Strzelecki <ja...@users.noreply.github.com>
Committed: Fri Jan 6 21:56:00 2017 +0100
----------------------------------------------------------------------
.../apache/commons/text/StringEscapeUtils.java | 68 +---------------
.../commons/text/translate/CsvTranslators.java | 82 ++++++++++++++++++++
.../text/translate/SinglePassTranslator.java | 53 +++++++++++++
.../translate/SinglePassTranslatorTest.java | 57 ++++++++++++++
4 files changed, 195 insertions(+), 65 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-text/blob/ac536cda/src/main/java/org/apache/commons/text/StringEscapeUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/StringEscapeUtils.java b/src/main/java/org/apache/commons/text/StringEscapeUtils.java
index 57eb92a..a315819 100644
--- a/src/main/java/org/apache/commons/text/StringEscapeUtils.java
+++ b/src/main/java/org/apache/commons/text/StringEscapeUtils.java
@@ -16,10 +16,10 @@
*/
package org.apache.commons.text;
-import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.translate.AggregateTranslator;
import org.apache.commons.text.translate.CharSequenceTranslator;
+import org.apache.commons.text.translate.CsvTranslators;
import org.apache.commons.text.translate.EntityArrays;
import org.apache.commons.text.translate.JavaUnicodeEscaper;
import org.apache.commons.text.translate.LookupTranslator;
@@ -254,36 +254,7 @@ public class StringEscapeUtils {
* object allows the CSV escaping functionality to be used
* as the foundation for a custom translator.
*/
- public static final CharSequenceTranslator ESCAPE_CSV = new CsvEscaper();
-
- // TODO: Create a parent class - 'SinglePassTranslator' ?
- // It would handle the index checking + length returning,
- // and could also have an optimization check method.
- static class CsvEscaper extends CharSequenceTranslator {
-
- private static final char CSV_DELIMITER = ',';
- private static final char CSV_QUOTE = '"';
- private static final String CSV_QUOTE_STR = String.valueOf(CSV_QUOTE);
- private static final char[] CSV_SEARCH_CHARS =
- new char[] {CSV_DELIMITER, CSV_QUOTE, CharUtils.CR, CharUtils.LF};
-
- @Override
- public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
-
- if(index != 0) {
- throw new IllegalStateException("CsvEscaper should never reach the [1] index");
- }
-
- if (StringUtils.containsNone(input.toString(), CSV_SEARCH_CHARS)) {
- out.write(input.toString());
- } else {
- out.write(CSV_QUOTE);
- out.write(StringUtils.replace(input.toString(), CSV_QUOTE_STR, CSV_QUOTE_STR + CSV_QUOTE_STR));
- out.write(CSV_QUOTE);
- }
- return Character.codePointCount(input, 0, input.length());
- }
- }
+ public static final CharSequenceTranslator ESCAPE_CSV = new CsvTranslators.CsvEscaper();
/**
* Translator object for escaping Shell command language.
@@ -410,40 +381,7 @@ public class StringEscapeUtils {
* object allows the CSV unescaping functionality to be used
* as the foundation for a custom translator.
*/
- public static final CharSequenceTranslator UNESCAPE_CSV = new CsvUnescaper();
-
- static class CsvUnescaper extends CharSequenceTranslator {
-
- private static final char CSV_DELIMITER = ',';
- private static final char CSV_QUOTE = '"';
- private static final String CSV_QUOTE_STR = String.valueOf(CSV_QUOTE);
- private static final char[] CSV_SEARCH_CHARS =
- new char[] {CSV_DELIMITER, CSV_QUOTE, CharUtils.CR, CharUtils.LF};
-
- @Override
- public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
-
- if(index != 0) {
- throw new IllegalStateException("CsvUnescaper should never reach the [1] index");
- }
-
- if ( input.charAt(0) != CSV_QUOTE || input.charAt(input.length() - 1) != CSV_QUOTE ) {
- out.write(input.toString());
- return Character.codePointCount(input, 0, input.length());
- }
-
- // strip quotes
- final String quoteless = input.subSequence(1, input.length() - 1).toString();
-
- if ( StringUtils.containsAny(quoteless, CSV_SEARCH_CHARS) ) {
- // deal with escaped quotes; ie) ""
- out.write(StringUtils.replace(quoteless, CSV_QUOTE_STR + CSV_QUOTE_STR, CSV_QUOTE_STR));
- } else {
- out.write(input.toString());
- }
- return Character.codePointCount(input, 0, input.length());
- }
- }
+ public static final CharSequenceTranslator UNESCAPE_CSV = new CsvTranslators.CsvUnescaper();
public static final CharSequenceTranslator UNESCAPE_XSI = new XsiUnescaper();
http://git-wip-us.apache.org/repos/asf/commons-text/blob/ac536cda/src/main/java/org/apache/commons/text/translate/CsvTranslators.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/translate/CsvTranslators.java b/src/main/java/org/apache/commons/text/translate/CsvTranslators.java
new file mode 100644
index 0000000..f11b9fe
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/translate/CsvTranslators.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.translate;
+
+import org.apache.commons.lang3.CharUtils;
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * This class holds inner classes for escaping/unescaping Comma Separated Values.
+ */
+public class CsvTranslators {
+
+ private static final char CSV_DELIMITER = ',';
+ private static final char CSV_QUOTE = '"';
+ private static final String CSV_QUOTE_STR = String.valueOf(CSV_QUOTE);
+ private static final String CSV_ESCAPED_QUOTE_STR = CSV_QUOTE_STR + CSV_QUOTE_STR;
+ private static final char[] CSV_SEARCH_CHARS =
+ new char[] {CSV_DELIMITER, CSV_QUOTE, CharUtils.CR, CharUtils.LF};
+
+ private CsvTranslators() { }
+
+ /**
+ * Translator for escaping Comma Separated Values.
+ */
+ public static class CsvEscaper extends SinglePassTranslator {
+
+ @Override
+ void translateWhole(final CharSequence input, final Writer out) throws IOException {
+ final String inputSting = input.toString();
+ if (StringUtils.containsNone(inputSting, CSV_SEARCH_CHARS)) {
+ out.write(inputSting);
+ } else {
+ // input needs quoting
+ out.write(CSV_QUOTE);
+ out.write(StringUtils.replace(inputSting, CSV_QUOTE_STR, CSV_ESCAPED_QUOTE_STR));
+ out.write(CSV_QUOTE);
+ }
+ }
+ }
+
+ /**
+ * Translator for unescaping escaped Comma Separated Value entries.
+ */
+ public static class CsvUnescaper extends SinglePassTranslator {
+
+ @Override
+ void translateWhole(final CharSequence input, final Writer out) throws IOException {
+ // is input not quoted?
+ if (input.charAt(0) != CSV_QUOTE || input.charAt(input.length() - 1) != CSV_QUOTE) {
+ out.write(input.toString());
+ return;
+ }
+
+ // strip quotes
+ final String quoteless = input.subSequence(1, input.length() - 1).toString();
+
+ if (StringUtils.containsAny(quoteless, CSV_SEARCH_CHARS)) {
+ // deal with escaped quotes; ie) ""
+ out.write(StringUtils.replace(quoteless, CSV_ESCAPED_QUOTE_STR, CSV_QUOTE_STR));
+ } else {
+ out.write(input.toString());
+ }
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/commons-text/blob/ac536cda/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java b/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java
new file mode 100644
index 0000000..21124ff
--- /dev/null
+++ b/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java
@@ -0,0 +1,53 @@
+/*
+ * 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.translate;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * Abstract translator for processing whole input in single pass.
+ * Handles initial index checking and counting of returned code points.
+ */
+abstract class SinglePassTranslator extends CharSequenceTranslator {
+
+ @Override
+ public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
+ if (index != 0) {
+ throw new IllegalStateException(getClassName() + " should never reach index different than 0");
+ }
+
+ translateWhole(input, out);
+
+ return Character.codePointCount(input, 0, input.length());
+ }
+
+ private String getClassName() {
+ final Class clazz = this.getClass();
+ return clazz.isAnonymousClass() ? clazz.getName() : clazz.getSimpleName();
+ }
+
+ /**
+ * Translate whole set of code points passed in input.
+ *
+ * @param input CharSequence that is being translated
+ * @param out Writer to translate the text to
+ * @return total count of codepoints in input
+ * @throws IOException if and only if the Writer produces an IOException
+ */
+ abstract void translateWhole(final CharSequence input, final Writer out) throws IOException;
+}
http://git-wip-us.apache.org/repos/asf/commons-text/blob/ac536cda/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java b/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java
new file mode 100644
index 0000000..d7858c5
--- /dev/null
+++ b/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.translate;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit test for {@link SinglePassTranslator}
+ */
+public class SinglePassTranslatorTest {
+
+ private final SinglePassTranslator dummyTranslator = new SinglePassTranslator() {
+ @Override
+ void translateWhole(final CharSequence input, final Writer out) throws IOException {
+ }
+ };
+
+ private StringWriter out;
+
+ @Before
+ public void before() {
+ out = new StringWriter();
+ }
+
+ @Test
+ public void codePointsAreReturned() throws Exception {
+ assertEquals(0, dummyTranslator.translate("", 0, out));
+ assertEquals(3, dummyTranslator.translate("abc", 0, out));
+ assertEquals(7, dummyTranslator.translate("abcdefg", 0, out));
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void indexIsValidated() throws Exception {
+ dummyTranslator.translate("abc", 1, out);
+ }
+}
\ No newline at end of file
[4/4] [text] TEXT-56: giving Jarek Strzeleck proper credit
Posted by ch...@apache.org.
TEXT-56: giving Jarek Strzeleck proper credit
Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/ea145ef5
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/ea145ef5
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/ea145ef5
Branch: refs/heads/master
Commit: ea145ef5ca8b14b71b0eb545d7e1d26d73c6e0fe
Parents: d305173
Author: Rob Tompkins <ch...@gmail.com>
Authored: Sat Jan 14 10:04:31 2017 -0500
Committer: Rob Tompkins <ch...@gmail.com>
Committed: Sat Jan 14 10:04:31 2017 -0500
----------------------------------------------------------------------
pom.xml | 3 +++
src/changes/changes.xml | 1 +
2 files changed, 4 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-text/blob/ea145ef5/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 61cbe8f..f134d69 100644
--- a/pom.xml
+++ b/pom.xml
@@ -78,6 +78,9 @@
<contributor>
<name>Sampanna Kahu</name>
</contributor>
+ <contributor>
+ <name>Jarek Strzeleck</name>
+ </contributor>
</contributors>
<dependencies>
http://git-wip-us.apache.org/repos/asf/commons-text/blob/ea145ef5/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 09a684c..7bd77b2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -22,6 +22,7 @@
<body>
<release version="1.0" date="tba" description="tba">
+ <action issue="TEXT-56" type="add" dev="chtompki" due-to="Jarek Strzeleck">Move CvsTranslators out of StringEscapeUtils and make them DRY</action>
<action issue="TEXT-55" type="remove" dev="chtompki">Remove WordUtils to be added back in an upcoming 1.X release</action>
<action issue="TEXT-52" type="fix" dev="chtompki">Possible attacks through StringEscapeUtils.escapeEcmaScrip better javadoc</action>
<action issue="TEXT-51" type="remove" dev="chtompki">Remove RandomStringGenerator to be added back in the 1.1 release</action>
[2/4] [text] Merge branch 'feature/CvsTranslators' of
https://github.com/jarst/commons-text
Posted by ch...@apache.org.
Merge branch 'feature/CvsTranslators' of https://github.com/jarst/commons-text
Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/dba22a2f
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/dba22a2f
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/dba22a2f
Branch: refs/heads/master
Commit: dba22a2f5b03d595c0d9ce681a1c3b2018a75f1d
Parents: 1414ae2 ac536cd
Author: Rob Tompkins <ch...@gmail.com>
Authored: Sat Jan 14 09:54:17 2017 -0500
Committer: Rob Tompkins <ch...@gmail.com>
Committed: Sat Jan 14 09:54:17 2017 -0500
----------------------------------------------------------------------
.../apache/commons/text/StringEscapeUtils.java | 68 +---------------
.../commons/text/translate/CsvTranslators.java | 82 ++++++++++++++++++++
.../text/translate/SinglePassTranslator.java | 53 +++++++++++++
.../translate/SinglePassTranslatorTest.java | 57 ++++++++++++++
4 files changed, 195 insertions(+), 65 deletions(-)
----------------------------------------------------------------------
[3/4] [text] TEXT-56: IllegalAgumentException
SinglePassTranslator.translate index!=0
Posted by ch...@apache.org.
TEXT-56: IllegalAgumentException SinglePassTranslator.translate index!=0
Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/d3051739
Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/d3051739
Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/d3051739
Branch: refs/heads/master
Commit: d3051739d9fc77890218a8dbebdc9560d12532a7
Parents: dba22a2
Author: Rob Tompkins <ch...@gmail.com>
Authored: Sat Jan 14 10:00:44 2017 -0500
Committer: Rob Tompkins <ch...@gmail.com>
Committed: Sat Jan 14 10:00:44 2017 -0500
----------------------------------------------------------------------
.../org/apache/commons/text/translate/SinglePassTranslator.java | 5 +++--
.../apache/commons/text/translate/SinglePassTranslatorTest.java | 2 +-
2 files changed, 4 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-text/blob/d3051739/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java b/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java
index 21124ff..321136a 100644
--- a/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java
+++ b/src/main/java/org/apache/commons/text/translate/SinglePassTranslator.java
@@ -28,12 +28,13 @@ abstract class SinglePassTranslator extends CharSequenceTranslator {
@Override
public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
if (index != 0) {
- throw new IllegalStateException(getClassName() + " should never reach index different than 0");
+ throw new IllegalArgumentException(getClassName() + ".translate(final CharSequence input, final int " +
+ "index, final Writer out) can not handle a non-zero index.");
}
translateWhole(input, out);
- return Character.codePointCount(input, 0, input.length());
+ return Character.codePointCount(input, index, input.length());
}
private String getClassName() {
http://git-wip-us.apache.org/repos/asf/commons-text/blob/d3051739/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java b/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java
index d7858c5..d36d6e2 100644
--- a/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java
+++ b/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java
@@ -50,7 +50,7 @@ public class SinglePassTranslatorTest {
assertEquals(7, dummyTranslator.translate("abcdefg", 0, out));
}
- @Test(expected = IllegalStateException.class)
+ @Test(expected = IllegalArgumentException.class)
public void indexIsValidated() throws Exception {
dummyTranslator.translate("abc", 1, out);
}