Search in sources :

Example 6 with StringPrepParseException

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

the class IDNA2003 method convertIDNToASCII.

public static StringBuffer convertIDNToASCII(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);
        // make sure this is not a root label separator.
        if (!(label.length() == 0 && sepIndex == srcArr.length)) {
            UCharacterIterator iter = UCharacterIterator.getInstance(label);
            result.append(convertToASCII(iter, options));
        }
        if (sepIndex == srcArr.length) {
            break;
        }
        // increment the sepIndex to skip past the separator
        sepIndex++;
        oldSepIndex = sepIndex;
        result.append((char) FULL_STOP);
    }
    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 7 with StringPrepParseException

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

the class TestIDNA method doTestIDNToASCII.

private void doTestIDNToASCII(String src, String expected, int options, Object expectedException) throws Exception {
    StringBuffer inBuf = new StringBuffer(src);
    UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    try {
        StringBuffer out = IDNA.convertIDNToASCII(src, options);
        if (expected != null && out != null && !out.toString().equals(expected)) {
            errln("convertToIDNASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToIDNASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !ex.equals(expectedException)) {
            errln("convertToIDNASCII did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNA.convertIDNToASCII(inBuf, options);
        if (expected != null && out != null && !out.toString().equals(expected)) {
            errln("convertToIDNASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToIDNASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !ex.equals(expectedException)) {
            errln("convertToIDNASCII did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNA.convertIDNToASCII(inIter, options);
        if (expected != null && out != null && !out.toString().equals(expected)) {
            errln("convertIDNToASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertIDNToASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !ex.equals(expectedException)) {
            errln("convertIDNToASCII did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) UCharacterIterator(android.icu.text.UCharacterIterator)

Example 8 with StringPrepParseException

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

the class TestIDNA method TestNamePrepConformance.

@Test
public void TestNamePrepConformance() throws Exception {
    StringPrep namePrep = StringPrep.getInstance(StringPrep.RFC3491_NAMEPREP);
    for (int i = 0; i < TestData.conformanceTestCases.length; i++) {
        TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];
        UCharacterIterator iter = UCharacterIterator.getInstance(testCase.input);
        try {
            StringBuffer output = namePrep.prepare(iter, StringPrep.DEFAULT);
            if (testCase.output != null && output != null && !testCase.output.equals(output.toString())) {
                errln("Did not get the expected output. Expected: " + prettify(testCase.output) + " Got: " + prettify(output));
            }
            if (testCase.expected != null && !unassignedException.equals(testCase.expected)) {
                errln("Did not get the expected exception. The operation succeeded!");
            }
        } catch (StringPrepParseException ex) {
            if (testCase.expected == null || !ex.equals(testCase.expected)) {
                errln("Did not get the expected exception for source: " + testCase.input + " Got:  " + ex.toString());
            }
        }
        try {
            iter.setToStart();
            StringBuffer output = namePrep.prepare(iter, StringPrep.ALLOW_UNASSIGNED);
            if (testCase.output != null && output != null && !testCase.output.equals(output.toString())) {
                errln("Did not get the expected output. Expected: " + prettify(testCase.output) + " Got: " + prettify(output));
            }
            if (testCase.expected != null && !unassignedException.equals(testCase.expected)) {
                errln("Did not get the expected exception. The operation succeeded!");
            }
        } catch (StringPrepParseException ex) {
            if (testCase.expected == null || !ex.equals(testCase.expected)) {
                errln("Did not get the expected exception for source: " + testCase.input + " Got:  " + ex.toString());
            }
        }
    }
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) StringPrep(android.icu.text.StringPrep) UCharacterIterator(android.icu.text.UCharacterIterator) Test(org.junit.Test)

Example 9 with StringPrepParseException

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

the class TestIDNA method doTestToASCII.

private void doTestToASCII(String src, String expected, int options, Object expectedException) throws Exception {
    StringBuffer inBuf = new StringBuffer(src);
    UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    try {
        StringBuffer out = IDNA.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 + "\n Got:  " + ex.toString() + "\n Expected: " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNA.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 = IDNA.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)

Example 10 with StringPrepParseException

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

the class IDNA2003 method convertToASCII.

public static StringBuffer convertToASCII(UCharacterIterator src, int options) throws StringPrepParseException {
    boolean[] caseFlags = null;
    // the source contains all ascii codepoints
    boolean srcIsASCII = true;
    // assume the source contains all LDH codepoints
    boolean srcIsLDH = true;
    // get the options
    boolean useSTD3ASCIIRules = ((options & IDNA.USE_STD3_RULES) != 0);
    int ch;
    // step 1
    while ((ch = src.next()) != UCharacterIterator.DONE) {
        if (ch > 0x7f) {
            srcIsASCII = false;
        }
    }
    int failPos = -1;
    src.setToStart();
    StringBuffer processOut = null;
    // step 2 is performed only if the source contains non ASCII
    if (!srcIsASCII) {
        // step 2
        processOut = namePrep.prepare(src, options);
    } else {
        processOut = new StringBuffer(src.getText());
    }
    int poLen = processOut.length();
    if (poLen == 0) {
        throw new StringPrepParseException("Found zero length lable after NamePrep.", StringPrepParseException.ZERO_LENGTH_LABEL);
    }
    StringBuffer dest = new StringBuffer();
    // reset the variable to verify if output of prepare is ASCII or not
    srcIsASCII = true;
    // step 3 & 4
    for (int j = 0; j < poLen; j++) {
        ch = processOut.charAt(j);
        if (ch > 0x7F) {
            srcIsASCII = false;
        } else if (isLDHChar(ch) == false) {
            // here we do not assemble surrogates
            // since we know that LDH code points
            // are in the ASCII range only
            srcIsLDH = false;
            failPos = j;
        }
    }
    if (useSTD3ASCIIRules == true) {
        // verify 3a and 3b
        if (srcIsLDH == false || /* source contains some non-LDH characters */
        processOut.charAt(0) == HYPHEN || processOut.charAt(processOut.length() - 1) == HYPHEN) {
            /* populate the parseError struct */
            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(), (poLen > 0) ? poLen - 1 : poLen);
            }
        }
    }
    if (srcIsASCII) {
        dest = processOut;
    } else {
        // step 5 : verify the sequence does not begin with ACE prefix
        if (!startsWithPrefix(processOut)) {
            // step 6: encode the sequence with punycode
            caseFlags = new boolean[poLen];
            StringBuilder punyout = Punycode.encode(processOut, caseFlags);
            // convert all codepoints to lower case ASCII
            StringBuffer lowerOut = toASCIILower(punyout);
            // Step 7: prepend the ACE prefix
            dest.append(ACE_PREFIX, 0, ACE_PREFIX.length);
            // Step 6: copy the contents in b2 into dest
            dest.append(lowerOut);
        } else {
            throw new StringPrepParseException("The input does not start with the ACE Prefix.", StringPrepParseException.ACE_PREFIX_ERROR, processOut.toString(), 0);
        }
    }
    if (dest.length() > MAX_LABEL_LENGTH) {
        throw new StringPrepParseException("The labels in the input are too long. Length > 63.", StringPrepParseException.LABEL_TOO_LONG_ERROR, dest.toString(), 0);
    }
    return dest;
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException)

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