Search in sources :

Example 11 with StringPrepParseException

use of android.icu.text.StringPrepParseException in project j2objc by google.

the class IDNA2003 method convertIDNToUnicode.

public static StringBuffer convertIDNToUnicode(String src, int options) throws StringPrepParseException {
    char[] srcArr = src.toCharArray();
    StringBuffer result = new StringBuffer();
    int sepIndex = 0;
    int oldSepIndex = 0;
    for (; ; ) {
        sepIndex = getSeparatorIndex(srcArr, sepIndex, srcArr.length);
        String label = new String(srcArr, oldSepIndex, sepIndex - oldSepIndex);
        if (label.length() == 0 && sepIndex != srcArr.length) {
            throw new StringPrepParseException("Found zero length lable after NamePrep.", StringPrepParseException.ZERO_LENGTH_LABEL);
        }
        UCharacterIterator iter = UCharacterIterator.getInstance(label);
        result.append(convertToUnicode(iter, options));
        if (sepIndex == srcArr.length) {
            break;
        }
        // Unlike the ToASCII operation we don't normalize the label separators
        result.append(srcArr[sepIndex]);
        // increment the sepIndex to skip past the separator
        sepIndex++;
        oldSepIndex = sepIndex;
    }
    if (result.length() > MAX_DOMAIN_NAME_LENGTH) {
        throw new StringPrepParseException("The output exceed the max allowed length.", StringPrepParseException.DOMAIN_NAME_TOO_LONG_ERROR);
    }
    return result;
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) UCharacterIterator(android.icu.text.UCharacterIterator)

Example 12 with StringPrepParseException

use of android.icu.text.StringPrepParseException in project j2objc by google.

the class TestStringPrepProfiles method TestProfiles.

@Test
public void TestProfiles() {
    String profileName = null;
    StringPrep sprep = null;
    String result = null;
    String src = null;
    String expected = null;
    for (int i = 0; i < testCases.length; i++) {
        for (int j = 0; j < testCases[i].length; j++) {
            if (j == 0) {
                profileName = testCases[i][j];
                sprep = StringPrep.getInstance(getOptionFromProfileName(profileName));
            } else {
                src = testCases[i][j];
                expected = testCases[i][++j];
                try {
                    result = sprep.prepare(src, StringPrep.ALLOW_UNASSIGNED);
                    if (src.startsWith("FAIL")) {
                        errln("Failed: Expected error for Test[" + i + "] Profile: " + profileName);
                    } else if (!result.equals(expected)) {
                        errln("Failed: Test[" + i + "] Result string does not match expected string for StringPrep test for profile: " + profileName);
                    }
                } catch (StringPrepParseException ex) {
                    if (!src.startsWith("FAIL")) {
                        errln("Failed: Test[" + i + "] StringPrep profile " + profileName + " got error: " + ex);
                    }
                }
            }
        }
    }
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) StringPrep(android.icu.text.StringPrep) Test(org.junit.Test)

Example 13 with StringPrepParseException

use of android.icu.text.StringPrepParseException in project j2objc by google.

the class IDNAConformanceTest method TestConformance.

@Test
public void TestConformance() {
    TreeMap inputData = null;
    try {
        inputData = ReadInput.getInputData();
    } catch (UnsupportedEncodingException e) {
        errln(e.getMessage());
        return;
    } catch (IOException e) {
        errln(e.getMessage());
        return;
    }
    Set keyMap = inputData.keySet();
    for (Iterator iter = keyMap.iterator(); iter.hasNext(); ) {
        Long element = (Long) iter.next();
        HashMap tempHash = (HashMap) inputData.get(element);
        // get all attributes from input data
        String passfail = (String) tempHash.get("passfail");
        String desc = (String) tempHash.get("desc");
        String type = (String) tempHash.get("type");
        String namebase = (String) tempHash.get("namebase");
        String nameutf8 = (String) tempHash.get("nameutf8");
        String namezone = (String) tempHash.get("namezone");
        String failzone1 = (String) tempHash.get("failzone1");
        String failzone2 = (String) tempHash.get("failzone2");
        // they maybe includes <*> style unicode
        namebase = stringReplace(namebase);
        namezone = stringReplace(namezone);
        String result = null;
        boolean failed = false;
        if ("toascii".equals(tempHash.get("type"))) {
            // get the result
            try {
                // includes UseSTD3ASCIIRules, we will set it.
                if (desc.toLowerCase().indexOf("UseSTD3ASCIIRules".toLowerCase()) == -1) {
                    result = IDNA.convertIDNToASCII(namebase, IDNA.ALLOW_UNASSIGNED).toString();
                } else {
                    result = IDNA.convertIDNToASCII(namebase, IDNA.USE_STD3_RULES).toString();
                }
            } catch (StringPrepParseException e2) {
                // errln(e2.getMessage());
                failed = true;
            }
            if ("pass".equals(passfail)) {
                if (!namezone.equals(result)) {
                    printInfo(desc, namebase, nameutf8, namezone, failzone1, failzone2, result, type, passfail);
                    errln("\t pass fail standard is pass, but failed");
                } else {
                    printInfo(desc, namebase, nameutf8, namezone, failzone1, failzone2, result, type, passfail);
                    logln("\tpassed");
                }
            }
            if ("fail".equals(passfail)) {
                if (failed) {
                    printInfo(desc, namebase, nameutf8, namezone, failzone1, failzone2, result, type, passfail);
                    logln("passed");
                } else {
                    printInfo(desc, namebase, nameutf8, namezone, failzone1, failzone2, result, type, passfail);
                    errln("\t pass fail standard is fail, but no exception thrown out");
                }
            }
        } else if ("tounicode".equals(tempHash.get("type"))) {
            try {
                // includes UseSTD3ASCIIRules, we will set it.
                if (desc.toLowerCase().indexOf("UseSTD3ASCIIRules".toLowerCase()) == -1) {
                    result = IDNA.convertIDNToUnicode(namebase, IDNA.ALLOW_UNASSIGNED).toString();
                } else {
                    result = IDNA.convertIDNToUnicode(namebase, IDNA.USE_STD3_RULES).toString();
                }
            } catch (StringPrepParseException e2) {
                // errln(e2.getMessage());
                failed = true;
            }
            if ("pass".equals(passfail)) {
                if (!namezone.equals(result)) {
                    printInfo(desc, namebase, nameutf8, namezone, failzone1, failzone2, result, type, passfail);
                    errln("\t Did not get the expected result. Expected: " + prettify(namezone) + " Got: " + prettify(result));
                } else {
                    printInfo(desc, namebase, nameutf8, namezone, failzone1, failzone2, result, type, passfail);
                    logln("\tpassed");
                }
            }
            if ("fail".equals(passfail)) {
                if (failed || namebase.equals(result)) {
                    printInfo(desc, namebase, nameutf8, namezone, failzone1, failzone2, result, type, passfail);
                    logln("\tpassed");
                } else {
                    printInfo(desc, namebase, nameutf8, namezone, failzone1, failzone2, result, type, passfail);
                    errln("\t pass fail standard is fail, but no exception thrown out");
                }
            }
        } else {
            continue;
        }
    }
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) Set(java.util.Set) HashMap(java.util.HashMap) Iterator(java.util.Iterator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 14 with StringPrepParseException

use of android.icu.text.StringPrepParseException in project j2objc by google.

the class TestIDNARef method doTestToUnicode.

private void doTestToUnicode(String src, String expected, int options, Object expectedException) throws Exception {
    if (!IDNAReference.isReady()) {
        logln("Transliterator is not available on this environment.  Skipping doTestToUnicode.");
        return;
    }
    StringBuffer inBuf = new StringBuffer(src);
    UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    try {
        StringBuffer out = IDNAReference.convertToUnicode(src, options);
        if (expected != null && out != null && !out.toString().equals(expected)) {
            errln("convertToUnicode did not return expected result with options : " + options + " Expected: " + prettify(expected) + " Got: " + prettify(out));
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToUnicode did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !ex.equals(expectedException)) {
            errln("convertToUnicode did not get the expected exception for source: " + prettify(src) + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNAReference.convertToUnicode(inBuf, options);
        if (expected != null && out != null && !out.toString().equals(expected)) {
            errln("convertToUnicode did not return expected result with options : " + options + " Expected: " + prettify(expected) + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToUnicode did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !ex.equals(expectedException)) {
            errln("convertToUnicode did not get the expected exception for source: " + prettify(src) + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNAReference.convertToUnicode(inIter, options);
        if (expected != null && out != null && !out.toString().equals(expected)) {
            errln("convertToUnicode did not return expected result with options : " + options + " Expected: " + prettify(expected) + " Got: " + prettify(out));
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("Did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !ex.equals(expectedException)) {
            errln("Did not get the expected exception for source: " + prettify(src) + " Got:  " + ex.toString());
        }
    }
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) UCharacterIterator(android.icu.text.UCharacterIterator)

Example 15 with StringPrepParseException

use of android.icu.text.StringPrepParseException in project j2objc by google.

the class TestIDNARef method doTestToASCII.

private void doTestToASCII(String src, String expected, int options, Object expectedException) throws Exception {
    if (!IDNAReference.isReady()) {
        logln("Transliterator is not available on this environment.  Skipping doTestToASCII.");
        return;
    }
    StringBuffer inBuf = new StringBuffer(src);
    UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    try {
        StringBuffer out = IDNAReference.convertToASCII(src, options);
        if (!unassignedException.equals(expectedException) && expected != null && out != null && expected != null && out != null && !out.toString().equals(expected.toLowerCase())) {
            errln("convertToASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !expectedException.equals(ex)) {
            errln("convertToASCII did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNAReference.convertToASCII(inBuf, options);
        if (!unassignedException.equals(expectedException) && expected != null && out != null && expected != null && out != null && !out.toString().equals(expected.toLowerCase())) {
            errln("convertToASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !expectedException.equals(ex)) {
            errln("convertToASCII did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNAReference.convertToASCII(inIter, options);
        if (!unassignedException.equals(expectedException) && expected != null && out != null && expected != null && out != null && !out.toString().equals(expected.toLowerCase())) {
            errln("convertToASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !expectedException.equals(ex)) {
            errln("convertToASCII did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) UCharacterIterator(android.icu.text.UCharacterIterator)

Aggregations

StringPrepParseException (android.icu.text.StringPrepParseException)24 UCharacterIterator (android.icu.text.UCharacterIterator)16 Test (org.junit.Test)7 StringPrep (android.icu.text.StringPrep)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Locale (java.util.Locale)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1