1 13 package mondrian.web.taglib; 14 15 import javax.servlet.ServletContext ; 16 import javax.servlet.ServletContextEvent ; 17 import javax.xml.transform.Templates ; 18 import javax.xml.transform.Transformer ; 19 import javax.xml.transform.TransformerConfigurationException ; 20 import javax.xml.transform.TransformerFactory ; 21 import javax.xml.transform.stream.StreamSource ; 22 import java.io.InputStream ; 23 import java.util.HashMap ; 24 25 28 29 public class ApplResources implements Listener.ApplicationContext { 30 31 private static final String ATTRNAME = "mondrian.web.taglib.ApplResources"; 32 private ServletContext context; 33 34 38 public ApplResources() { 39 } 40 41 45 public static ApplResources getInstance(ServletContext context) { 46 return (ApplResources)context.getAttribute(ATTRNAME); 47 } 48 49 private HashMap templatesCache = new HashMap (); 50 public Transformer getTransformer(String xsltURI, boolean useCache) { 51 try { 52 Templates templates = null; 53 if (useCache) 54 templates = (Templates )templatesCache.get(xsltURI); 55 if (templates == null) { 56 TransformerFactory tf = TransformerFactory.newInstance(); 57 InputStream input = context.getResourceAsStream(xsltURI); 58 templates = tf.newTemplates(new StreamSource (input)); 59 if (useCache) 60 templatesCache.put(xsltURI, templates); 61 } 62 return templates.newTransformer(); 63 } 64 catch (TransformerConfigurationException e) { 65 e.printStackTrace(); 66 throw new RuntimeException (e.toString()); 67 } 68 } 69 70 public void init(ServletContextEvent event) { 72 this.context = event.getServletContext(); 73 context.setAttribute(ATTRNAME, this); 74 } 75 76 public void destroy(ServletContextEvent ev) { 77 } 78 79 80 } 81 82 | Popular Tags |