KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > validator > config > XmlObjectFactory


1 package org.sapia.validator.config;
2
3 import org.sapia.util.xml.confix.CreationStatus;
4 import org.sapia.util.xml.confix.ObjectCreationException;
5 import org.sapia.util.xml.confix.ObjectFactoryIF;
6 import org.sapia.validator.Rule;
7 import org.sapia.validator.Vlad;
8
9 import java.util.*;
10
11 /**
12  * Creates rule instances based on the prefix:ruleName that is parsed
13  * from ruleset configurations.
14  *
15  * @author Yanick Duchesne
16  * <dl>
17  * <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>
18  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
19  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
20  * </dl>
21  */

22 public class XmlObjectFactory implements ObjectFactoryIF {
23   private Map _defs = new HashMap();
24   private Vlad _vlad;
25
26   /**
27    * Constructor for XmlObjectFactory.
28    */

29   public XmlObjectFactory(Vlad vlad) {
30     _vlad = vlad;
31   }
32
33   /**
34    * @see org.sapia.util.xml.confix.ObjectFactoryIF#newObjectFor(String, String, String, Object)
35    */

36   public CreationStatus newObjectFor(String JavaDoc prefix, String JavaDoc uri,
37     String JavaDoc localName, Object JavaDoc parent)
38     throws ObjectCreationException {
39     Def def = (Def) _defs.get(localName);
40
41     if (def == null) {
42       throw new ObjectCreationException("No definition for: " + prefix + ":"
43         + localName);
44     }
45
46     try {
47       Object JavaDoc toReturn = def.toInstance();
48       if(toReturn instanceof Rule){
49         ((Rule)toReturn).initName(prefix, localName);
50       }
51       return CreationStatus.create(toReturn);
52
53       // if(obj instanceof Rule){
54
// _vlad.addRule((Rule)obj);
55
// }
56
// return CreationStatus.create(obj);
57
} catch (ConfigException e) {
58       throw new ObjectCreationException(
59         "Could not create object from definition for: " + prefix + ":"
60         + localName, e);
61     }
62   }
63
64   /**
65    *
66    */

67   public void registerDef(String JavaDoc localName, Def def)
68     throws IllegalArgumentException JavaDoc {
69     if (_defs.get(localName) != null) {
70       throw new IllegalArgumentException JavaDoc(
71         "A definition already exists for the given name: " + localName);
72     }
73
74     _defs.put(localName, def);
75   }
76 }
77
Popular Tags