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 ThreadLocal<ArrayList<File>> tempFiles
          Used to track temp files, per thread
 
Constructor Summary
FileUtils()
           
 
Method Summary
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.
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 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.
private 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.
 
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

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.

resolveFile

private 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.

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