diff --git a/src/java/org/apache/commons/lang/text/translate/UnicodeUnescaper.java b/src/java/org/apache/commons/lang/text/translate/UnicodeUnescaper.java index 638e75ecf..b28c90670 100644 --- a/src/java/org/apache/commons/lang/text/translate/UnicodeUnescaper.java +++ b/src/java/org/apache/commons/lang/text/translate/UnicodeUnescaper.java @@ -26,6 +26,15 @@ import java.io.Writer; */ public class UnicodeUnescaper extends CharSequenceTranslator { + private boolean escapingPlus = false; + + public void setEscapingPlus(boolean b) { + this.escapingPlus = b; + } + public boolean isEscapingPlus() { + return this.escapingPlus; + } + /** * {@inheritDoc} */ @@ -39,6 +48,13 @@ public class UnicodeUnescaper extends CharSequenceTranslator { i++; } + // consume + symbol in \\u+0045 + if(isEscapingPlus()) { + if( (index + i < input.length()) && (input.charAt(index + i) == '+') ) { + i++; + } + } + if( (index + i + 4 <= input.length()) ) { // Get 4 hex digits CharSequence unicode = input.subSequence(index + i, index + i + 4); @@ -47,7 +63,7 @@ public class UnicodeUnescaper extends CharSequenceTranslator { int value = Integer.parseInt(unicode.toString(), 16); out.write((char) value); } catch (NumberFormatException nfe) { - throw new RuntimeException("Unable to parse unicode value: " + unicode, nfe); + throw new IllegalArgumentException("Unable to parse unicode value: " + unicode, nfe); } return i + 4; } else { diff --git a/src/test/org/apache/commons/lang/text/translate/UnicodeUnescaperTest.java b/src/test/org/apache/commons/lang/text/translate/UnicodeUnescaperTest.java index 3a6f85a50..9982d8f96 100644 --- a/src/test/org/apache/commons/lang/text/translate/UnicodeUnescaperTest.java +++ b/src/test/org/apache/commons/lang/text/translate/UnicodeUnescaperTest.java @@ -27,6 +27,22 @@ import java.io.IOException; */ public class UnicodeUnescaperTest extends TestCase { + // Requested in LANG-507 + public void testUPlus() throws IOException { + UnicodeUnescaper uu = new UnicodeUnescaper(); + + String input = "\\u+0047"; + try { + String result = uu.translate(input); + fail("Default behaviour should not parse u+"); + } catch(IllegalArgumentException iae) { + // expected + } + + uu.setEscapingPlus(true); + assertEquals("Failed to unescape unicode characters with 'u+' notation", "G", uu.translate(input)); + } + public void testUuuuu() throws IOException { UnicodeUnescaper uu = new UnicodeUnescaper();