Java Convert via ByteBuffer toURI(String value)

Here you can find the source of toURI(String value)

Description

Convert a string to java.net.URI If part name is not a valid URI, it is resolved as follows:

1.

License

Apache License

Parameter

Parameter Description
value the string to be parsed into a URI

Return

the resolved part name that should be OK to construct a URI TODO YK: for now this method does only (5). Finish the rest.

Declaration

public static URI toURI(String value) throws URISyntaxException 

Method Source Code


//package com.java2s;
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
    //  w w  w  .j  av a 2 s  .c om
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.io.UnsupportedEncodingException;

public class Main {
    private final static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
            'E', 'F' };

    /**
     * Convert a string to {@link java.net.URI}
     *
     * If  part name is not a valid URI, it is resolved as follows:
     * <p>
     * 1. Percent-encode each open bracket ([) and close bracket (]).</li>
     * 2. Percent-encode each percent (%) character that is not followed by a hexadecimal notation of an octet value.</li>
     * 3. Un-percent-encode each percent-encoded unreserved character.
     * 4. Un-percent-encode each forward slash (/) and back slash (\).
     * 5. Convert all back slashes to forward slashes.
     * 6. If present in a segment containing non-dot (?.?) characters, remove trailing dot (?.?) characters from each segment.
     * 7. Replace each occurrence of multiple consecutive forward slashes (/) with a single forward slash.
     * 8. If a single trailing forward slash (/) is present, remove that trailing forward slash.
     * 9. Remove complete segments that consist of three or more dots.
     * 10. Resolve the relative reference against the base URI of the part holding the Unicode string, as it is defined
     * in ?5.2 of RFC 3986. The path component of the resulting absolute URI is the part name.
     *</p>
     *
     * @param   value   the string to be parsed into a URI
     * @return  the resolved part name that should be OK to construct a URI
     *
     * TODO YK: for now this method does only (5). Finish the rest.
     */
    public static URI toURI(String value) throws URISyntaxException {
        //5. Convert all back slashes to forward slashes
        if (value.indexOf("\\") != -1) {
            value = value.replace('\\', '/');
        }

        // URI fragemnts (those starting with '#') are not encoded
        // and may contain white spaces and raw unicode characters
        int fragmentIdx = value.indexOf('#');
        if (fragmentIdx != -1) {
            String path = value.substring(0, fragmentIdx);
            String fragment = value.substring(fragmentIdx + 1);

            value = path + "#" + encode(fragment);
        }

        return new URI(value);
    }

    /**
     * percent-encode white spaces and characters above 0x80.
     * <p>
     *   Examples:
     *   'Apache POI' --> 'Apache%20POI'
     *   'Apache\u0410POI' --> 'Apache%04%10POI'
     *
     * @param s the string to encode
     * @return  the encoded string
     */
    public static String encode(String s) {
        int n = s.length();
        if (n == 0)
            return s;

        ByteBuffer bb;
        try {
            bb = ByteBuffer.wrap(s.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            // should not happen
            throw new RuntimeException(e);
        }
        StringBuilder sb = new StringBuilder();
        while (bb.hasRemaining()) {
            int b = bb.get() & 0xff;
            if (isUnsafe(b)) {
                sb.append('%');
                sb.append(hexDigits[(b >> 4) & 0x0F]);
                sb.append(hexDigits[(b >> 0) & 0x0F]);
            } else {
                sb.append((char) b);
            }
        }
        return sb.toString();
    }

    private static boolean isUnsafe(int ch) {
        return ch > 0x80 || " ".indexOf(ch) >= 0;
    }
}

Related

  1. toString(Object o)
  2. toString(Reader r)
  3. toString(Reader reader)
  4. toText(@Nonnull byte[] bytes)
  5. toThriftBinary(UUID uuid)
  6. toUTF(byte[] bytes)
  7. toUTF8(String str)
  8. toUuid(byte[] bytes)
  9. toUUID(byte[] uuid)