org.aitools.programd.util
Class URLTools

java.lang.Object
  extended by org.aitools.programd.util.URLTools

public class URLTools
extends Object

URLTools contains helper methods for dealing with URLs.

Since:
4.6
Author:
Noel Bush

Constructor Summary
URLTools()
           
 
Method Summary
static URL contextualize(URL context, String subject)
          Same as contextualize(URL, URL), except the subject is a String which is supposed to not be absolute (a quick check is made of this, and if the subject does look absolute, it is made into a URL and sent to contextualize(URL, URL)).
static URL contextualize(URL context, URL subject)
           Tries to put the subject in the "context" of the context.
static URL createValidURL(String path)
          Attempts to create the given path into a valid URL, using a few heuristics.
static URL createValidURL(String path, URL context)
          Attempts to create the given path into a valid URL, using a few heuristics.
static URL createValidURL(String path, URL context, boolean tryToValidate)
          Attempts to create the given path into a valid URL, using a few heuristics.
static String escape(String url)
          Does very minimal URL escaping -- just enough to avoid complaints from the URI & URL constructors (maybe).
static long getLastModified(URL path)
          Tries to get the last modified timestamp for the path.
static URL getParent(URL url)
           
static List<URL> getURLs(String pathspec, URL context)
          Take a path spec that may, or may not, use glob-style wildcards to indicate multiple files, and returns a list of URLs pointing to those files.
static boolean pathsAreEquivalent(String path1, String path2)
          Using some rather uncomfortable heuristics, judges whether two given paths are (probably) equivalent, by ignoring certain differences like platform-specific path separators vs.
static boolean seemsToExist(URL url)
          Tests whether a resource seems to exist at the given URL
static String unescape(String url)
          Reverses escape(String).
static String unescape(URL url)
          A convenience method that calls toString() on the given URL, then returns the result of unescape(String).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

URLTools

public URLTools()
Method Detail

contextualize

public static URL contextualize(URL context,
                                URL subject)

Tries to put the subject in the "context" of the context. If the context URL does not appear to specify a file, this will essentially be the equivalent of URI.resolve; if a file is specified by context, and if subject is relative, then this will replace the file component of context with subject.

If subject is not relative, this will throw a MalformedURLException is thrown.

Parameters:
context -
subject -
Returns:
the result of "contextualizing" the given subject in the context

contextualize

public static URL contextualize(URL context,
                                String subject)
Same as contextualize(URL, URL), except the subject is a String which is supposed to not be absolute (a quick check is made of this, and if the subject does look absolute, it is made into a URL and sent to contextualize(URL, URL)).

Parameters:
context -
subject -
Returns:
the result of "contextualizing" the given subject in the context

createValidURL

public static URL createValidURL(String path)
                          throws FileNotFoundException
Attempts to create the given path into a valid URL, using a few heuristics. Tries to validate the given path (if it is a file).

Parameters:
path -
Returns:
a valid URL, if possible
Throws:
FileNotFoundException

createValidURL

public static URL createValidURL(String path,
                                 URL context)
                          throws FileNotFoundException
Attempts to create the given path into a valid URL, using a few heuristics. Tries to validate the given path (if it is a file).

Parameters:
path -
context - the context in which to resolve relative URLs (may be null)
Returns:
a valid URL, if possible
Throws:
FileNotFoundException

createValidURL

public static URL createValidURL(String path,
                                 URL context,
                                 boolean tryToValidate)
                          throws FileNotFoundException
Attempts to create the given path into a valid URL, using a few heuristics.

Parameters:
path -
context - the context in which to resolve relative URLs (may be null)
tryToValidate - whether the method should try to validate the existence of the path
Returns:
a valid URL, if possible
Throws:
FileNotFoundException

getURLs

public static List<URL> getURLs(String pathspec,
                                URL context)
Take a path spec that may, or may not, use glob-style wildcards to indicate multiple files, and returns a list of URLs pointing to those files.

Parameters:
pathspec - the path specification that may point to one or many files
Returns:
a list of URLs

getParent

public static URL getParent(URL url)
Parameters:
url - some URL
Returns:
the "parent" of the given URL, if possible

seemsToExist

public static boolean seemsToExist(URL url)
Tests whether a resource seems to exist at the given URL

Parameters:
url - the URL to test
Returns:
whether a resource seems to exist at the URL

getLastModified

public static long getLastModified(URL path)
Tries to get the last modified timestamp for the path.

Parameters:
path - the URL to check
Returns:
the apparent last modified timestamp, or 0 if cannot be determined

escape

public static String escape(String url)
Does very minimal URL escaping -- just enough to avoid complaints from the URI & URL constructors (maybe).

Parameters:
url - the URL to escape
Returns:
the escaped URL

unescape

public static String unescape(String url)
Reverses escape(String).

Parameters:
url - the URL to unescape
Returns:
the unescaped URL

unescape

public static String unescape(URL url)
A convenience method that calls toString() on the given URL, then returns the result of unescape(String).


pathsAreEquivalent

public static boolean pathsAreEquivalent(String path1,
                                         String path2)
Using some rather uncomfortable heuristics, judges whether two given paths are (probably) equivalent, by ignoring certain differences like platform-specific path separators vs. the URI/URL standard slash, and the use of a Windows drive letter preceded, or not, by a slash. Yuck.

Parameters:
path1 -
path2 -
Returns:
whether or not they are (probably) equivalent