Java URL Create createURL(final String url)

Here you can find the source of createURL(final String url)

Description

create URL

License

Apache License

Declaration

public static URL createURL(final String url) 

Method Source Code

//package com.java2s;
/*//from w  w w.ja v  a 2s. c o  m
 * Copyright 2005-2010 the original author or authors.
 * 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
 *
 * 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.MalformedURLException;

import java.net.URL;

public class Main {
    private static final String SLASH = "/";
    private static final String FILE_PROTOCOL = "file";

    public static URL createURL(final String url) {
        try {
            return new URL(url);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    public static URL createURL(final URL baseURL, String relativeURL) {
        try {
            String protocol = baseURL.getProtocol();
            relativeURL = relativeURL.replace("\\", SLASH);
            // TODO
            if (protocol.startsWith(FILE_PROTOCOL)) {
                if (relativeURL.startsWith(SLASH)) {
                    relativeURL = relativeURL.substring(1, relativeURL.length());
                }
                return new URL(baseURL, relativeURL);
            } else {
                // TODO
                String baseArchivePath = baseURL.toExternalForm();
                if (baseArchivePath.endsWith(SLASH)) {
                    baseArchivePath = baseArchivePath.substring(0, baseArchivePath.length() - 1);
                }
                if (baseArchivePath.endsWith("!")) {
                    baseArchivePath = baseArchivePath.substring(0, baseArchivePath.length() - 1);
                }
                if (!relativeURL.startsWith(SLASH)) {
                    relativeURL = SLASH + relativeURL;
                }
                return new URL(baseArchivePath + "!" + relativeURL);
            }
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. convertToUrl(String[] paths)
  2. convertToURLs(String[] hosts)
  3. createURL(final String address)
  4. createURL(final String protocol, final String userInfo, final String host, final int port, final String path, final String query, final String ref)
  5. createUrl(final String spec)
  6. createURL(String fileName)
  7. createUrl(String spec)
  8. createURL(String spec, URLStreamHandlerFactory urlHandlerFactory)
  9. createURL(String src, File basedir)