Search in sources :

Example 21 with StringPrepParseException

use of android.icu.text.StringPrepParseException 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 22 with StringPrepParseException

use of android.icu.text.StringPrepParseException 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)

Example 23 with StringPrepParseException

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

the class TestIDNA method doTestToUnicode.

private void doTestToUnicode(String src, String expected, int options, Object expectedException) throws Exception {
    StringBuffer inBuf = new StringBuffer(src);
    UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    try {
        StringBuffer out = IDNA.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 = IDNA.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 = IDNA.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 24 with StringPrepParseException

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

the class NamePrepTransform method map.

private String map(String src, int options) throws StringPrepParseException {
    // map
    boolean allowUnassigned = ((options & ALLOW_UNASSIGNED) > 0);
    // disable test
    String caseMapOut = mapTransform.transliterate(src);
    UCharacterIterator iter = UCharacterIterator.getInstance(caseMapOut);
    int ch;
    while ((ch = iter.nextCodePoint()) != UCharacterIterator.DONE) {
        if (transform.unassignedSet.contains(ch) == true && allowUnassigned == false) {
            throw new StringPrepParseException("An unassigned code point was found in the input", StringPrepParseException.UNASSIGNED_ERROR);
        }
    }
    return caseMapOut;
}
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