Java URL Create constructURL(URL base, String url, boolean stripRef)

Here you can find the source of constructURL(URL base, String url, boolean stripRef)

Description

Construct a URL from its basic parts.

License

Open Source License

Parameter

Parameter Description
base The base URL.
url The URL that was found on the base URL's page.
stripRef True if the URL's reference should be stripped.

Exception

Parameter Description
IOException Thrown if any IO error occurs.

Return

The new URL, built upon the base URL.

Declaration

public static URL constructURL(URL base, String url, boolean stripRef) throws IOException 

Method Source Code

//package com.java2s;
/**// w ww . j a  v  a2 s  .co m
 * The Heaton Research Spider Copyright 2007 by Heaton
 * Research, Inc.
 * 
 * HTTP Programming Recipes for Java ISBN: 0-9773206-6-9
 * http://www.heatonresearch.com/articles/series/16/
 * 
 * URLUtility: A set of useful utilities for processing
 * URL's.
 * 
 * This class is released under the:
 * GNU Lesser General Public License (LGPL)
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * @author Jeff Heaton
 * @version 1.1
 */

import java.io.*;
import java.net.*;

public class Main {
    /**
     * Construct a URL from its basic parts.
     * @param base The base URL.
     * @param url The URL that was found on the base URL's page.
     * @param stripRef True if the URL's reference should be stripped.
     * @return The new URL, built upon the base URL.
     * @throws IOException Thrown if any IO error occurs.
     */
    public static URL constructURL(URL base, String url, boolean stripRef) throws IOException {
        URL result = new URL(base, url);
        String file = result.getFile();
        String protocol = result.getProtocol();
        String host = result.getHost();
        int port = result.getPort();
        String ref = result.getRef();
        StringBuilder sb = new StringBuilder(file);
        int index = sb.indexOf(" ");
        while (index != -1) {
            if (index != -1) {
                sb.replace(index, index + 1, "%20");
            }
            index = sb.indexOf(" ");
        }

        file = sb.toString();
        if ((ref != null) && !stripRef) {
            result = new URL(protocol, host, port, file + "#" + ref);
        } else {
            result = new URL(protocol, host, port, file);
        }
        return result;
    }
}

Related

  1. concatenate(URL server, String address)
  2. concatenateURL(final URL url, final String query)
  3. concatenateURL(URL url, String query)
  4. concatUrl(final URL baseUrl, final String extraPath)
  5. concaturl(final URL p_base, final String p_string)
  6. constructURLQueryString(Map urlParameters)
  7. constructURLString(Map parameters)
  8. convertToURL(String host)
  9. convertToURL(String url)