package com.bgsag.xml;
import java.io.*;
import java.util.Hashtable;

// Imported TraX classes
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Templates;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;

public class StylesheetCache {
TransformerFactory processor = TransformerFactory.newInstance();
private Hashtable cache = new Hashtable();

public synchronized Templates tryCache(String url, String reload) throws TransformerException, java.io.IOException {

        Templates x = (Templates)cache.get(url);
        if (x==null || (reload!=null && reload.equals("true"))) {
            x = processor.newTemplates(new StreamSource(url));
            cache.put(url, x);
        }
        return x;
    }

    /**
    * Clear the cache. Useful if stylesheets have been modified, or simply if space is
    * running low. We let the garbage collector do the work.
    */

public synchronized void clearCache() {
        cache = new Hashtable();
    }

}