com.gargoylesoftware.htmlunit.util
Class UrlUtils

java.lang.Object
  extended by com.gargoylesoftware.htmlunit.util.UrlUtils

public final class UrlUtils
extends Object

URL utilities class that makes it easy to create new URLs based off of old URLs without having to assemble or parse them yourself.

Version:
$Revision: 5644 $
Author:
Daniel Gredler, Martin Tamme, Sudhan Moghe, Marc Guillemot, Ahmed Ashour

Method Summary
static String decode(String escaped)
          Unescapes and decodes the specified string.
static String encodeAnchor(String anchor)
          Encodes and escapes the specified URI anchor string.
static URL encodeUrl(URL url, boolean minimalQueryEncoding)
          Encodes illegal characters in the specified URL's path, query string and anchor according to the URL encoding rules observed in real browsers.
static URL getUrlWithNewHost(URL u, String newHost)
          Creates and returns a new URL identical to the specified URL, except using the specified host.
static URL getUrlWithNewPath(URL u, String newPath)
          Creates and returns a new URL identical to the specified URL, except using the specified path.
static URL getUrlWithNewPort(URL u, int newPort)
          Creates and returns a new URL identical to the specified URL, except using the specified port.
static URL getUrlWithNewProtocol(URL u, String newProtocol)
          Creates and returns a new URL identical to the specified URL, except using the specified protocol.
static URL getUrlWithNewQuery(URL u, String newQuery)
          Creates and returns a new URL identical to the specified URL, except using the specified query string.
static URL getUrlWithNewRef(URL u, String newRef)
          Creates and returns a new URL identical to the specified URL, except using the specified reference.
static String resolveUrl(String baseUrl, String relativeUrl)
          Resolves a given relative URL against a base URL.
static String resolveUrl(URL baseUrl, String relativeUrl)
          Resolves a given relative URL against a base URL.
static URL toUrlSafe(String url)
          Constructs a URL instance based on the specified URL string, taking into account the fact that the specified URL string may represent an "about:..." URL, a "javascript:..." URL, or a data:... URL.
static URL toUrlUnsafe(String url)
          Constructs a URL instance based on the specified URL string, taking into account the fact that the specified URL string may represent an "about:..." URL, a "javascript:..." URL, or a data:... URL.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toUrlSafe

public static URL toUrlSafe(String url)

Constructs a URL instance based on the specified URL string, taking into account the fact that the specified URL string may represent an "about:..." URL, a "javascript:..." URL, or a data:... URL.

The caller should be sure that URL strings passed to this method will parse correctly as URLs, as this method never expects to have to handle MalformedURLExceptions.

Parameters:
url - the URL string to convert into a URL instance
Returns:
the constructed URL instance

toUrlUnsafe

public static URL toUrlUnsafe(String url)
                       throws MalformedURLException

Constructs a URL instance based on the specified URL string, taking into account the fact that the specified URL string may represent an "about:..." URL, a "javascript:..." URL, or a data:... URL.

Unlike toUrlSafe(String), the caller need not be sure that URL strings passed to this method will parse correctly as URLs.

Parameters:
url - the URL string to convert into a URL instance
Returns:
the constructed URL instance
Throws:
MalformedURLException - if the URL string cannot be converted to a URL instance

encodeUrl

public static URL encodeUrl(URL url,
                            boolean minimalQueryEncoding)

Encodes illegal characters in the specified URL's path, query string and anchor according to the URL encoding rules observed in real browsers.

For example, this method changes "http://first/?a=b c" to "http://first/?a=b%20c".

Parameters:
url - the URL to encode
minimalQueryEncoding - whether or not to perform minimal query encoding, like IE does
Returns:
the encoded URL

encodeAnchor

public static String encodeAnchor(String anchor)
Encodes and escapes the specified URI anchor string.

Parameters:
anchor - the anchor string to encode and escape
Returns:
the encoded and escaped anchor string

decode

public static String decode(String escaped)
Unescapes and decodes the specified string.

Parameters:
escaped - the string to be unescaped and decoded
Returns:
the unescaped and decoded string

getUrlWithNewProtocol

public static URL getUrlWithNewProtocol(URL u,
                                        String newProtocol)
                                 throws MalformedURLException
Creates and returns a new URL identical to the specified URL, except using the specified protocol.

Parameters:
u - the URL on which to base the returned URL
newProtocol - the new protocol to use in the returned URL
Returns:
a new URL identical to the specified URL, except using the specified protocol
Throws:
MalformedURLException - if there is a problem creating the new URL

getUrlWithNewHost

public static URL getUrlWithNewHost(URL u,
                                    String newHost)
                             throws MalformedURLException
Creates and returns a new URL identical to the specified URL, except using the specified host.

Parameters:
u - the URL on which to base the returned URL
newHost - the new host to use in the returned URL
Returns:
a new URL identical to the specified URL, except using the specified host
Throws:
MalformedURLException - if there is a problem creating the new URL

getUrlWithNewPort

public static URL getUrlWithNewPort(URL u,
                                    int newPort)
                             throws MalformedURLException
Creates and returns a new URL identical to the specified URL, except using the specified port.

Parameters:
u - the URL on which to base the returned URL
newPort - the new port to use in the returned URL
Returns:
a new URL identical to the specified URL, except using the specified port
Throws:
MalformedURLException - if there is a problem creating the new URL

getUrlWithNewPath

public static URL getUrlWithNewPath(URL u,
                                    String newPath)
                             throws MalformedURLException
Creates and returns a new URL identical to the specified URL, except using the specified path.

Parameters:
u - the URL on which to base the returned URL
newPath - the new path to use in the returned URL
Returns:
a new URL identical to the specified URL, except using the specified path
Throws:
MalformedURLException - if there is a problem creating the new URL

getUrlWithNewRef

public static URL getUrlWithNewRef(URL u,
                                   String newRef)
                            throws MalformedURLException
Creates and returns a new URL identical to the specified URL, except using the specified reference.

Parameters:
u - the URL on which to base the returned URL
newRef - the new reference to use in the returned URL
Returns:
a new URL identical to the specified URL, except using the specified reference
Throws:
MalformedURLException - if there is a problem creating the new URL

getUrlWithNewQuery

public static URL getUrlWithNewQuery(URL u,
                                     String newQuery)
                              throws MalformedURLException
Creates and returns a new URL identical to the specified URL, except using the specified query string.

Parameters:
u - the URL on which to base the returned URL
newQuery - the new query string to use in the returned URL
Returns:
a new URL identical to the specified URL, except using the specified query string
Throws:
MalformedURLException - if there is a problem creating the new URL

resolveUrl

public static String resolveUrl(String baseUrl,
                                String relativeUrl)
Resolves a given relative URL against a base URL. See RFC1808 Section 4 for more details.

Parameters:
baseUrl - The base URL in which to resolve the specification.
relativeUrl - The relative URL to resolve against the base URL.
Returns:
the resolved specification.

resolveUrl

public static String resolveUrl(URL baseUrl,
                                String relativeUrl)
Resolves a given relative URL against a base URL. See RFC1808 Section 4 for more details.

Parameters:
baseUrl - The base URL in which to resolve the specification.
relativeUrl - The relative URL to resolve against the base URL.
Returns:
the resolved specification.


Copyright © 2002-2010 Gargoyle Software Inc.. All Rights Reserved.