uk.ac.starlink.util
Class URLUtils

java.lang.Object
  |
  +--uk.ac.starlink.util.URLUtils

public class URLUtils
extends Object

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 Sources 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

URLUtils

public URLUtils()
Method Detail

makeURL

public static URL makeURL(String location)
Obtains a URL from a string. If the String has the form of a URL, it turned directly into a URL. If it does not, it is treated as a filename, and turned into a file-protocol URL. In the latter case a relative or absolute filename may be used. If it is null or a blank string (or something else equally un-filename like?) then null is returned.

Parameters:
location - a string representing the location of a resource
Returns:
a URL representing the location of the resource

makeURL

public static URL makeURL(String context,
                          String location)
Obtains a URL from a string in a given context. The string context is turned into a URL as per the 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.

Parameters:
context - a string representing the context within which location is to be resolved
location - a string representing the location of a resource
Returns:
a URL representing the location of the resource

urlToUri

public static URI urlToUri(URL url)
Turns a URL into a URI catching the exceptions.

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.

Parameters:
url - a URL to be converted. If this is null, then the method returns null
Returns:
the input URL as a URI, or null if the input was null
Throws:
AssertionError - if there is any problem converting the URL

Copyright © 2004 CLRC: Central Laboratory of the Research Councils. All rights reserved.