|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--uk.ac.starlink.util.URLUtils
Provides convenience methods for resolving URLs. This class provides some static methods for turning strings into URLs. This tends to be a bit of a pain in java, since you have to watch out for MalformedURLExceptions all over and work out what the context is. The methods provided here assume that if a string looks like a URL it is one, if it doesn't it's a file name, and if it's not absolute or resolved against a given context it is relative to the current directory. From the point of view of a user providing text to an application, or an XML document providing an href, this is nearly always what is wanted. The strategy can lead to surprising situations in the case that wacky URL protocols are used; for instance if makeURL is called on the string "gftp://host/file" and no gftp handler is installed, it will be interpreted as a file-protocol URL referring to the (presumably non-existent) file "gftp://host/file". In this case the eventual upshot will presumably be a file-not-found type error rather than a MalformedURLException type error getting presented to the user. Users of this class should be of the opinion that this is not a particularly bad thing.
The systemId strings used by Source
s
have similar semantics to the strings which this class converts
to URLs or contexts.
This class assumes that the "file:" protocol is legal for URLs, and will throw AssertionErrors if this turns out not to be the case.
Constructor Summary | |
URLUtils()
|
Method Summary | |
static URL |
makeURL(String location)
Obtains a URL from a string. |
static URL |
makeURL(String context,
String location)
Obtains a URL from a string in a given context. |
static URI |
urlToUri(URL url)
Turns a URL into a URI catching the exceptions. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public URLUtils()
Method Detail |
public static URL makeURL(String location)
location
- a string representing the location of a resource
public static URL makeURL(String context, String location)
makeURL(String)
method, unless it is null or
the empty string, in which case it is treated as a reference
to the current directory.
The string location is then turned into a URL in
the same way as using makeURL(String)
, except that
if it represents a relative path it is resolved in the context
of context, taking its protocol and/or relative position
from it.
context
- a string representing the context within which
location is to be resolvedlocation
- a string representing the location of a resource
public static URI urlToUri(URL url)
It seems very unlikely that an exception can actuallly result here, since a URIs are surely a superset of URLs? So why doesn't this method (or an equivalent constructor) exist in the URI class? Anyway, if there is any problem creating the URI, we throw an assertion error, because the world is surely out of joint. That is, this method always succeeds.
url
- a URL to be converted. If this is null, then the method returns null
AssertionError
- if there is any problem converting the URL
|
Copyright © 2004 CLRC: Central Laboratory of the Research Councils. All rights reserved. | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |