Author: ggregory
Date: Sat Nov 24 01:56:20 2012
New Revision: 1413114
URL: http://svn.apache.org/viewvc?rev=1413114&view=rev
Log:
<action issue="LANG-858" type="fix">StringEscapeUtils.escapeJava() and
escapeEcmaScript() do not output the escaped surrogate pairs that are Java
parsable</action>
Added:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/JavaUnicodeEscaper.java
(with props)
Modified:
commons/proper/lang/trunk/src/changes/changes.xml
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/UnicodeEscaper.java
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1413114&r1=1413113&r2=1413114&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml Sat Nov 24 01:56:20 2012
@@ -22,6 +22,7 @@
<body>
<release version="3.2" date="TBA" description="Next release">
+ <action issue="LANG-858" type="fix">StringEscapeUtils.escapeJava() and
escapeEcmaScript() do not output the escaped surrogate pairs that are Java
parsable</action>
<action issue="LANG-857" type="add">StringIndexOutOfBoundsException in
CharSequenceTranslator</action>
<action issue="LANG-856" type="add">Code refactoring in
NumberUtils</action>
<action issue="LANG-855" type="add">NumberUtils#createBigInteger does not
allow for hex and octal numbers</action>
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java?rev=1413114&r1=1413113&r2=1413114&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java
(original)
+++
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringEscapeUtils.java
Sat Nov 24 01:56:20 2012
@@ -22,6 +22,7 @@ import java.io.Writer;
import org.apache.commons.lang3.text.translate.AggregateTranslator;
import org.apache.commons.lang3.text.translate.CharSequenceTranslator;
import org.apache.commons.lang3.text.translate.EntityArrays;
+import org.apache.commons.lang3.text.translate.JavaUnicodeEscaper;
import org.apache.commons.lang3.text.translate.LookupTranslator;
import org.apache.commons.lang3.text.translate.NumericEntityUnescaper;
import org.apache.commons.lang3.text.translate.OctalUnescaper;
@@ -57,7 +58,7 @@ public class StringEscapeUtils {
}).with(
new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE())
).with(
- UnicodeEscaper.outsideOf(32, 0x7f)
+ JavaUnicodeEscaper.outsideOf(32, 0x7f)
);
/**
@@ -79,7 +80,7 @@ public class StringEscapeUtils {
{"/", "\\/"}
}),
new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE()),
- UnicodeEscaper.outsideOf(32, 0x7f)
+ JavaUnicodeEscaper.outsideOf(32, 0x7f)
);
/**
Added:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/JavaUnicodeEscaper.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/JavaUnicodeEscaper.java?rev=1413114&view=auto
==============================================================================
---
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/JavaUnicodeEscaper.java
(added)
+++
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/JavaUnicodeEscaper.java
Sat Nov 24 01:56:20 2012
@@ -0,0 +1,96 @@
+package org.apache.commons.lang3.text.translate;
+
+/**
+ * Translates codepoints to their Unicode escaped value suitable for Java
source.
+ *
+ * @since 3.2
+ * @version $Id$
+ */
+public class JavaUnicodeEscaper extends UnicodeEscaper {
+
+ /**
+ * <p>
+ * Constructs a <code>JavaUnicodeEscaper</code> above the specified value
(exclusive).
+ * </p>
+ *
+ * @param codepoint
+ * above which to escape
+ * @return the newly created {@code UnicodeEscaper} instance
+ */
+ public static JavaUnicodeEscaper above(int codepoint) {
+ return outsideOf(0, codepoint);
+ }
+
+ /**
+ * <p>
+ * Constructs a <code>JavaUnicodeEscaper</code> below the specified value
(exclusive).
+ * </p>
+ *
+ * @param codepoint
+ * below which to escape
+ * @return the newly created {@code UnicodeEscaper} instance
+ */
+ public static JavaUnicodeEscaper below(int codepoint) {
+ return outsideOf(codepoint, Integer.MAX_VALUE);
+ }
+
+ /**
+ * <p>
+ * Constructs a <code>JavaUnicodeEscaper</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 JavaUnicodeEscaper between(int codepointLow, int
codepointHigh) {
+ return new JavaUnicodeEscaper(codepointLow, codepointHigh, true);
+ }
+
+ /**
+ * <p>
+ * Constructs a <code>JavaUnicodeEscaper</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 JavaUnicodeEscaper outsideOf(int codepointLow, int
codepointHigh) {
+ return new JavaUnicodeEscaper(codepointLow, codepointHigh, false);
+ }
+
+ /**
+ * <p>
+ * Constructs a <code>JavaUnicodeEscaper</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
+ */
+ public JavaUnicodeEscaper(int below, int above, boolean between) {
+ super(below, above, between);
+ }
+
+ /**
+ * Converts the given codepoint to a hex string of the form {@code
"\\uXXXX\\uXXXX"}
+ *
+ * @param codePoint
+ * a Unicode code point
+ */
+ protected String toUtf16Escape(int codepoint) {
+ char[] surrogatePair = Character.toChars(codepoint);
+ return "\\u" + hex(surrogatePair[0]) + "\\u" + hex(surrogatePair[1]);
+ }
+
+}
Propchange:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/JavaUnicodeEscaper.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/JavaUnicodeEscaper.java
------------------------------------------------------------------------------
svn:keywords = Id
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/UnicodeEscaper.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/UnicodeEscaper.java?rev=1413114&r1=1413113&r2=1413114&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/UnicodeEscaper.java
(original)
+++
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/UnicodeEscaper.java
Sat Nov 24 01:56:20 2012
@@ -48,7 +48,7 @@ public class UnicodeEscaper extends Code
* @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) {
+ protected UnicodeEscaper(int below, int above, boolean between) {
this.below = below;
this.above = above;
this.between = between;
@@ -101,7 +101,7 @@ public class UnicodeEscaper extends Code
*/
@Override
public boolean translate(int codepoint, Writer out) throws IOException {
- if(between) {
+ if (between) {
if (codepoint < below || codepoint > above) {
return false;
}
@@ -113,9 +113,7 @@ public class UnicodeEscaper extends Code
// TODO: Handle potential + sign per various Unicode escape
implementations
if (codepoint > 0xffff) {
- // TODO: Figure out what to do. Output as two Unicodes?
- // Does this make this a Java-specific output class?
- out.write("\\u" + hex(codepoint));
+ out.write(toUtf16Escape(codepoint));
} else if (codepoint > 0xfff) {
out.write("\\u" + hex(codepoint));
} else if (codepoint > 0xff) {
@@ -127,4 +125,8 @@ public class UnicodeEscaper extends Code
}
return true;
}
+
+ protected String toUtf16Escape(int codepoint) {
+ return "\\u" + hex(codepoint);
+ }
}
Modified:
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java?rev=1413114&r1=1413113&r2=1413114&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
(original)
+++
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
Sat Nov 24 01:56:20 2012
@@ -2201,7 +2201,6 @@ public class StringUtilsTest {
* @throws Exception
*/
@Test
- @Ignore
public void testEscapeSurrogatePairsLang858() throws Exception {
assertEquals("\\uDBFF\\uDFFD",
StringEscapeUtils.escapeJava("\uDBFF\uDFFD")); //fail LANG-858
assertEquals("\\uDBFF\\uDFFD",
StringEscapeUtils.escapeEcmaScript("\uDBFF\uDFFD")); //fail LANG-858