001    /*
002    // $Id: //open/mondrian/src/main/mondrian/web/taglib/ApplResources.java#6 $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.html.
006    // Copyright (C) 2002-2002 Kana Software, Inc.
007    // Copyright (C) 2002-2008 Julian Hyde and others
008    // All Rights Reserved.
009    // You must accept the terms of that agreement to use this software.
010    //
011    // Andreas Voss, 22 March, 2002
012    */
013    package mondrian.web.taglib;
014    
015    import javax.servlet.ServletContext;
016    import javax.servlet.ServletContextEvent;
017    import javax.xml.transform.Templates;
018    import javax.xml.transform.Transformer;
019    import javax.xml.transform.TransformerConfigurationException;
020    import javax.xml.transform.TransformerFactory;
021    import javax.xml.transform.stream.StreamSource;
022    import java.io.InputStream;
023    import java.util.HashMap;
024    
025    /**
026     * holds compiled stylesheets
027     */
028    
029    public class ApplResources implements Listener.ApplicationContext {
030    
031        private static final String ATTRNAME = "mondrian.web.taglib.ApplResources";
032        private ServletContext context;
033    
034        /**
035         * Creates a <code>ApplResources</code>. Only {@link Listener} calls this;
036         * you should probably call {@link #getInstance}.
037         */
038        public ApplResources() {
039        }
040    
041        /**
042         * Retrieves the one and only instance of <code>ApplResources</code> in
043         * this servlet's context.
044         */
045        public static ApplResources getInstance(ServletContext context) {
046            return (ApplResources)context.getAttribute(ATTRNAME);
047        }
048    
049        private HashMap templatesCache = new HashMap();
050        public Transformer getTransformer(String xsltURI, boolean useCache) {
051            try {
052                Templates templates = null;
053                if (useCache)
054                    templates = (Templates)templatesCache.get(xsltURI);
055                if (templates == null) {
056                    TransformerFactory tf = TransformerFactory.newInstance();
057                    InputStream input = context.getResourceAsStream(xsltURI);
058                    templates = tf.newTemplates(new StreamSource(input));
059                    if (useCache)
060                        templatesCache.put(xsltURI, templates);
061                }
062                return templates.newTransformer();
063            } catch (TransformerConfigurationException e) {
064                e.printStackTrace();
065                throw new RuntimeException(e.toString());
066            }
067        }
068    
069        // implement ApplicationContext
070        public void init(ServletContextEvent event) {
071            this.context = event.getServletContext();
072            context.setAttribute(ATTRNAME, this);
073        }
074    
075        public void destroy(ServletContextEvent ev) {
076        }
077    
078    
079    }
080    
081    // End ApplResources.java