/*
*******************************************************************************
- * Copyright (C) 2003-2010, International Business Machines Corporation and *
+ * Copyright (C) 2003-2011, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
public static StringBuffer convertToUnicode(UCharacterIterator iter, int options)
throws StringPrepParseException{
- char[] caseFlags = null;
-
- //get the options
- boolean useSTD3ASCIIRules = ((options & USE_STD3_RULES) != 0);
-
// the source contains all ascii codepoints
- boolean srcIsASCII = true;
- // assume the source contains all LDH codepoints
- boolean srcIsLDH = true;
-
- int failPos = -1;
+ boolean srcIsASCII = true;
+
int ch;
int saveIndex = iter.getIndex();
- // step 1: find out if all the codepoints in src are ASCII
- while((ch=iter.next())!= UCharacterIterator.DONE){
- if(ch>0x7F){
+ // step 1: find out if all the codepoints in src are ASCII
+ while ((ch = iter.next()) != UCharacterIterator.DONE) {
+ if (ch > 0x7F) {
srcIsASCII = false;
- }else if(isLDHChar(ch)==false){
- failPos = iter.getIndex();
- srcIsLDH = false;
+ break;
}
}
- StringBuffer processOut;
-
- if(srcIsASCII == false){
- // step 2: process the string
- iter.setIndex(saveIndex);
- processOut = transform.prepare(iter,options);
- }else{
- //just point to source
- processOut = new StringBuffer(iter.getText());
- }
- // TODO:
- // The RFC states that
+ // The RFC states that
// <quote>
// ToUnicode never fails. If any step fails, then the original input
// is returned immediately in that step.
// </quote>
-
- //step 3: verify ACE Prefix
- if(startsWithPrefix(processOut)){
+ do {
+ StringBuffer processOut;
+ if (srcIsASCII == false) {
+ // step 2: process the string
+ iter.setIndex(saveIndex);
+ try {
+ processOut = transform.prepare(iter, options);
+ } catch (StringPrepParseException e) {
+ break;
+ }
+ } else {
+ // just point to source
+ processOut = new StringBuffer(iter.getText());
+ }
- //step 4: Remove the ACE Prefix
- String temp = processOut.substring(ACE_PREFIX_LENGTH,processOut.length());
+ // step 3: verify ACE Prefix
+ if (startsWithPrefix(processOut)) {
- //step 5: Decode using punycode
- StringBuffer decodeOut = PunycodeReference.decode(new StringBuffer(temp),caseFlags);
-
- //step 6:Apply toASCII
- StringBuffer toASCIIOut = convertToASCII(decodeOut, options);
+ // step 4: Remove the ACE Prefix
+ String temp = processOut.substring(ACE_PREFIX_LENGTH, processOut.length());
- //step 7: verify
- if(compareCaseInsensitiveASCII(processOut, toASCIIOut) !=0){
- throw new StringPrepParseException("The verification step prescribed by the RFC 3491 failed",
- StringPrepParseException.VERIFICATION_ERROR);
- }
+ // step 5: Decode using punycode
+ StringBuffer decodeOut = null;
+ try {
+ decodeOut = PunycodeReference.decode(new StringBuffer(temp), null);
+ } catch (StringPrepParseException e) {
+ break;
+ }
- //step 8: return output of step 5
- return decodeOut;
-
- }else{
- // verify that STD3 ASCII rules are satisfied
- if(useSTD3ASCIIRules == true){
- if( srcIsLDH == false /* source contains some non-LDH characters */
- || processOut.charAt(0) == HYPHEN
- || processOut.charAt(processOut.length()-1) == HYPHEN){
-
- if(srcIsLDH==false){
- throw new StringPrepParseException("The input does not conform to the STD 3 ASCII rules",
- StringPrepParseException.STD3_ASCII_RULES_ERROR,processOut.toString(),
- (failPos>0) ? (failPos-1) : failPos);
- }else if(processOut.charAt(0) == HYPHEN){
- throw new StringPrepParseException("The input does not conform to the STD 3 ASCII rules",
- StringPrepParseException.STD3_ASCII_RULES_ERROR,
- processOut.toString(),0);
-
- }else{
- throw new StringPrepParseException("The input does not conform to the STD 3 ASCII rules",
- StringPrepParseException.STD3_ASCII_RULES_ERROR,
- processOut.toString(),
- processOut.length());
-
- }
- }
+ // step 6:Apply toASCII
+ StringBuffer toASCIIOut = convertToASCII(decodeOut, options);
+
+ // step 7: verify
+ if (compareCaseInsensitiveASCII(processOut, toASCIIOut) != 0) {
+ break;
+ }
+ // step 8: return output of step 5
+ return decodeOut;
}
- // just return the source
- return new StringBuffer(iter.getText());
- }
+ } while (false);
+
+ return new StringBuffer(iter.getText());
}
+
public static StringBuffer convertIDNToUnicode(UCharacterIterator iter, int options)
throws StringPrepParseException{
return convertIDNToUnicode(iter.getText(), options);
/*
*******************************************************************************
- * Copyright (C) 2003-2006, International Business Machines Corporation and *
+ * Copyright (C) 2003-2011, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
},
"www.-abcde.com",
new StringPrepParseException("",StringPrepParseException.STD3_ASCII_RULES_ERROR),
- true, true, false
+ true, false /* ToUnicode preserves casing for this case */, false
),
new ErrorCase( new char[] {
0x0077, 0x0077, 0x0077, 0x002e, // www.
},
"www.abcde-.com",
new StringPrepParseException("",StringPrepParseException.STD3_ASCII_RULES_ERROR),
- true, true, false
+ true, false /* ToUnicode preserves casing for this case */, false
),
new ErrorCase( new char[]{
0x0077, 0x0077, 0x0077, 0x002e, // www.
},
"www.abcde@.com",
new StringPrepParseException("",StringPrepParseException.STD3_ASCII_RULES_ERROR),
- true, true, false
+ true, false /* ToUnicode preserves casing for this case */, false
),
new ErrorCase( new char[]{
0x0077, 0x0077, 0x0077, 0x002e, // www.
/*
*******************************************************************************
- * Copyright (C) 2003-2010, International Business Machines Corporation and *
+ * Copyright (C) 2003-2011, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
}else{
doTestIDNToASCII(new String(errCase.unicode),errCase.ascii,IDNA.USE_STD3_RULES,errCase.expected);
}
-
//TestToUnicode
- if(false && errCase.testToUnicode==true){
+ if(errCase.testToUnicode==true){
if(errCase.useSTD3ASCIIRules!=true){
// Test IDNToUnicode
doTestIDNToUnicode(errCase.ascii,new String(errCase.unicode),IDNA.DEFAULT,errCase.expected);
doTestCompareReferenceImpl(source);
doTestToASCII(source.toString(),expected.toString(), IDNA.DEFAULT, null);
}
- private void doTestCompareReferenceImpl(StringBuffer src) throws Exception{
-
- StringBuffer label = src;
- StringPrepParseException expected = null;
- StringBuffer ascii = null;
- int options = IDNA.DEFAULT;
- logln("Comparing idnaref_toASCII with uidna_toASCII for input: " + prettify(label));
- try{
- ascii = IDNAReference.convertToASCII(label, options);
- }catch( StringPrepParseException e){
- expected = e;
- if(e.equals(unassignedException)){
- options = IDNA.ALLOW_UNASSIGNED;
- expected = null;
- try{
- ascii = IDNAReference.convertToASCII(label, options);
- }catch( StringPrepParseException ex){
- expected = ex;
- }
- }
+ private StringBuffer _doTestCompareReferenceImpl(StringBuffer src, boolean toASCII, int options) {
+ String refIDNAName = toASCII ? "IDNAReference.convertToASCII" : "IDNAReference.convertToUnicode";
+ String uIDNAName = toASCII ? "IDNA.convertToASCII" : "IDNA.convertToUnicode";
+
+ logln("Comparing " + refIDNAName + " with " + uIDNAName + " for input: "
+ + prettify(src) + " with options: " + options);
+
+ StringBuffer exp = null;
+ int expStatus = -1;
+ try {
+ exp = toASCII ? IDNAReference.convertToASCII(src, options) : IDNAReference.convertToUnicode(src, options);
+ } catch (StringPrepParseException e) {
+ expStatus = e.getError();
}
-
- doTestToASCII(label.toString(),
- (ascii == null) ? null : ascii.toString(),
- options,
- expected);
- logln("Comparing idnaref_toUnicode with uidna_toUnicode for input: " + prettify(label));
- StringBuffer uni =null;
-
- if(expected == null){
- options = IDNA.DEFAULT;
- try{
- uni = IDNAReference.convertToUnicode(ascii, options);
- }catch( StringPrepParseException e ){
- expected = e;
- if(expected.equals(unassignedException)){
- options = IDNA.ALLOW_UNASSIGNED;
- expected = null;
- try{
- uni = IDNAReference.convertToUnicode(ascii, options);
- }catch(StringPrepParseException ex){
- expected = ex;
- }
+ StringBuffer got = null;
+ int gotStatus = -1;
+ try {
+ got = toASCII ? IDNA.convertToASCII(src, options) : IDNA.convertToUnicode(src, options);
+ } catch (StringPrepParseException e) {
+ gotStatus = e.getError();
+ }
+
+ if (expStatus != gotStatus) {
+ errln("Did not get the expected status while comparing " + refIDNAName + " with " + uIDNAName
+ + " Expected: " + expStatus
+ + " Got: " + gotStatus
+ + " for Source: "+ prettify(src)
+ + " Options: " + options);
+ } else {
+ // now we know that both implementation yielded same status
+ if (gotStatus == -1) {
+ // compare the outputs
+ if (!got.toString().equals(exp.toString())) {
+ errln("Did not get the expected output while comparing " + refIDNAName + " with " + uIDNAName
+ + " Expected: " + exp
+ + " Got: " + got
+ + " for Source: "+ prettify(src)
+ + " Options: " + options);
}
+ } else {
+ logln("Got the same error while comparing " + refIDNAName + " with " + uIDNAName
+ +" for input: " + prettify(src) + " with options: " + options);
}
- doTestToUnicode(ascii.toString(),
- (uni==null)? null : uni.toString(),
- options,
- expected);
}
+ return exp;
}
- public void TestCompareRefImpl() throws Exception{
-
- for(int i = 0x40000 ; i< 0x10ffff; i++){
+
+ private void doTestCompareReferenceImpl(StringBuffer src) throws Exception{
+ // test toASCII
+ src.setLength(0);
+ src.append("[");
+ StringBuffer asciiLabel = _doTestCompareReferenceImpl(src, true, IDNA.ALLOW_UNASSIGNED);
+ _doTestCompareReferenceImpl(src, true, IDNA.DEFAULT);
+ _doTestCompareReferenceImpl(src, true, IDNA.USE_STD3_RULES);
+ _doTestCompareReferenceImpl(src, true, IDNA.USE_STD3_RULES | IDNA.ALLOW_UNASSIGNED);
+
+ if (asciiLabel != null) {
+ // test toUnicode
+ _doTestCompareReferenceImpl(src, false, IDNA.ALLOW_UNASSIGNED);
+ _doTestCompareReferenceImpl(src, false, IDNA.DEFAULT);
+ _doTestCompareReferenceImpl(src, false, IDNA.USE_STD3_RULES);
+ _doTestCompareReferenceImpl(src, false, IDNA.USE_STD3_RULES | IDNA.ALLOW_UNASSIGNED);
+ }
+ }
+
+ public void TestCompareRefImpl() throws Exception {
+ for (int i = 65; i < 0x10FFFF; i++) {
StringBuffer src = new StringBuffer();
-
- if(isQuick()==true && i> 0x1FFFF){
+ if (isQuick() == true && i > 0x0FFF) {
return;
}
- if(i >= 0x30000 && i<=0xf0000){
- i+=0xB0000;
+ if (i == 0x30000) {
+ // jump to E0000, no characters assigned in plain 3 to plain 13 as of Unicode 6.0
+ i = 0xE0000;
}
- UTF16.append(src,i);
+ UTF16.append(src, i);
doTestCompareReferenceImpl(src);
-
- }
+ }
}
+
public void TestJB4490(){
String[] in = new String[]{
"\u00F5\u00dE\u00dF\u00dD",