public static String encodeURL()

in doxia-core/src/main/java/org/apache/maven/doxia/util/HtmlTools.java [354:416]


    public static String encodeURL(String url) {
        if (url == null) {
            return null;
        }

        StringBuilder encoded = new StringBuilder();
        int length = url.length();

        char[] unicode = new char[1];

        for (int i = 0; i < length; ++i) {
            char c = url.charAt(i);

            switch (c) {
                case ';':
                case '/':
                case '?':
                case ':':
                case '@':
                case '&':
                case '=':
                case '+':
                case '$':
                case ',':
                case '[':
                case ']': // RFC 2732 (IPV6)
                case '-':
                case '_':
                case '.':
                case '!':
                case '~':
                case '*':
                case '\'':
                case '(':
                case ')':
                case '#': // XLink mark
                    encoded.append(c);
                    break;
                default:
                    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
                        encoded.append(c);
                    } else {
                        byte[] bytes;

                        if (isHighSurrogate(c)) {
                            int codePoint = toCodePoint(c, url.charAt(++i));
                            unicode = toChars(codePoint);
                            bytes = (new String(unicode, 0, unicode.length)).getBytes(StandardCharsets.UTF_8);
                        } else {
                            unicode[0] = c;
                            bytes = (new String(unicode, 0, 1)).getBytes(StandardCharsets.UTF_8);
                        }

                        for (byte aByte : bytes) {
                            encoded.append('%');
                            encoded.append(String.format("%02X", aByte));
                        }
                    }
            }
        }

        return encoded.toString();
    }