KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > util > DefObjectFactory


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

24 public class DefObjectFactory extends ReflectionFactory {
25   private Map JavaDoc _defs = new HashMap JavaDoc();
26
27   /**
28    * Constructor for ReflectObjectFactory.
29    */

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

39   public void addDef(Def def) throws ConfigurationException {
40     if (_defs.get(def.getName()) != null) {
41       throw new ConfigurationException("Definition already declared: " +
42         def.getName() + ", " + def.getClazz());
43     }
44
45     _defs.put(def.getName(), def);
46   }
47
48   /**
49    * @see org.sapia.util.xml.confix.ObjectFactoryIF#newObjectFor(String, String, String, Object)
50    */

51   public CreationStatus newObjectFor(String JavaDoc prefix, String JavaDoc uri, String JavaDoc name,
52     Object JavaDoc parent) throws ObjectCreationException {
53     if ((prefix == null) || (prefix.length() == 0)) {
54       return super.newObjectFor(prefix, uri, name, parent);
55     }
56
57     Def def = (Def) _defs.get(name);
58
59     if (def == null) {
60       throw new ObjectCreationException("Unknown element: " + name);
61     } else {
62       try {
63         return CreationStatus.create(Class.forName(def.getClazz()).newInstance());
64       } catch (Exception JavaDoc e) {
65         throw new ObjectCreationException("Could not create object for " +
66           name, e);
67       }
68     }
69   }
70 }
71
Popular Tags