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

StylesheetCache

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

Fields Summary
private static Map
cache
Constructors Summary
private StylesheetCache()

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

param
xsltFileName the file name of the stylesheet to remove.

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


                  
         
        cache.clear();
    
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.

param
xsltFileName the file name of an XSLT stylesheet.
return
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();