org.aitools.programd.util
Class URLTools

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

public class URLTools
extends java.lang.Object

URLTools contains helper methods for dealing with URLs.

Since:
4.6
Author:
Noel Bush

Field Summary
private static java.lang.String COLON_SLASH
          The string ":/".
private static java.lang.String DOT
          A dot (period).
private static java.lang.String EMPTY_STRING
          The empty string.
private static org.apache.log4j.Logger logger
           
private static java.lang.String SLASH
          A slash.
 
Constructor Summary
URLTools()
           
 
Method Summary
static java.net.URL contextualize(java.net.URL context, java.lang.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 java.net.URL contextualize(java.net.URL context, java.net.URL subject)
           Tries to put the subject in the "context" of the context.
static java.net.URL createValidURL(java.lang.String path)
          Attempts to create the given path into a valid URL, using a few heuristics.
static java.net.URL createValidURL(java.lang.String path, java.net.URL context)
          Attempts to create the given path into a valid URL, using a few heuristics.
static java.net.URL createValidURL(java.lang.String path, java.net.URL context, boolean tryToValidate)
          Attempts to create the given path into a valid URL, using a few heuristics.
static java.lang.String escape(java.lang.String url)
          Does very minimal URL escaping -- just enough to avoid complaints from the URI & URL constructors (maybe).
static long getLastModified(java.net.URL path)
          Tries to get the last modified timestamp for the path.
static java.net.URL getParent(java.net.URL url)
           
static java.util.List<java.net.URL> getURLs(java.lang.String pathspec, java.net.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(java.lang.String path1, java.lang.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.
private static boolean probablyIsNotFile(java.lang.String file)
          Uses a couple of simple heuristics to guess whether a given URL probably is not pointing at a file.
private static boolean probablyIsNotFile(java.net.URL url)
          Uses a couple of simple heuristics to guess whether a given URL probably is not pointing at a file.
static boolean seemsToExist(java.net.URL url)
          Tests whether a resource seems to exist at the given URL
static java.lang.String unescape(java.lang.String url)
          Reverses escape(String).
static java.lang.String unescape(java.net.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
 

Field Detail

SLASH

private static final java.lang.String SLASH
A slash.

See Also:
Constant Field Values

COLON_SLASH

private static final java.lang.String COLON_SLASH
The string ":/".

See Also:
Constant Field Values

DOT

private static final java.lang.String DOT
A dot (period).

See Also:
Constant Field Values

EMPTY_STRING

private static final java.lang.String EMPTY_STRING
The empty string.

See Also:
Constant Field Values

logger

private static final org.apache.log4j.Logger logger
Constructor Detail

URLTools

public URLTools()
Method Detail

contextualize

public static java.net.URL contextualize(java.net.URL context,
                                         java.net.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 java.net.URL contextualize(java.net.URL context,
                                         java.lang.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

probablyIsNotFile

private static boolean probablyIsNotFile(java.net.URL url)
Uses a couple of simple heuristics to guess whether a given URL probably is not pointing at a file. NOTE: This is way imperfect! :-)

Parameters:
url - the URL to check
Returns:
whether it probably is not a file

probablyIsNotFile

private static boolean probablyIsNotFile(java.lang.String file)
Uses a couple of simple heuristics to guess whether a given URL probably is not pointing at a file. NOTE: This is way imperfect! :-)

Parameters:
file - the path to check
Returns:
whether it probably is not a file

createValidURL

public static java.net.URL createValidURL(java.lang.String path)
                                   throws java.io.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:
java.io.FileNotFoundException

createValidURL

public static java.net.URL createValidURL(java.lang.String path,
                                          java.net.URL context)
                                   throws java.io.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:
java.io.FileNotFoundException

createValidURL

public static java.net.URL createValidURL(java.lang.String path,
                                          java.net.URL context,
                                          boolean tryToValidate)
                                   throws java.io.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:
java.io.FileNotFoundException

getURLs

public static java.util.List<java.net.URL> getURLs(java.lang.String pathspec,
                                                   java.net.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 java.net.URL getParent(java.net.URL url)
Parameters:
url - some URL
Returns:
the "parent" of the given URL, if possible

seemsToExist

public static boolean seemsToExist(java.net.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(java.net.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 java.lang.String escape(java.lang.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 java.lang.String unescape(java.lang.String url)
Reverses escape(String).

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

unescape

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


pathsAreEquivalent

public static boolean pathsAreEquivalent(java.lang.String path1,
                                         java.lang.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