public class URLFactory extends Object
URLFactory
supports static methods to instantiate URL objects that
support custom protocols/schemes. Since URL
by default only
guarantees protocol handlers for
http, https, ftp, file, and jar
and the URLStreamHandlerFactory
registration is not extensible,
URLFactory will allow application developers to create URL objects for
protocols such as
ws, wse, wsn, wss, wse+ssl
like this:
URL url = URLFactory.createURL("ws://<hostname>:<port>/<serviceName>");
Constructor and Description |
---|
URLFactory() |
Modifier and Type | Method and Description |
---|---|
static URL |
createURL(String spec)
Creates a URL object from the String representation.
|
static URL |
createURL(String protocol,
String host,
int port,
String file)
Creates a URL from the specified protocol name, host name, port number,
and file name.
|
static URL |
createURL(String protocol,
String host,
String file)
Creates a URL from the specified protocol name, host name, and file name.
|
static URL |
createURL(URL context,
String spec)
Creates a URL by parsing the given spec within a specified context.
|
public static URL createURL(String spec) throws MalformedURLException
spec
- the String to parse as a URL.MalformedURLException
- if no protocol is specified, or an
unknown protocol is found, or spec is nullpublic static URL createURL(URL context, String spec) throws MalformedURLException
<scheme>://<authority><path>?<query>#<fragment>
The reference is parsed into the scheme, authority, path, query and fragment parts. If the path component is empty and the scheme, authority, and query components are undefined, then the new URL is a reference to the current document. Otherwise, the fragment and query parts present in the spec are used in the new URL.
If the scheme component is defined in the given spec and does not match the scheme of the context, then the new URL is created as an absolute URL based on the spec alone. Otherwise the scheme component is inherited from the context URL.
If the authority component is present in the spec then the spec is treated as absolute and the spec authority and path will replace the context authority and path. If the authority component is absent in the spec then the authority of the new URL will be inherited from the context.
If the spec's path component begins with a slash character "/" then the path is treated as absolute and the spec path replaces the context path.
Otherwise, the path is treated as a relative path and is appended to the context path, as described in RFC2396. Also, in this case, the path is canonicalized through the removal of directory changes made by occurrences of ".." and ".".
For a more detailed description of URL parsing, refer to RFC2396.
context
- the context in which to parse the specificationspec
- the String to parse as a URLMalformedURLException
- if no protocol is specified, or an unknown
protocol is found, or spec is null.public static URL createURL(String protocol, String host, String file) throws MalformedURLException
This method is equivalent to calling the four-argument method with the arguments being protocol, host, -1, and file. No validation of the inputs is performed by this method.
protocol
- the name of the protocol to usehost
- the name of the hostfile
- the file on the hostMalformedURLException
- if an unknown protocol is specifiedpublic static URL createURL(String protocol, String host, int port, String file) throws MalformedURLException
No validation of the inputs is performed by this method.
protocol
- the name of the protocol to usehost
- the name of the hostport
- the port numberfile
- the file on the hostMalformedURLException
- if an unknown protocol is specifiedCopyright © 2015. All Rights Reserved.