Search in sources :

Example 21 with UCharacterIterator

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

the class TestIDNARef method doTestIDNToUnicode.

private void doTestIDNToUnicode(String src, String expected, int options, Object expectedException) throws Exception {
    if (!IDNAReference.isReady()) {
        logln("Transliterator is not available on this environment.  Skipping doTestIDNToUnicode.");
        return;
    }
    StringBuffer inBuf = new StringBuffer(src);
    UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    try {
        StringBuffer out = IDNAReference.convertIDNToUnicode(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 || !expectedException.equals(ex)) {
            errln("convertToUnicode did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNAReference.convertIDNToUnicode(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 || !expectedException.equals(ex)) {
            errln("convertToUnicode did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNAReference.convertIDNToUnicode(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 || !expectedException.equals(ex)) {
            errln("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 22 with UCharacterIterator

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

the class TestIDNARef method doTestIDNToASCII.

private void doTestIDNToASCII(String src, String expected, int options, Object expectedException) throws Exception {
    if (!IDNAReference.isReady()) {
        logln("Transliterator is not available on this environment.  Skipping doTestIDNToASCII.");
        return;
    }
    StringBuffer inBuf = new StringBuffer(src);
    UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    try {
        StringBuffer out = IDNAReference.convertIDNToASCII(src, options);
        if (expected != null && out != null && !out.toString().equals(expected)) {
            errln("convertToIDNAReferenceASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToIDNAReferenceASCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !ex.equals(expectedException)) {
            errln("convertToIDNAReferenceASCII did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNAReference.convertIDNtoASCII(inBuf, options);
        if (expected != null && out != null && !out.toString().equals(expected)) {
            errln("convertToIDNAReferenceASCII did not return expected result with options : " + options + " Expected: " + expected + " Got: " + out);
        }
        if (expectedException != null && !unassignedException.equals(expectedException)) {
            errln("convertToIDNAReferenceSCII did not get the expected exception. The operation succeeded!");
        }
    } catch (StringPrepParseException ex) {
        if (expectedException == null || !ex.equals(expectedException)) {
            errln("convertToIDNAReferenceSCII did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNAReference.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 23 with UCharacterIterator

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

the class NFS4StringPrep method mixed_prepare.

public static byte[] mixed_prepare(byte[] src) throws IOException, StringPrepParseException, UnsupportedEncodingException {
    String s = new String(src, "UTF-8");
    int index = s.indexOf(AT_SIGN);
    StringBuffer out = new StringBuffer();
    if (index > -1) {
        /* special prefixes must not be followed by suffixes! */
        String prefixString = s.substring(0, index);
        int i = findStringIndex(special_prefixes, prefixString);
        String suffixString = s.substring(index + 1, s.length());
        if (i > -1 && !suffixString.equals("")) {
            throw new StringPrepParseException("Suffix following a special index", StringPrepParseException.INVALID_CHAR_FOUND);
        }
        UCharacterIterator prefix = UCharacterIterator.getInstance(prefixString);
        UCharacterIterator suffix = UCharacterIterator.getInstance(suffixString);
        out.append(prep.nfsmxp.prepare(prefix, StringPrep.DEFAULT));
        // add the delimiter
        out.append(AT_SIGN);
        out.append(prep.nfsmxs.prepare(suffix, StringPrep.DEFAULT));
    } else {
        UCharacterIterator iter = UCharacterIterator.getInstance(s);
        out.append(prep.nfsmxp.prepare(iter, StringPrep.DEFAULT));
    }
    return out.toString().getBytes("UTF-8");
}
Also used : StringPrepParseException(android.icu.text.StringPrepParseException) UCharacterIterator(android.icu.text.UCharacterIterator)

Example 24 with UCharacterIterator

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

the class PunycodeReference method encode.

public static final StringBuffer encode(StringBuffer input, char[] case_flags) throws StringPrepParseException {
    int[] in = new int[input.length()];
    int inLen = 0;
    int ch;
    StringBuffer result = new StringBuffer();
    UCharacterIterator iter = UCharacterIterator.getInstance(input);
    while ((ch = iter.nextCodePoint()) != UCharacterIterator.DONE) {
        in[inLen++] = ch;
    }
    int[] outLen = new int[1];
    outLen[0] = input.length() * 4;
    char[] output = new char[outLen[0]];
    int rc = punycode_success;
    for (; ; ) {
        rc = encode(inLen, in, case_flags, outLen, output);
        if (rc == punycode_big_output) {
            outLen[0] = outLen[0] * 4;
            output = new char[outLen[0]];
            // continue to convert
            continue;
        }
        break;
    }
    if (rc == punycode_success) {
        return result.append(output, 0, outLen[0]);
    }
    getException(rc);
    return result;
}
Also used : UCharacterIterator(android.icu.text.UCharacterIterator)

Example 25 with UCharacterIterator

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

the class TestIDNA method doTestIDNToUnicode.

private void doTestIDNToUnicode(String src, String expected, int options, Object expectedException) throws Exception {
    StringBuffer inBuf = new StringBuffer(src);
    UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    try {
        StringBuffer out = IDNA.convertIDNToUnicode(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 || !expectedException.equals(ex)) {
            errln("convertToUnicode did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNA.convertIDNToUnicode(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 || !expectedException.equals(ex)) {
            errln("convertToUnicode did not get the expected exception for source: " + src + " Got:  " + ex.toString());
        }
    }
    try {
        StringBuffer out = IDNA.convertIDNToUnicode(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 || !expectedException.equals(ex)) {
            errln("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

UCharacterIterator (android.icu.text.UCharacterIterator)33 StringPrepParseException (android.icu.text.StringPrepParseException)17 Test (org.junit.Test)15 StringCharacterIterator (java.text.StringCharacterIterator)5 CharacterIterator (java.text.CharacterIterator)4 Normalizer (android.icu.text.Normalizer)3 ReplaceableString (android.icu.text.ReplaceableString)3 CollationElementIterator (android.icu.text.CollationElementIterator)2 RuleBasedCollator (android.icu.text.RuleBasedCollator)2 CollationData (android.icu.impl.coll.CollationData)1 FCDIterCollationIterator (android.icu.impl.coll.FCDIterCollationIterator)1 FCDUTF16CollationIterator (android.icu.impl.coll.FCDUTF16CollationIterator)1 Collator (android.icu.text.Collator)1 StringPrep (android.icu.text.StringPrep)1