KickJava   Java API By Example, From Geeks To Geeks.

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


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

26 public class NamespacedFactory extends ReflectionFactory {
27
28   private Map JavaDoc _defs = new HashMap JavaDoc();
29
30   /**
31    * Constructor for ReflectObjectFactory.
32    */

33   public NamespacedFactory() {
34     super(new String JavaDoc[0]);
35   }
36
37   /**
38    * Adds the given definition to this instance.
39    *
40    * @param def
41    * a <code>Def</code> instance.
42    */

43   public void addDef(Def def) throws ConfigurationException {
44     if(_defs.get(def.getName()) != null) {
45       throw new ConfigurationException("Definition already declared: "
46           + def.getName() + ", " + def.getClassName());
47     }
48
49     _defs.put(def.getName(), def);
50   }
51
52   public boolean hasDefFor(String JavaDoc name) {
53     return _defs.get(name) != null;
54   }
55
56   /**
57    * @see org.sapia.util.xml.confix.ObjectFactoryIF#newObjectFor(String, String,
58    * String, Object)
59    */

60   public CreationStatus newObjectFor(String JavaDoc prefix, String JavaDoc uri, String JavaDoc name,
61       Object JavaDoc parent) throws ObjectCreationException {
62     if((prefix == null) || (prefix.length() == 0)) {
63       return super.newObjectFor(prefix, uri, name, parent);
64     }
65
66     Def def = (Def) _defs.get(name);
67
68     if(def == null) {
69       throw new ObjectCreationException("Unknown element: " + name);
70     } else {
71       try {
72         return CreationStatus.create(Class.forName(def.getClassName())
73             .newInstance());
74       } catch(Exception JavaDoc e) {
75         throw new ObjectCreationException(
76             "Could not create object for " + name, e);
77       }
78     }
79   }
80
81 }
82
Popular Tags