KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > gumby > RenderContextFactory


1 package org.sapia.gumby;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5
6 import org.sapia.gumby.factory.GumbyObjectFactory;
7 import org.sapia.util.xml.confix.Dom4jProcessor;
8
9 /**
10  * This class is a factory of <code>RenderContext</code> instances. Ideally,
11  * only the first context acquired by an application should be created by this
12  * class. Other contexts should be created as children of that first context,
13  * and so on.
14  * <p>
15  * The above approach is a nice way to have a context that is shared globally,
16  * and child contexts that are intented to hold state that is specific to some
17  * parts of the application.
18  *
19  * @author Yanick Duchesne
20  *
21  * <dl>
22  * <dt><b>Copyright: </b>
23  * <dd>Copyright &#169; 2002-2005 <a HREF="http://www.sapia-oss.org">Sapia Open
24  * Source Software </a>. All Rights Reserved.</dd>
25  * </dt>
26  * <dt><b>License: </b>
27  * <dd>Read the license.txt file of the jar or visit the <a
28  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
29  * OSS web site</dd>
30  * </dt>
31  * </dl>
32  */

33 public class RenderContextFactory {
34
35   private static GumbyObjectFactory _defaults;
36
37   static {
38     if(_defaults == null) {
39       GumbyObjectFactory defaults = new GumbyObjectFactory();
40       try {
41         loadDefaults(defaults);
42         _defaults = defaults;
43       } catch(Exception JavaDoc e) {
44         e.printStackTrace();
45       }
46     }
47   }
48
49   /**
50    * Returns a new context.
51    *
52    * @return a <code>RenderContext</code> instance.
53    */

54   public static RenderContext newInstance() {
55     return new RenderContext(_defaults, new DefaultGuiEnv(), new Settings());
56   }
57
58   /**
59    * Returns a new context that will encapsulate the given environment.
60    *
61    * @param env
62    * a <code>GuiEnv</code> instance.
63    * @return a <code>RenderContext</code>.
64    */

65   public static RenderContext newInstance(GuiEnv env) {
66     return new RenderContext(_defaults, env, new Settings());
67   }
68
69   /**
70    * Loads object definitions that will be available to all
71    * <code>RenderContext</code> instances.
72    *
73    * @param is
74    * an <code>InputStream</code>.
75    * @throws Exception
76    * if a problem occurs while loading the definitions.
77    */

78   public static void loadDefinitions(InputStream JavaDoc is) throws Exception JavaDoc {
79     if(_defaults != null) {
80       doLoad(_defaults, is);
81     }
82   }
83
84   private static void loadDefaults(GumbyObjectFactory fac) throws Exception JavaDoc {
85     doLoad(fac, RenderContextFactory.class
86         .getResourceAsStream("gumby_defs.xml"));
87   }
88
89   private static void doLoad(GumbyObjectFactory fac, InputStream JavaDoc is)
90       throws Exception JavaDoc {
91     if(is != null) {
92       try {
93         Dom4jProcessor proc = new Dom4jProcessor(fac);
94         proc.process(new Object JavaDoc(), is);
95       } finally {
96         try {
97           is.close();
98         } catch(IOException JavaDoc e) {
99         }
100       }
101     }
102   }
103 }
104
Popular Tags