Moving text translation classes to the Range class for its configuration

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1090111 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2011-04-08 04:47:21 +00:00
parent 0d01e050fa
commit 287b2cd800
5 changed files with 45 additions and 143 deletions

View File

@ -57,7 +57,10 @@ public class StringEscapeUtils {
}).with(
new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE())
).with(
UnicodeEscaper.outsideOf(32, 0x7f)
new AggregateTranslator(
new UnicodeEscaper(Range.between(0, 31)),
new UnicodeEscaper(Range.between(0x80, Integer.MAX_VALUE))
)
);
/**
@ -79,7 +82,10 @@ public class StringEscapeUtils {
{"/", "\\/"}
}),
new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE()),
UnicodeEscaper.outsideOf(32, 0x7f)
new AggregateTranslator(
new UnicodeEscaper(Range.between(0, 31)),
new UnicodeEscaper(Range.between(0x80, Integer.MAX_VALUE))
)
);
/**

View File

@ -19,6 +19,8 @@ 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.
*
@ -27,73 +29,23 @@ import java.io.Writer;
*/
public class NumericEntityEscaper extends CodePointTranslator {
private final int below;
private final int above;
private final boolean between;
private Range<Integer> range;
/**
* <p>Constructs a <code>NumericEntityEscaper</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>
* the underlying method for the other constructors/builders. </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
* @param range range within which to escape entities
*/
private NumericEntityEscaper(int below, int above, boolean between) {
this.below = below;
this.above = above;
this.between = between;
public NumericEntityEscaper(Range<Integer> range) {
this.range = range;
}
/**
* <p>Constructs a <code>NumericEntityEscaper</code> for all characters. </p>
*/
public NumericEntityEscaper() {
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);
this.range = Range.between(0, Integer.MAX_VALUE);
}
/**
@ -101,14 +53,8 @@ public class NumericEntityEscaper extends CodePointTranslator {
*/
@Override
public boolean translate(int codepoint, Writer out) throws IOException {
if(between) {
if (codepoint < below || codepoint > above) {
return false;
}
} else {
if (codepoint >= below && codepoint <= above) {
return false;
}
if(!range.contains(codepoint)) {
return false;
}
out.write("&#");

View File

@ -19,6 +19,8 @@ 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.
*
@ -27,73 +29,23 @@ import java.io.Writer;
*/
public class UnicodeEscaper extends CodePointTranslator {
private final int below;
private final int above;
private final boolean between;
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;
}
/**
* <p>Constructs a <code>UnicodeEscaper</code> for all characters. </p>
*/
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);
public UnicodeEscaper() {
this.range = Range.between(0, Integer.MAX_VALUE);
}
/**
@ -101,14 +53,8 @@ public class UnicodeEscaper extends CodePointTranslator {
*/
@Override
public boolean translate(int codepoint, Writer out) throws IOException {
if(between) {
if (codepoint < below || codepoint > above) {
return false;
}
} else {
if (codepoint >= below && codepoint <= above) {
return false;
}
if(!range.contains(codepoint)) {
return false;
}
// TODO: Handle potential + sign per various unicode escape implementations

View File

@ -19,6 +19,8 @@ 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$
@ -26,7 +28,7 @@ import junit.framework.TestCase;
public class NumericEntityEscaperTest extends TestCase {
public void testBelow() {
NumericEntityEscaper nee = NumericEntityEscaper.below('F');
NumericEntityEscaper nee = new NumericEntityEscaper(Range.between(0, (int)'E'));
String input = "ADFGZ";
String result = nee.translate(input);
@ -34,7 +36,7 @@ public class NumericEntityEscaperTest extends TestCase {
}
public void testBetween() {
NumericEntityEscaper nee = NumericEntityEscaper.between('F', 'L');
NumericEntityEscaper nee = new NumericEntityEscaper(Range.between((int)'F', (int)'L'));
String input = "ADFGZ";
String result = nee.translate(input);
@ -42,7 +44,7 @@ public class NumericEntityEscaperTest extends TestCase {
}
public void testAbove() {
NumericEntityEscaper nee = NumericEntityEscaper.above('F');
NumericEntityEscaper nee = new NumericEntityEscaper(Range.between((int)'G', Integer.MAX_VALUE));
String input = "ADFGZ";
String result = nee.translate(input);

View File

@ -19,6 +19,8 @@ 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$
@ -26,7 +28,7 @@ import junit.framework.TestCase;
public class UnicodeEscaperTest extends TestCase {
public void testBelow() {
UnicodeEscaper ue = UnicodeEscaper.below('F');
UnicodeEscaper ue = new UnicodeEscaper(Range.between(0, (int)'E'));
String input = "ADFGZ";
String result = ue.translate(input);
@ -34,7 +36,7 @@ public class UnicodeEscaperTest extends TestCase {
}
public void testBetween() {
UnicodeEscaper ue = UnicodeEscaper.between('F', 'L');
UnicodeEscaper ue = new UnicodeEscaper(Range.between((int)'F', (int)'L'));
String input = "ADFGZ";
String result = ue.translate(input);
@ -42,7 +44,7 @@ public class UnicodeEscaperTest extends TestCase {
}
public void testAbove() {
UnicodeEscaper ue = UnicodeEscaper.above('F');
UnicodeEscaper ue = new UnicodeEscaper(Range.between((int)'G', Integer.MAX_VALUE));
String input = "ADFGZ";
String result = ue.translate(input);