Java URI to toExternalForm(URI u)

Here you can find the source of toExternalForm(URI u)

Description

To fix the 3 slashes bug for File URI: For example: file:/C:/work/test.txt -> file:///C:/work/test.txt

License

Open Source License

Parameter

Parameter Description
u - the File URI

Return

the String of the URI

Declaration

public static String toExternalForm(URI u) throws URISyntaxException 

Method Source Code

//package com.java2s;
/************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * //from   www .  j a v a2s. com
 * Copyright 2008, 2010 Oracle and/or its affiliates. All rights reserved.
 * 
 * Use is subject to license terms.
 * 
 * Licensed 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 http://www.apache.org/licenses/LICENSE-2.0. You can also
 * obtain a copy of the License at http://odftoolkit.org/docs/license.txt
 * 
 * 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;

public class Main {
    /**
     * To fix the 3 slashes bug for File URI: For example:
     * file:/C:/work/test.txt -> file:///C:/work/test.txt
     * 
     * @param u - the File URI
     * @return the String of the URI
     */
    public static String toExternalForm(URI u) throws URISyntaxException {
        StringBuilder sb = new StringBuilder();
        if (u.getScheme() != null) {
            sb.append(u.getScheme());
            sb.append(':');
        }
        if (u.isOpaque()) {
            sb.append(u.getSchemeSpecificPart());
        } else {
            if (u.getHost() != null) {
                sb.append("//");
                if (u.getUserInfo() != null) {
                    sb.append(u.getUserInfo());
                    sb.append('@');
                }
                boolean needBrackets = ((u.getHost().indexOf(':') >= 0) && !u.getHost().startsWith("[")
                        && !u.getHost().endsWith("]"));
                if (needBrackets)
                    sb.append('[');
                sb.append(u.getHost());
                if (needBrackets)
                    sb.append(']');
                if (u.getPort() != -1) {
                    sb.append(':');
                    sb.append(u.getPort());
                }
            } else if (u.getRawAuthority() != null) {
                sb.append("//");
                sb.append(u.getRawAuthority());
            } else {
                sb.append("//");
            }
            if (u.getRawPath() != null)
                sb.append(u.getRawPath());
            if (u.getRawQuery() != null) {
                sb.append('?');
                sb.append(u.getRawQuery());
            }
        }
        if (u.getFragment() != null) {
            sb.append('#');
            sb.append(u.getFragment());
        }
        String ret = new URI(sb.toString()).toASCIIString();
        return ret;
    }
}

Related

  1. extractS3Key(URI uri)
  2. extractTargetSystemFromUri(URI uri)
  3. extractURIFromText(String texto)
  4. toDecodedString(URI uri)
  5. toExistingFile(URI uri)
  6. toFile(java.net.URI uri)
  7. toFile(String filenameOrFileURI)
  8. toFile(String unixPathOrUri)
  9. toFile(URI uri)