Example usage for java.net IDN toASCII

List of usage examples for java.net IDN toASCII

Introduction

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

Prototype

public static String toASCII(String input) 

Source Link

Document

Translates a string from Unicode to ASCII Compatible Encoding (ACE), as defined by the ToASCII 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

public static String domainToAscii(String input) {
    try {/*from  ww  w. java  2 s . c  om*/
        String result = IDN.toASCII(input).toLowerCase(Locale.US);
        if (result.isEmpty())
            return null;
        if (containsInvalidHostnameAsciiCodes(result)) {
            return null;
        }
        return result;
    } catch (IllegalArgumentException e) {
        return null;
    }
}

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  ww.j  ava2  s. co 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:WebBrowserBasedOnJEditorPane.java

public WebBrowserBasedOnJEditorPane() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel pnlURL = new JPanel();
    pnlURL.setLayout(new BorderLayout());
    pnlURL.add(new JLabel("URL: "), BorderLayout.WEST);
    pnlURL.add(txtURL, BorderLayout.CENTER);
    getContentPane().add(pnlURL, BorderLayout.NORTH);
    getContentPane().add(ep, BorderLayout.CENTER);

    getContentPane().add(lblStatus, BorderLayout.SOUTH);

    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            try {
                String url = ae.getActionCommand().toLowerCase();
                if (url.startsWith("http://"))
                    url = url.substring(7);
                ep.setPage("http://" + IDN.toASCII(url));
            } catch (Exception e) {
                e.printStackTrace();//from  w  w w  .java  2s.  c  o m
                JOptionPane.showMessageDialog(WebBrowserBasedOnJEditorPane.this,
                        "Browser problem: " + e.getMessage());
            }
        }
    };
    txtURL.addActionListener(al);

    setSize(300, 300);
    setVisible(true);
}

From source file:com.linkedin.urls.HostNormalizer.java

private void normalizeHost() {
    if (StringUtils.isEmpty(_host)) {
        return;/* w w  w .j  a v a 2  s  . c om*/
    }

    String host;
    try {
        //replace high unicode characters
        host = IDN.toASCII(_host);
    } catch (IllegalArgumentException ex) {
        //occurs when the url is invalid. Just return
        return;
    }

    host = host.toLowerCase();
    host = UrlUtil.decode(host);

    _bytes = tryDecodeHostToIp(host);

    if (_bytes != null) {
        InetAddress address;
        try {
            address = InetAddress.getByAddress(_bytes);
            String ipAddress = address.getHostAddress();
            if (address instanceof Inet6Address) {
                host = "[" + ipAddress + "]";
            } else {
                host = ipAddress;
            }
        } catch (UnknownHostException e) {
            return;
        }
    }

    if (StringUtils.isEmpty(host)) {
        return;
    }

    host = UrlUtil.removeExtraDots(host);

    _normalizedHost = UrlUtil.encode(host).replace("\\x", "%");
}

From source file:ch.cyberduck.core.idna.PunycodeConverter.java

/**
 * @return IDN normalized hostname//ww w . j  a  v  a2  s  . c  om
 */
public String convert(final String hostname) {
    if (!PreferencesFactory.get().getBoolean("connection.hostname.idn")) {
        return StringUtils.strip(hostname);
    }
    if (StringUtils.isNotEmpty(hostname)) {
        try {
            // Convenience function that implements the IDNToASCII operation as defined in
            // the IDNA RFC. This operation is done on complete domain names, e.g: "www.example.com".
            // It is important to note that this operation can fail. If it fails, then the input
            // domain name cannot be used as an Internationalized Domain Name and the application
            // should have methods defined to deal with the failure.
            // IDNA.DEFAULT Use default options, i.e., do not process unassigned code points
            // and do not use STD3 ASCII rules If unassigned code points are found
            // the operation fails with ParseException
            final String idn = IDN.toASCII(StringUtils.strip(hostname));
            if (log.isDebugEnabled()) {
                if (!StringUtils.equals(StringUtils.strip(hostname), idn)) {
                    log.debug(String.format("IDN hostname for %s is %s", hostname, idn));
                }
            }
            if (StringUtils.isNotEmpty(idn)) {
                return idn;
            }
        } catch (IllegalArgumentException e) {
            log.warn(String.format("Failed to convert hostname %s to IDNA", hostname), e);
        }
    }
    return StringUtils.strip(hostname);
}

From source file:com.jajja.arachne.net.Domain.java

/**
 * Creates a domain.//ww w  .j ava2s .  com
 *
 * @param name
 *            the domain name
 * @throws MalformedDomainException
 *             when the domain name can not be parsed as a domain
 */
public Domain(String name) throws MalformedDomainException {
    super(IDN.toASCII(name));
    parse();
}

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
 *//*w  w  w. ja va 2  s  .  com*/
@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:$.MyValidator.java

private boolean matchPart(String part, Pattern pattern) {
        try {/*from   ww  w  .  j a  v a 2s  .com*/
            part = IDN.toASCII(part);
        } catch (IllegalArgumentException e) {
            // occurs when the label is too long (>63, even though it should
            // probably be 64 - see
            // http://www.rfc-editor.org/errata_search.php?rfc=3696,
            // practically that should not be a problem)
            return false;
        }
        Matcher matcher = pattern.matcher(part);
        return matcher.matches();
    }