Reverting r1090111 - moving the text.translate escapers back from using Range to replicating parts of the Range API. See the list for details ('unnecessary boxing in StringEscapeUtils etc'), the move to Range was an uncomfortable fit.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1142151 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2011-07-02 04:06:23 +00:00
parent 1d41504168
commit 8de2366fc1
5 changed files with 143 additions and 45 deletions

View File

@ -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)
);
/**

View File

@ -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<Integer> range;
private final int below;
private final int above;
private final boolean between;
/**
* <p>Constructs a <code>NumericEntityEscaper</code> for the specified range. This is
* the underlying method for the other constructors/builders. </p>
* the underlying method for the other constructors/builders. The <code>below</code>
* and <code>above</code> boundaries are inclusive when <code>between</code> is
* <code>true</code> and exclusive when it is <code>false</code>. </p>
*
* @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<Integer> range) {
this.range = range;
private NumericEntityEscaper(int below, int above, boolean between) {
this.below = below;
this.above = above;
this.between = between;
}
/**
* <p>Constructs a <code>NumericEntityEscaper</code> for all characters. </p>
*/
public NumericEntityEscaper() {
this.range = Range.between(0, Integer.MAX_VALUE);
this(0, Integer.MAX_VALUE, true);
}
/**
* <p>Constructs a <code>NumericEntityEscaper</code> below the specified value (exclusive). </p>
*
* @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);
}
/**
* <p>Constructs a <code>NumericEntityEscaper</code> above the specified value (exclusive). </p>
*
* @param codepoint above which to escape
* @return the newly created {@code NumericEntityEscaper} instance
*/
public static NumericEntityEscaper above(int codepoint) {
return outsideOf(0, codepoint);
}
/**
* <p>Constructs a <code>NumericEntityEscaper</code> between the specified values (inclusive). </p>
*
* @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);
}
/**
* <p>Constructs a <code>NumericEntityEscaper</code> outside of the specified values (exclusive). </p>
*
* @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("&#");

View File

@ -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<Integer> range;
/**
* <p>Constructs a <code>UnicodeEscaper</code> for the specified range. This is
* the underlying method for the other constructors/builders. </p>
*
* @param range range within which to escape entities
*/
public UnicodeEscaper(Range<Integer> range) {
this.range = range;
}
private final int below;
private final int above;
private final boolean between;
/**
* <p>Constructs a <code>UnicodeEscaper</code> for all characters. </p>
*/
public UnicodeEscaper() {
this.range = Range.between(0, Integer.MAX_VALUE);
public UnicodeEscaper(){
this(0, Integer.MAX_VALUE, true);
}
/**
* <p>Constructs a <code>UnicodeEscaper</code> for the specified range. This is
* the underlying method for the other constructors/builders. The <code>below</code>
* and <code>above</code> boundaries are inclusive when <code>between</code> is
* <code>true</code> and exclusive when it is <code>false</code>. </p>
*
* @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;
}
/**
* <p>Constructs a <code>UnicodeEscaper</code> below the specified value (exclusive). </p>
*
* @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);
}
/**
* <p>Constructs a <code>UnicodeEscaper</code> above the specified value (exclusive). </p>
*
* @param codepoint above which to escape
* @return the newly created {@code UnicodeEscaper} instance
*/
public static UnicodeEscaper above(int codepoint) {
return outsideOf(0, codepoint);
}
/**
* <p>Constructs a <code>UnicodeEscaper</code> outside of the specified values (exclusive). </p>
*
* @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);
}
/**
* <p>Constructs a <code>UnicodeEscaper</code> between the specified values (inclusive). </p>
*
* @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

View File

@ -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);

View File

@ -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);