diff --git a/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java b/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java index a05c2f249..d84aae58a 100644 --- a/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java @@ -57,10 +57,7 @@ public class StringEscapeUtils { }).with( new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE()) ).with( - new AggregateTranslator( - new UnicodeEscaper(Range.between(0, 31)), - new UnicodeEscaper(Range.between(0x80, Integer.MAX_VALUE)) - ) + UnicodeEscaper.outsideOf(32, 0x7f) ); /** @@ -82,10 +79,7 @@ public class StringEscapeUtils { {"/", "\\/"} }), new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE()), - new AggregateTranslator( - new UnicodeEscaper(Range.between(0, 31)), - new UnicodeEscaper(Range.between(0x80, Integer.MAX_VALUE)) - ) + UnicodeEscaper.outsideOf(32, 0x7f) ); /** diff --git a/src/main/java/org/apache/commons/lang3/text/translate/NumericEntityEscaper.java b/src/main/java/org/apache/commons/lang3/text/translate/NumericEntityEscaper.java index 57ce842f3..0bf0bf947 100644 --- a/src/main/java/org/apache/commons/lang3/text/translate/NumericEntityEscaper.java +++ b/src/main/java/org/apache/commons/lang3/text/translate/NumericEntityEscaper.java @@ -19,8 +19,6 @@ package org.apache.commons.lang3.text.translate; import java.io.IOException; import java.io.Writer; -import org.apache.commons.lang3.Range; - /** * Translates codepoints to their XML numeric entity escaped value. * @@ -29,23 +27,73 @@ import org.apache.commons.lang3.Range; */ public class NumericEntityEscaper extends CodePointTranslator { - private Range range; + private final int below; + private final int above; + private final boolean between; /** *

Constructs a NumericEntityEscaper for the specified range. This is - * the underlying method for the other constructors/builders.

+ * the underlying method for the other constructors/builders. The below + * and above boundaries are inclusive when between is + * true and exclusive when it is false.

* - * @param range range within which to escape entities + * @param below int value representing the lowest codepoint boundary + * @param above int value representing the highest codepoint boundary + * @param between whether to escape between the boundaries or outside them */ - public NumericEntityEscaper(Range range) { - this.range = range; + private NumericEntityEscaper(int below, int above, boolean between) { + this.below = below; + this.above = above; + this.between = between; } /** *

Constructs a NumericEntityEscaper for all characters.

*/ public NumericEntityEscaper() { - this.range = Range.between(0, Integer.MAX_VALUE); + this(0, Integer.MAX_VALUE, true); + } + + /** + *

Constructs a NumericEntityEscaper below the specified value (exclusive).

+ * + * @param codepoint below which to escape + * @return the newly created {@code NumericEntityEscaper} instance + */ + public static NumericEntityEscaper below(int codepoint) { + return outsideOf(codepoint, Integer.MAX_VALUE); + } + + /** + *

Constructs a NumericEntityEscaper above the specified value (exclusive).

+ * + * @param codepoint above which to escape + * @return the newly created {@code NumericEntityEscaper} instance + */ + public static NumericEntityEscaper above(int codepoint) { + return outsideOf(0, codepoint); + } + + /** + *

Constructs a NumericEntityEscaper between the specified values (inclusive).

+ * + * @param codepointLow above which to escape + * @param codepointHigh below which to escape + * @return the newly created {@code NumericEntityEscaper} instance + */ + public static NumericEntityEscaper between(int codepointLow, int codepointHigh) { + return new NumericEntityEscaper(codepointLow, codepointHigh, true); + } + + /** + *

Constructs a NumericEntityEscaper outside of the specified values (exclusive).

+ * + * @param codepointLow below which to escape + * @param codepointHigh above which to escape + * @return the newly created {@code NumericEntityEscaper} instance + */ + public static NumericEntityEscaper outsideOf(int codepointLow, int codepointHigh) { + return new NumericEntityEscaper(codepointLow, codepointHigh, false); } /** @@ -53,8 +101,14 @@ public class NumericEntityEscaper extends CodePointTranslator { */ @Override public boolean translate(int codepoint, Writer out) throws IOException { - if(!range.contains(codepoint)) { - return false; + if(between) { + if (codepoint < below || codepoint > above) { + return false; + } + } else { + if (codepoint >= below && codepoint <= above) { + return false; + } } out.write("&#"); diff --git a/src/main/java/org/apache/commons/lang3/text/translate/UnicodeEscaper.java b/src/main/java/org/apache/commons/lang3/text/translate/UnicodeEscaper.java index c74e1b329..3ffc8ab11 100644 --- a/src/main/java/org/apache/commons/lang3/text/translate/UnicodeEscaper.java +++ b/src/main/java/org/apache/commons/lang3/text/translate/UnicodeEscaper.java @@ -19,8 +19,6 @@ package org.apache.commons.lang3.text.translate; import java.io.IOException; import java.io.Writer; -import org.apache.commons.lang3.Range; - /** * Translates codepoints to their unicode escaped value. * @@ -29,23 +27,73 @@ import org.apache.commons.lang3.Range; */ public class UnicodeEscaper extends CodePointTranslator { - private Range range; - - /** - *

Constructs a UnicodeEscaper for the specified range. This is - * the underlying method for the other constructors/builders.

- * - * @param range range within which to escape entities - */ - public UnicodeEscaper(Range range) { - this.range = range; - } + private final int below; + private final int above; + private final boolean between; /** *

Constructs a UnicodeEscaper for all characters.

*/ - public UnicodeEscaper() { - this.range = Range.between(0, Integer.MAX_VALUE); + public UnicodeEscaper(){ + this(0, Integer.MAX_VALUE, true); + } + + /** + *

Constructs a UnicodeEscaper for the specified range. This is + * the underlying method for the other constructors/builders. The below + * and above boundaries are inclusive when between is + * true and exclusive when it is false.

+ * + * @param below int value representing the lowest codepoint boundary + * @param above int value representing the highest codepoint boundary + * @param between whether to escape between the boundaries or outside them + */ + private UnicodeEscaper(int below, int above, boolean between) { + this.below = below; + this.above = above; + this.between = between; + } + + /** + *

Constructs a UnicodeEscaper below the specified value (exclusive).

+ * + * @param codepoint below which to escape + * @return the newly created {@code UnicodeEscaper} instance + */ + public static UnicodeEscaper below(int codepoint) { + return outsideOf(codepoint, Integer.MAX_VALUE); + } + + /** + *

Constructs a UnicodeEscaper above the specified value (exclusive).

+ * + * @param codepoint above which to escape + * @return the newly created {@code UnicodeEscaper} instance + */ + public static UnicodeEscaper above(int codepoint) { + return outsideOf(0, codepoint); + } + + /** + *

Constructs a UnicodeEscaper outside of the specified values (exclusive).

+ * + * @param codepointLow below which to escape + * @param codepointHigh above which to escape + * @return the newly created {@code UnicodeEscaper} instance + */ + public static UnicodeEscaper outsideOf(int codepointLow, int codepointHigh) { + return new UnicodeEscaper(codepointLow, codepointHigh, false); + } + + /** + *

Constructs a UnicodeEscaper between the specified values (inclusive).

+ * + * @param codepointLow above which to escape + * @param codepointHigh below which to escape + * @return the newly created {@code UnicodeEscaper} instance + */ + public static UnicodeEscaper between(int codepointLow, int codepointHigh) { + return new UnicodeEscaper(codepointLow, codepointHigh, true); } /** @@ -53,8 +101,14 @@ public class UnicodeEscaper extends CodePointTranslator { */ @Override public boolean translate(int codepoint, Writer out) throws IOException { - if(!range.contains(codepoint)) { - return false; + if(between) { + if (codepoint < below || codepoint > above) { + return false; + } + } else { + if (codepoint >= below && codepoint <= above) { + return false; + } } // TODO: Handle potential + sign per various unicode escape implementations diff --git a/src/test/java/org/apache/commons/lang3/text/translate/NumericEntityEscaperTest.java b/src/test/java/org/apache/commons/lang3/text/translate/NumericEntityEscaperTest.java index 4c2b4247f..c46badd4d 100644 --- a/src/test/java/org/apache/commons/lang3/text/translate/NumericEntityEscaperTest.java +++ b/src/test/java/org/apache/commons/lang3/text/translate/NumericEntityEscaperTest.java @@ -19,8 +19,6 @@ package org.apache.commons.lang3.text.translate; import junit.framework.TestCase; -import org.apache.commons.lang3.Range; - /** * Unit tests for {@link org.apache.commons.lang3.text.translate.NumericEntityEscaper}. * @version $Id$ @@ -28,7 +26,7 @@ import org.apache.commons.lang3.Range; public class NumericEntityEscaperTest extends TestCase { public void testBelow() { - NumericEntityEscaper nee = new NumericEntityEscaper(Range.between(0, (int)'E')); + NumericEntityEscaper nee = NumericEntityEscaper.below('F'); String input = "ADFGZ"; String result = nee.translate(input); @@ -36,7 +34,7 @@ public class NumericEntityEscaperTest extends TestCase { } public void testBetween() { - NumericEntityEscaper nee = new NumericEntityEscaper(Range.between((int)'F', (int)'L')); + NumericEntityEscaper nee = NumericEntityEscaper.between('F', 'L'); String input = "ADFGZ"; String result = nee.translate(input); @@ -44,7 +42,7 @@ public class NumericEntityEscaperTest extends TestCase { } public void testAbove() { - NumericEntityEscaper nee = new NumericEntityEscaper(Range.between((int)'G', Integer.MAX_VALUE)); + NumericEntityEscaper nee = NumericEntityEscaper.above('F'); String input = "ADFGZ"; String result = nee.translate(input); diff --git a/src/test/java/org/apache/commons/lang3/text/translate/UnicodeEscaperTest.java b/src/test/java/org/apache/commons/lang3/text/translate/UnicodeEscaperTest.java index fe83416f8..5ca5d33db 100644 --- a/src/test/java/org/apache/commons/lang3/text/translate/UnicodeEscaperTest.java +++ b/src/test/java/org/apache/commons/lang3/text/translate/UnicodeEscaperTest.java @@ -19,8 +19,6 @@ package org.apache.commons.lang3.text.translate; import junit.framework.TestCase; -import org.apache.commons.lang3.Range; - /** * Unit tests for {@link org.apache.commons.lang3.text.translate.UnicodeEscaper}. * @version $Id$ @@ -28,7 +26,7 @@ import org.apache.commons.lang3.Range; public class UnicodeEscaperTest extends TestCase { public void testBelow() { - UnicodeEscaper ue = new UnicodeEscaper(Range.between(0, (int)'E')); + UnicodeEscaper ue = UnicodeEscaper.below('F'); String input = "ADFGZ"; String result = ue.translate(input); @@ -36,7 +34,7 @@ public class UnicodeEscaperTest extends TestCase { } public void testBetween() { - UnicodeEscaper ue = new UnicodeEscaper(Range.between((int)'F', (int)'L')); + UnicodeEscaper ue = UnicodeEscaper.between('F', 'L'); String input = "ADFGZ"; String result = ue.translate(input); @@ -44,7 +42,7 @@ public class UnicodeEscaperTest extends TestCase { } public void testAbove() { - UnicodeEscaper ue = new UnicodeEscaper(Range.between((int)'G', Integer.MAX_VALUE)); + UnicodeEscaper ue = UnicodeEscaper.above('F'); String input = "ADFGZ"; String result = ue.translate(input);