On 18/10/2009, bay...@apache.org <bay...@apache.org> wrote: > Author: bayard > Date: Sun Oct 18 07:25:59 2009 > New Revision: 826370 > > URL: http://svn.apache.org/viewvc?rev=826370&view=rev > Log: > Implementing an option to UnicodeUnescaper in which the syntax '\u+0047' is > supported. By default it remains unsupported to match Java's method of > parsing. Request in LANG-507 > > Modified: > > commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/UnicodeUnescaper.java > > commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/translate/UnicodeUnescaperTest.java > > Modified: > commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/UnicodeUnescaper.java > URL: > http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/UnicodeUnescaper.java?rev=826370&r1=826369&r2=826370&view=diff > > ============================================================================== > --- > commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/UnicodeUnescaper.java > (original) > +++ > commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/translate/UnicodeUnescaper.java > Sun Oct 18 07:25:59 2009 > @@ -26,6 +26,15 @@ > */ > public class UnicodeUnescaper extends CharSequenceTranslator { > > + private boolean escapingPlus = false; > + > + public void setEscapingPlus(boolean b) { > + this.escapingPlus = b; > + } > + public boolean isEscapingPlus() { > + return this.escapingPlus; > + } > +
Would it not be better to make the class immutable (and thread-safe) by providing the setting as a constructor parameter, rather than as set/get methods? > /** > * {...@inheritdoc} > */ > @@ -39,6 +48,13 @@ > 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 @@ > 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 { > > Modified: > commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/translate/UnicodeUnescaperTest.java > URL: > http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/translate/UnicodeUnescaperTest.java?rev=826370&r1=826369&r2=826370&view=diff > > ============================================================================== > --- > commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/translate/UnicodeUnescaperTest.java > (original) > +++ > commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/translate/UnicodeUnescaperTest.java > Sun Oct 18 07:25:59 2009 > @@ -27,6 +27,22 @@ > */ > 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(); > > > > --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org