Example usage for java.net IDN toUnicode

List of usage examples for java.net IDN toUnicode

Introduction

In this page you can find the example usage for java.net IDN toUnicode.

Prototype

public static String toUnicode(String input) 

Source Link

Document

Translates a string from ASCII Compatible Encoding (ACE) to Unicode, as defined by the ToUnicode operation of <a href="http://www.ietf.org/rfc/rfc3490.txt">RFC 3490</a>.

Usage

From source file:IDNConverter.java

public static void main(String[] args) {
    IDN.toASCII("");
    IDN.toUnicode("");
}

From source file:IDNDemo.java

public static void main(String[] args) {
    String input = "www.yourName.com";
    String ascii = IDN.toASCII(input);
    String unicode = IDN.toUnicode(input);

    System.out.println("Input:" + input);
    System.out.println("toAscii (input):" + ascii);
    System.out.println("toUnicode (input):" + unicode);
}

From source file:Main.java

/**
 * Converts an internationalized domain name (IDN) in an URL to and from ASCII/Unicode.
 * @param url the URL where the domain name should be converted
 * @param toASCII if true converts from Unicode to ASCII, if false converts from ASCII to Unicode
 * @return the URL containing the converted domain name
 *//*from  w  w w.j  a  v a  2s . c o m*/
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static String convertIdn(String url, boolean toASCII) {

    String urlNoDots = url;
    String dots = "";
    while (urlNoDots.startsWith(".")) {
        urlNoDots = url.substring(1);
        dots = dots + ".";
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        // Find host name after '//' or '@'
        int hostStart = 0;
        if (urlNoDots.indexOf("//") != -1) {
            hostStart = url.indexOf("//") + "//".length();
        } else if (url.indexOf("@") != -1) {
            hostStart = url.indexOf("@") + "@".length();
        }

        int hostEnd = url.substring(hostStart).indexOf("/");
        // Handle URL which doesn't have a path (path is implicitly '/')
        hostEnd = (hostEnd == -1 ? urlNoDots.length() : hostStart + hostEnd);

        String host = urlNoDots.substring(hostStart, hostEnd);
        host = (toASCII ? IDN.toASCII(host) : IDN.toUnicode(host));

        return dots + urlNoDots.substring(0, hostStart) + host + urlNoDots.substring(hostEnd);
    } else {
        return dots + url;
    }
}

From source file:org.apache.metron.common.typosquat.HomoglyphStrategy.java

@Override
public Set<String> generateCandidates(String originalString) {
    Set<String> result = new HashSet<>();
    String domain = originalString;
    if (StringUtils.isEmpty(domain)) {
        return result;
    }// www .  ja  v a 2  s  . c  o m
    if (isAce(domain)) {
        //this is an ace domain.
        domain = IDN.toUnicode(domain);
    }
    for (int ws = 0; ws < domain.length(); ws++) {
        for (int i = 0; i < domain.length() - ws + 1; ++i) {
            String win = domain.substring(i, i + ws);
            for (int j = 0; j < ws; j++) {
                char c = win.charAt(j);
                if (glyphs.containsKey(c)) {
                    for (String g : glyphs.get(c)) {
                        String winNew = win.replaceAll("" + c, g);
                        String d = domain.substring(0, i) + winNew + domain.substring(i + ws);
                        result.add(d);
                        if (!isAce(d)) {
                            try {
                                String dAscii = IDN.toASCII(d, IDN.ALLOW_UNASSIGNED);
                                if (!d.equals(dAscii)) {
                                    result.add(dAscii);
                                }
                            } catch (IllegalArgumentException iae) {
                                LOG.debug("Unable to parse " + d + ": " + iae.getMessage(), iae);
                            }
                        }
                    }
                }
            }
        }
    }
    return result;
}

From source file:com.synox.android.utils.DisplayUtils.java

/**
 * Converts an internationalized domain name (IDN) in an URL to and from ASCII/Unicode.
 * @param url the URL where the domain name should be converted
 * @param toASCII if true converts from Unicode to ASCII, if false converts from ASCII to Unicode
 * @return the URL containing the converted domain name
 *//*from ww  w .j a v  a  2s .  c o m*/
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static String convertIdn(String url, boolean toASCII) {

    String urlNoDots = url;
    String dots = "";
    while (urlNoDots.startsWith(".")) {
        urlNoDots = url.substring(1);
        dots = dots + ".";
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        // Find host name after '//' or '@'
        int hostStart = 0;
        if (urlNoDots.contains("//")) {
            hostStart = url.indexOf("//") + "//".length();
        } else if (url.contains("@")) {
            hostStart = url.indexOf("@") + "@".length();
        }

        int hostEnd = url.substring(hostStart).indexOf("/");
        // Handle URL which doesn't have a path (path is implicitly '/')
        hostEnd = (hostEnd == -1 ? urlNoDots.length() : hostStart + hostEnd);

        String host = urlNoDots.substring(hostStart, hostEnd);
        host = (toASCII ? IDN.toASCII(host) : IDN.toUnicode(host));

        return dots + urlNoDots.substring(0, hostStart) + host + urlNoDots.substring(hostEnd);
    } else {
        return dots + url;
    }
}

From source file:com.redhat.rhn.frontend.dto.SystemSearchResult.java

/**
  * @return the hostname in IDN encoding
  */
public String getDecodedHostname() {
    return (hostname == null) ? null : IDN.toUnicode(hostname);
}

From source file:com.redhat.rhn.domain.server.Server.java

/**
 * Get the primary hostname for this server
 * If hostname is IDN, it is decoded from Pune encoding
 * @return Returns the primary hostname for this server
 *//*from  w w w  .j  a  v a  2  s  . c  o  m*/
public String getDecodedHostname() {
    String hostname = getHostname();
    return (hostname == null) ? null : IDN.toUnicode(hostname);
}

From source file:com.redhat.rhn.domain.server.Server.java

/**
 * Get the hostname aliases (cname records) for this server
 * If hostname is IDN, it is decoded from Pune encoding
 * @return Returns the primary hostname for this server
 *//*from  w w  w.  ja v a 2s.  c o m*/
public List<String> getDecodedCnames() {
    List<String> result = new ArrayList<String>();
    for (String hostname : getCnames()) {
        result.add(IDN.toUnicode(hostname));
    }
    return result;
}

From source file:com.redhat.rhn.manager.system.SystemManager.java

/**
 * List duplicate systems by hostname/*  ww w  .  j av  a  2s .c  o  m*/
 * @param user the user doing the search
 * @param inactiveHours the number of hours a system hasn't checked in
 *          to consider it inactive
 * @return List of DuplicateSystemBucket objects
 */
public static List<DuplicateSystemGrouping> listDuplicatesByHostname(User user, Long inactiveHours) {
    List<DuplicateSystemGrouping> duplicateSystems = listDuplicates(user, "duplicate_system_ids_hostname",
            new ArrayList<String>(), inactiveHours);
    ListIterator<DuplicateSystemGrouping> litr = duplicateSystems.listIterator();
    while (litr.hasNext()) {
        DuplicateSystemGrouping element = litr.next();
        element.setKey(IDN.toUnicode(element.getKey()));
    }
    return duplicateSystems;
}