Java URI Create createURI(final String scheme, final String host, int port, final String path, final String query, final String fragment)

Here you can find the source of createURI(final String scheme, final String host, int port, final String path, final String query, final String fragment)

Description

create URI

License

Apache License

Declaration

public static URI createURI(final String scheme, final String host, int port, final String path,
            final String query, final String fragment) throws URISyntaxException 

Method Source Code


//package com.java2s;
/*/*from   w w  w  . j  a  va2s.c o  m*/
 * ====================================================================
 *
 *  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
 *
 *      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.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    public static URI createURI(final String scheme, final String host, int port, final String path,
            final String query, final String fragment) throws URISyntaxException {

        StringBuilder buffer = new StringBuilder();
        if (host != null) {
            if (scheme != null) {
                buffer.append(scheme);
                buffer.append("://");
            }
            buffer.append(host);
            if (port > 0) {
                buffer.append(':');
                buffer.append(port);
            }
        }
        if (path == null || !path.startsWith("/")) {
            buffer.append('/');
        }
        if (path != null) {
            buffer.append(path);
        }
        if (query != null) {
            buffer.append('?');
            buffer.append(query);
        }
        if (fragment != null) {
            buffer.append('#');
            buffer.append(fragment);
        }
        return new URI(buffer.toString());
    }
}

Related

  1. createUri(File file)
  2. createURI(final String file)
  3. createUri(final String path)
  4. createUri(final String path, final String query)
  5. createURI(final String scheme, final String host, final int port)
  6. createURI(final String scheme, final String[] pathAsArray)
  7. createURI(List proxyPorts)
  8. createUri(String base)
  9. createURI(String extension)