StylesheetCache.javaAPI DocExample2851Sun Sep 02 14:59:04 BST 2001com.oreilly.javaxslt.util


public class StylesheetCache extends Object
A utility class that caches XSLT stylesheets in memory.
Eric M. Burke
$Id:,v 1.1 2001/01/25 03:46:09 ericb Exp $

Fields Summary
private static Map
Constructors Summary
private StylesheetCache()

Methods Summary
public static synchronized voidflush(java.lang.String xsltFileName)
Flush a specific cached stylesheet from memory.

xsltFileName the file name of the stylesheet to remove.

public static synchronized voidflushAll()
Flush all cached stylesheets from memory, emptying the cache.

public static synchronized javax.xml.transform.TransformernewTransformer(java.lang.String xsltFileName)
Obtain a new Transformer instance for the specified XSLT file name. A new entry will be added to the cache if this is the first request for the specified file name.

xsltFileName the file name of an XSLT stylesheet.
a transformation context for the given stylesheet.

        File xsltFile = new File(xsltFileName);

        // determine when the file was last modified on disk
        long xslLastModified = xsltFile.lastModified();
        MapEntry entry = (MapEntry) cache.get(xsltFileName);

        if (entry != null) {
            // if the file has been modified more recently than the
            // cached stylesheet, remove the entry reference
            if (xslLastModified > entry.lastModified) {
                entry = null;

        // create a new entry in the cache if necessary
        if (entry == null) {
            Source xslSource = new StreamSource(xsltFile);

            TransformerFactory transFact = TransformerFactory.newInstance();
            Templates templates = transFact.newTemplates(xslSource);

            entry = new MapEntry(xslLastModified, templates);
            cache.put(xsltFileName, entry);

        return entry.templates.newTransformer();