KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > gumby > factory > GumbyObjectFactory


1 package org.sapia.gumby.factory;
2
3 import java.util.List JavaDoc;
4
5 import org.sapia.gumby.RenderContext;
6 import org.sapia.gumby.RenderContextFactory;
7 import org.sapia.util.xml.confix.CompositeObjectFactory;
8 import org.sapia.util.xml.confix.ConfigurationException;
9 import org.sapia.util.xml.confix.CreationStatus;
10 import org.sapia.util.xml.confix.ObjectCreationException;
11 import org.sapia.util.xml.confix.ReflectionFactory;
12
13 /**
14  * @author Yanick Duchesne
15  *
16  * <dl>
17  * <dt><b>Copyright: </b>
18  * <dd>Copyright &#169; 2002-2005 <a HREF="http://www.sapia-oss.org">Sapia Open
19  * Source Software </a>. All Rights Reserved.</dd>
20  * </dt>
21  * <dt><b>License: </b>
22  * <dd>Read the license.txt file of the jar or visit the <a
23  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
24  * OSS web site</dd>
25  * </dt>
26  * </dl>
27  */

28 public class GumbyObjectFactory extends CompositeObjectFactory {
29
30   private static final String JavaDoc SWING_PREFIX = "swing";
31   private static final String JavaDoc GUMBY_PREFIX = "gumby";
32
33   private static final String JavaDoc TAG_NAMESPACE = "namespace";
34   private static final String JavaDoc TAG_CONFIG = "config";
35
36   private ReflectionFactory _invoker = new ReflectionFactory(
37                                                 new String JavaDoc[0]);
38   private GumbyObjectFactory _parent;
39   private RenderContext _ctx;
40
41   public GumbyObjectFactory() {
42     super.setMapToPrefix(true);
43     super.registerFactory(SWING_PREFIX, new SwingFactory());
44     _ctx = RenderContextFactory.newInstance();
45   }
46
47   public GumbyObjectFactory(GumbyObjectFactory parent, RenderContext context) {
48     super.setMapToPrefix(true);
49     super.registerFactory(SWING_PREFIX, new SwingFactory());
50     _ctx = context;
51     _parent = parent;
52   }
53
54   /**
55    * @see org.sapia.util.xml.confix.CompositeObjectFactory#newObjectFor(java.lang.String,
56    * java.lang.String, java.lang.String, java.lang.Object)
57    */

58   public synchronized CreationStatus newObjectFor(String JavaDoc prefix, String JavaDoc uri,
59       String JavaDoc elementName, Object JavaDoc parent) throws ObjectCreationException {
60
61     CreationStatus stat = doNewObjectFor(prefix, uri, elementName, parent);
62     if(stat.getCreated() instanceof ContextAware) {
63       ((ContextAware) stat.getCreated()).handleContext(_ctx);
64     }
65     if(stat.getCreated() instanceof ParentAware) {
66       ((ParentAware) stat.getCreated()).handleParent(parent);
67     }
68     return stat;
69   }
70
71   public synchronized void registerDefs(Namespace defs)
72       throws ConfigurationException {
73     NamespacedFactory fac;
74
75     if(super.containsObjectFactory(defs.getPrefix())) {
76       fac = (NamespacedFactory) super.getFactoryFor(defs.getPrefix());
77     } else {
78       super.registerFactory(defs.getPrefix(), fac = new NamespacedFactory());
79     }
80
81     Def def;
82     List JavaDoc lst = defs.getDefs();
83
84     for(int i = 0; i < lst.size(); i++) {
85       def = (Def) lst.get(i);
86       fac.addDef(def);
87     }
88   }
89
90   private CreationStatus doNewObjectFor(String JavaDoc prefix, String JavaDoc uri,
91       String JavaDoc elementName, Object JavaDoc parent) throws ObjectCreationException {
92     if(_parent != null && prefix != null && prefix.length() > 0) {
93       if(_parent.containsObjectFactory(prefix)) {
94         try {
95           return _parent.getFactoryFor(prefix).newObjectFor(prefix, uri,
96               elementName, parent);
97         } catch(ObjectCreationException e) {
98         }
99       }
100     }
101     if(prefix == null || prefix.length() == 0) {
102       return _invoker.newObjectFor(prefix, uri, elementName, parent);
103     } else if(prefix.equals(GUMBY_PREFIX)) {
104       if(elementName.equals(TAG_NAMESPACE)) {
105         return CreationStatus.create(new Namespace(this)).assigned(true);
106       } else if(elementName.equals(TAG_CONFIG)) {
107         return CreationStatus.create(new Config()).assigned(true);
108       }
109       return super.newObjectFor(prefix, uri, elementName, parent);
110     } else {
111       return super.newObjectFor(prefix, uri, elementName, parent);
112     }
113   }
114
115 }
116
Popular Tags