com.ibm.icu.text.IDNA Java Examples

The following examples show how to use com.ibm.icu.text.IDNA. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: URLUtils.java    From galimatias with MIT License 6 votes vote down vote up
static String domainToASCII(final String domain, final ErrorHandler errorHandler) throws GalimatiasParseException {
    final IDNA.Info idnaInfo = new IDNA.Info();
    final StringBuilder idnaOutput = new StringBuilder();
    idna.nameToASCII(domain, idnaOutput, idnaInfo);
    processIdnaInfo(errorHandler, idnaInfo, false);
    return idnaOutput.toString();
}
 
Example #2
Source File: Domain.java    From galimatias with MIT License 5 votes vote down vote up
/**
 * Converts the domain to a Unicode representation suitable for human interpretation. It does IDNA conversion.
 *
 * @see Host#toHumanString()
 * @return unicode string
 */
@Override
public String toHumanString() {
    if (unicode) {
        return domain;
    }
    final IDNA.Info idnaInfo = new IDNA.Info();
    final StringBuilder idnaOutput = new StringBuilder();
    IDNA.getUTS46Instance(IDNA.DEFAULT).nameToUnicode(domain, idnaOutput, idnaInfo);
    return idnaOutput.toString();
}
 
Example #3
Source File: URLUtils.java    From galimatias with MIT License 5 votes vote down vote up
static String domainToASCII(final String domain, final ErrorHandler errorHandler) throws GalimatiasParseException {
    final IDNA.Info idnaInfo = new IDNA.Info();
    final StringBuilder idnaOutput = new StringBuilder();
    idna.nameToASCII(domain, idnaOutput, idnaInfo);
    processIdnaInfo(errorHandler, idnaInfo, false);
    return idnaOutput.toString();
}
 
Example #4
Source File: URLUtils.java    From galimatias with MIT License 5 votes vote down vote up
static String domainToUnicode(final String asciiDomain, final ErrorHandler errorHandler) throws GalimatiasParseException {
    final IDNA.Info unicodeIdnaInfo = new IDNA.Info();
    final StringBuilder unicodeIdnaOutput = new StringBuilder();
    idna.nameToUnicode(asciiDomain, unicodeIdnaOutput, unicodeIdnaInfo);
    processIdnaInfo(errorHandler, unicodeIdnaInfo, false);
    return unicodeIdnaOutput.toString();
}
 
Example #5
Source File: Domain.java    From galimatias with MIT License 5 votes vote down vote up
/**
 * Converts the domain to a Unicode representation suitable for human interpretation. It does IDNA conversion.
 *
 * @see Host#toHumanString()
 * @return unicode string
 */
@Override
public String toHumanString() {
    if (unicode) {
        return domain;
    }
    final IDNA.Info idnaInfo = new IDNA.Info();
    final StringBuilder idnaOutput = new StringBuilder();
    IDNA.getUTS46Instance(IDNA.DEFAULT).nameToUnicode(domain, idnaOutput, idnaInfo);
    return idnaOutput.toString();
}
 
Example #6
Source File: URLUtils.java    From galimatias with MIT License 5 votes vote down vote up
static String domainToUnicode(final String asciiDomain, final ErrorHandler errorHandler) throws GalimatiasParseException {
    final IDNA.Info unicodeIdnaInfo = new IDNA.Info();
    final StringBuilder unicodeIdnaOutput = new StringBuilder();
    idna.nameToUnicode(asciiDomain, unicodeIdnaOutput, unicodeIdnaInfo);
    processIdnaInfo(errorHandler, unicodeIdnaInfo, false);
    return unicodeIdnaOutput.toString();
}
 
Example #7
Source File: URLUtils.java    From galimatias with MIT License 4 votes vote down vote up
private static void processIdnaInfo(final ErrorHandler errorHandler,
        final IDNA.Info idnaInfo, final boolean checkHyphens)
        throws GalimatiasParseException {
    for (IDNA.Error error : idnaInfo.getErrors()) {
        String msg;
        switch (error) {
            case BIDI:
                msg = "A label does not meet the IDNA BiDi requirements (for right-to-left characters).";
                break;
            case CONTEXTJ:
                msg = "A label does not meet the IDNA CONTEXTJ requirements.";
                break;
            case CONTEXTO_DIGITS:
                msg = "A label does not meet the IDNA CONTEXTO requirements for digits.";
                break;
            case CONTEXTO_PUNCTUATION:
                msg = "A label does not meet the IDNA CONTEXTO requirements for punctuation characters.";
                break;
            case DISALLOWED:
                msg = "A label or domain name contains disallowed characters.";
                break;
            case DOMAIN_NAME_TOO_LONG:
                msg = "A domain name is longer than 255 bytes in its storage form.";
                break;
            case EMPTY_LABEL:
                msg = "A non-final domain name label (or the whole domain name) is empty.";
                break;
            case HYPHEN_3_4:
                if (!checkHyphens) {
                    return;
                }
                msg = "A label contains hyphen-minus ('-') in the third and fourth positions.";
                break;
            case INVALID_ACE_LABEL:
                msg = "An ACE label does not contain a valid label string.";
                break;
            case LABEL_HAS_DOT:
                msg = "A label contains a dot=full stop.";
                break;
            case LABEL_TOO_LONG:
                msg = "A domain name label is longer than 63 bytes.";
                break;
            case LEADING_COMBINING_MARK:
                msg = "A label starts with a combining mark.";
                break;
            case LEADING_HYPHEN:
                if (!checkHyphens) {
                    return;
                }
                msg = "A label starts with a hyphen-minus ('-').";
                break;
            case PUNYCODE:
                msg = "A label starts with \"xn--\" but does not contain valid Punycode.";
                break;
            case TRAILING_HYPHEN:
                if (!checkHyphens) {
                    return;
                }
                msg = "A label ends with a hyphen-minus ('-').";
                break;
            default:
                msg = "IDNA error.";
                break;
        }
        final GalimatiasParseException exception = new GalimatiasParseException(msg);
        errorHandler.fatalError(exception);
        throw exception;
    }
}
 
Example #8
Source File: URLUtils.java    From galimatias with MIT License 4 votes vote down vote up
private static void processIdnaInfo(final ErrorHandler errorHandler,
        final IDNA.Info idnaInfo, final boolean checkHyphens)
        throws GalimatiasParseException {
    for (IDNA.Error error : idnaInfo.getErrors()) {
        String msg;
        switch (error) {
            case BIDI:
                msg = "A label does not meet the IDNA BiDi requirements (for right-to-left characters).";
                break;
            case CONTEXTJ:
                msg = "A label does not meet the IDNA CONTEXTJ requirements.";
                break;
            case CONTEXTO_DIGITS:
                msg = "A label does not meet the IDNA CONTEXTO requirements for digits.";
                break;
            case CONTEXTO_PUNCTUATION:
                msg = "A label does not meet the IDNA CONTEXTO requirements for punctuation characters.";
                break;
            case DISALLOWED:
                msg = "A label or domain name contains disallowed characters.";
                break;
            case DOMAIN_NAME_TOO_LONG:
                msg = "A domain name is longer than 255 bytes in its storage form.";
                break;
            case EMPTY_LABEL:
                msg = "A non-final domain name label (or the whole domain name) is empty.";
                break;
            case HYPHEN_3_4:
                if (!checkHyphens) {
                    return;
                }
                msg = "A label contains hyphen-minus ('-') in the third and fourth positions.";
                break;
            case INVALID_ACE_LABEL:
                msg = "An ACE label does not contain a valid label string.";
                break;
            case LABEL_HAS_DOT:
                msg = "A label contains a dot=full stop.";
                break;
            case LABEL_TOO_LONG:
                msg = "A domain name label is longer than 63 bytes.";
                break;
            case LEADING_COMBINING_MARK:
                msg = "A label starts with a combining mark.";
                break;
            case LEADING_HYPHEN:
                if (!checkHyphens) {
                    return;
                }
                msg = "A label starts with a hyphen-minus ('-').";
                break;
            case PUNYCODE:
                msg = "A label starts with \"xn--\" but does not contain valid Punycode.";
                break;
            case TRAILING_HYPHEN:
                if (!checkHyphens) {
                    return;
                }
                msg = "A label ends with a hyphen-minus ('-').";
                break;
            default:
                msg = "IDNA error.";
                break;
        }
        final GalimatiasParseException exception = new GalimatiasParseException(msg);
        errorHandler.fatalError(exception);
        throw exception;
    }
}