org.cdlib.xtf.xslt
Class FileUtils

Object
  extended by FileUtils

public class FileUtils
extends Object

Provides file-related utilities to be called by XSLT stylesheets through Saxon's extension function mechanism.

Author:
Martin Haye

Field Summary
private static HashMap dateFormatCache
          Used to avoid recreating SimpleDateFormat objects all the time
private static long MILLISECS_PER_DAY
          Number of leap seconds per day except on
1. days when a leap second has been inserted, e.g. 1999 JAN 1.
private static long MILLISECS_PER_HOUR
          Number of milliseconds per hour, except when a leap second is inserted.
private static long MILLISECS_PER_MINUTE
          All minutes have this many milliseconds except the last minute of the day on a day defined with a leap second.
private static ThreadLocal<ArrayList<File>> tempFiles
          Used to track temp files, per thread
 
Constructor Summary
FileUtils()
           
 
Method Summary
private static long adjustedMillis(Calendar cal)
          Gets the time in milliseconds from a Calendar, adjusting for timezone so that day subtraction works properly.
static String createTempFile(XPathContext context, String prefix, String suffix)
          Generates a temporary file in the default temporary-file directory, using the given prefix and suffix to generate the name.
static String curDateTime(XPathContext context, String formatStr)
          Gets the current date and time.
static void deleteTempFiles()
          Deletes all temporary files created by the current thread using createTempFile(net.sf.saxon.expr.XPathContext, java.lang.String, java.lang.String).
static boolean exists(XPathContext context, String filePath)
          Checks whether a file with the given path exists (that is, if it can be read.)
private static SimpleDateFormat getDateFormat(String formatStr)
          Get a SimpleDateFormatter for the given format string.
private static String getSystemId(XPathContext context)
          Unfortunately the interface for getting systemId from an XPath context changed between Saxon 9.0 and Saxon 9.1, so we jump through hoops to be compatible with both.
static String humanFileSize(Long longFileSize)
          Converts the size of a file to a human-readable string, e.g
static String lastModified(XPathContext context, String filePath, String formatStr)
          Gets the last-modified time of the file with the given path exists (that is, if it can be read.)
static long length(XPathContext context, String filePath)
          Gets the size in bytes of the file with the given path (that is, if it can be read.)
static String md5Hash(String inputString)
          Calculate the MD5 digest of a string.
static DocumentInfo readHTMLPage(XPathContext context, String urlStr)
          Reads in an HTML page (specified by URL), and uses JTidy to make it into XML that can be subsequently processed by a stylesheet.
static DocumentInfo readXMLStub(XPathContext context, String filePath)
          Reads in the first part of an XML file, stopping at the first close-element marker.
static File resolveFile(XPathContext context, String filePath)
          Resolve the location of a file given the stylesheet context.
static String resolvePath(XPathContext context, String filePath)
          Resolve the location of a file given the stylesheet context.
static long timeSince(XPathContext context, String targetDateStr, String units, String formatStr)
          Compute the number of days, hours, or minutes that have elapsed between the given time and now.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dateFormatCache

private static HashMap dateFormatCache
Used to avoid recreating SimpleDateFormat objects all the time


tempFiles

private static ThreadLocal<ArrayList<File>> tempFiles
Used to track temp files, per thread


MILLISECS_PER_MINUTE

private static final long MILLISECS_PER_MINUTE
All minutes have this many milliseconds except the last minute of the day on a day defined with a leap second.

See Also:
Constant Field Values

MILLISECS_PER_HOUR

private static final long MILLISECS_PER_HOUR
Number of milliseconds per hour, except when a leap second is inserted.

See Also:
Constant Field Values

MILLISECS_PER_DAY

private static final long MILLISECS_PER_DAY
Number of leap seconds per day except on
1. days when a leap second has been inserted, e.g. 1999 JAN 1.
2. Daylight-savings "spring forward" or "fall back" days.

See Also:
Constant Field Values
Constructor Detail

FileUtils

public FileUtils()
Method Detail

exists

public static boolean exists(XPathContext context,
                             String filePath)
Checks whether a file with the given path exists (that is, if it can be read.) If the path is relative, it is resolved relative to the stylesheet calling this function.

Parameters:
context - Context used to figure out which stylesheet is calling the function.
filePath - Path to the file in question
Returns:
true if the file exists and can be read, else false

lastModified

public static String lastModified(XPathContext context,
                                  String filePath,
                                  String formatStr)
Gets the last-modified time of the file with the given path exists (that is, if it can be read.) If the path is relative, it is resolved relative to the stylesheet calling this function.

Parameters:
context - Context used to figure out which stylesheet is calling the function.
filePath - Path to the file in question
formatStr - A simple format string; see SimpleDateFormat.
Returns:
The formatted date/time if the file exists; null if the file doesn't exist.

length

public static long length(XPathContext context,
                          String filePath)
Gets the size in bytes of the file with the given path (that is, if it can be read.) If the path is relative, it is resolved relative to the stylesheet calling this function.

Parameters:
context - Context used to figure out which stylesheet is calling the function.
filePath - Path to the file in question
Returns:
The file size, or -1 if it doesn't exist.

humanFileSize

public static String humanFileSize(Long longFileSize)
Converts the size of a file to a human-readable string, e.g. "36 Kb", "1.2 Mb", etc. Contributor: Michael A. Russell

Parameters:
longFileSize - The size to convert
Returns:
Human-readable string approximating that size.

md5Hash

public static String md5Hash(String inputString)
Calculate the MD5 digest of a string. Contributor: Michael A. Russell

Parameters:
inputString - String to digest
Returns:
The string's MD5 hash

getSystemId

private static String getSystemId(XPathContext context)
Unfortunately the interface for getting systemId from an XPath context changed between Saxon 9.0 and Saxon 9.1, so we jump through hoops to be compatible with both.


resolveFile

public static File resolveFile(XPathContext context,
                               String filePath)
Resolve the location of a file given the stylesheet context.


resolvePath

public static String resolvePath(XPathContext context,
                                 String filePath)
Resolve the location of a file given the stylesheet context. If the path is absolute, nothing is done. If it is relative, it is converted to absolute by resolving it relative to the stylesheet path.


curDateTime

public static String curDateTime(XPathContext context,
                                 String formatStr)
Gets the current date and time.

Parameters:
context - Context used to figure out which stylesheet is calling the function.
formatStr - A simple format string; see SimpleDateFormat.
Returns:
The formatted date/time.

timeSince

public static long timeSince(XPathContext context,
                             String targetDateStr,
                             String units,
                             String formatStr)
Compute the number of days, hours, or minutes that have elapsed between the given time and now.

Parameters:
context - Context used to figure out which stylesheet is calling the function.
targetDateStr - The target date
units - Units to return: 'days', 'hours', or 'minutes'. Plural is optional, and single-letter abbreviations are accepted.
formatStr - The format of the target date; see SimpleDateFormat.
Returns:
number of days, hours or minutes elapsed

adjustedMillis

private static long adjustedMillis(Calendar cal)
Gets the time in milliseconds from a Calendar, adjusting for timezone so that day subtraction works properly.


getDateFormat

private static SimpleDateFormat getDateFormat(String formatStr)
Get a SimpleDateFormatter for the given format string. If one has already been created, use that; otherwise, make a new one.

Parameters:
formatStr - is the format string to use
Returns:
a SimpleDateFormatter for that format string.

createTempFile

public static String createTempFile(XPathContext context,
                                    String prefix,
                                    String suffix)
                             throws IOException
Generates a temporary file in the default temporary-file directory, using the given prefix and suffix to generate the name. Also registers the file for deletion at the end of the current request.

Parameters:
context - Context used to figure out which stylesheet is calling the function.
prefix - Prefix for the resulting file name.
suffix - Suffix for the resulting file name.
Returns:
The new temporary file name.
Throws:
IOException

deleteTempFiles

public static void deleteTempFiles()
Deletes all temporary files created by the current thread using createTempFile(net.sf.saxon.expr.XPathContext, java.lang.String, java.lang.String).


readXMLStub

public static DocumentInfo readXMLStub(XPathContext context,
                                       String filePath)
                                throws IOException,
                                       XPathException
Reads in the first part of an XML file, stopping at the first close-element marker. Generally this captures enough information to identify which kind of XML data is inside the file.

Throws:
IOException - if the file can't be read
XPathException - if the document cannot be parsed

readHTMLPage

public static DocumentInfo readHTMLPage(XPathContext context,
                                        String urlStr)
                                 throws IOException,
                                        XPathException
Reads in an HTML page (specified by URL), and uses JTidy to make it into XML that can be subsequently processed by a stylesheet.

Throws:
IOException - if the file can't be read
XPathException - if the document cannot be parsed