KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > web > taglib > ApplResources


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/web/taglib/ApplResources.java#5 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2002-2002 Kana Software, Inc.
7 // Copyright (C) 2002-2005 Julian Hyde and others
8 // All Rights Reserved.
9 // You must accept the terms of that agreement to use this software.
10 //
11 // Andreas Voss, 22 March, 2002
12 */

13 package mondrian.web.taglib;
14
15 import javax.servlet.ServletContext JavaDoc;
16 import javax.servlet.ServletContextEvent JavaDoc;
17 import javax.xml.transform.Templates JavaDoc;
18 import javax.xml.transform.Transformer JavaDoc;
19 import javax.xml.transform.TransformerConfigurationException JavaDoc;
20 import javax.xml.transform.TransformerFactory JavaDoc;
21 import javax.xml.transform.stream.StreamSource JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 /**
26  * holds compiled stylesheets
27  */

28
29 public class ApplResources implements Listener.ApplicationContext {
30
31     private static final String JavaDoc ATTRNAME = "mondrian.web.taglib.ApplResources";
32     private ServletContext JavaDoc context;
33
34     /**
35      * Creates a <code>ApplResources</code>. Only {@link Listener} calls this;
36      * you should probably call {@link #getInstance}.
37      */

38     public ApplResources() {
39     }
40
41     /**
42      * Retrieves the one and only instance of <code>ApplResources</code> in
43      * this servlet's context.
44      */

45     public static ApplResources getInstance(ServletContext JavaDoc context) {
46         return (ApplResources)context.getAttribute(ATTRNAME);
47     }
48
49     private HashMap JavaDoc templatesCache = new HashMap JavaDoc();
50     public Transformer JavaDoc getTransformer(String JavaDoc xsltURI, boolean useCache) {
51         try {
52             Templates JavaDoc templates = null;
53             if (useCache)
54                 templates = (Templates JavaDoc)templatesCache.get(xsltURI);
55             if (templates == null) {
56                 TransformerFactory JavaDoc tf = TransformerFactory.newInstance();
57                 InputStream JavaDoc input = context.getResourceAsStream(xsltURI);
58                 templates = tf.newTemplates(new StreamSource JavaDoc(input));
59                 if (useCache)
60                     templatesCache.put(xsltURI, templates);
61             }
62             return templates.newTransformer();
63         }
64         catch (TransformerConfigurationException JavaDoc e) {
65             e.printStackTrace();
66             throw new RuntimeException JavaDoc(e.toString());
67         }
68     }
69
70     // implement ApplicationContext
71
public void init(ServletContextEvent JavaDoc event) {
72         this.context = event.getServletContext();
73         context.setAttribute(ATTRNAME, this);
74     }
75
76     public void destroy(ServletContextEvent JavaDoc ev) {
77     }
78
79
80 }
81
82 // End ApplResources.java
83
Popular Tags