KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.validator.config;
2
3 import org.sapia.util.xml.confix.CompositeObjectFactory;
4 import org.sapia.util.xml.confix.CreationStatus;
5 import org.sapia.util.xml.confix.ObjectCreationException;
6 import org.sapia.util.xml.confix.ObjectFactoryIF;
7 import org.sapia.util.xml.confix.ReflectionFactory;
8 import org.sapia.validator.Defs;
9 import org.sapia.validator.Namespace;
10 import org.sapia.validator.RuleRef;
11 import org.sapia.validator.RuleSet;
12 import org.sapia.validator.RuleSetRef;
13 import org.sapia.validator.Vlad;
14
15 import java.util.*;
16
17 /**
18  * Instantiates Java objects corresponding to the various Vlad configuration
19  * files (rule definitions and rulesets).
20  *
21  * @author Yanick Duchesne
22  * <dl>
23  * <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>
24  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
25  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
26  * </dl>
27  */

28 public class VladObjectFactory implements ObjectFactoryIF {
29   private CompositeObjectFactory _comp = new CompositeObjectFactory();
30   private List _factories = new ArrayList();
31   private List _listeners = new ArrayList();
32   private Vlad _vlad;
33
34   /**
35    * Constructor for VladObjectFactory.
36    */

37   public VladObjectFactory( /*String[] packages*/
38   ) {
39     _factories.add(new ReflectionFactory(new String JavaDoc[0]));
40   }
41
42   public VladObjectFactory(Vlad vlad /*, String[] packages*/) {
43     this();
44     _vlad = vlad;
45   }
46
47   public void registerDef(String JavaDoc prefix, String JavaDoc localName, Def def) {
48     if (_vlad == null) {
49       throw new IllegalStateException JavaDoc("Vlad not set");
50     }
51
52     XmlObjectFactory fac;
53
54     if (_comp.containsObjectFactory(prefix)) {
55       fac = (XmlObjectFactory) _comp.getFactoryFor(prefix);
56     } else {
57       fac = new XmlObjectFactory(_vlad);
58       _comp.registerFactory(prefix, fac);
59     }
60
61     fac.registerDef(localName, def);
62   }
63
64   public void addFactoryFor(String JavaDoc[] packages) {
65     _factories.add(new ReflectionFactory(packages));
66   }
67
68   /**
69    * @see org.sapia.util.xml.confix.ObjectFactoryIF#newObjectFor(String, String, String, Object)
70    */

71   public CreationStatus newObjectFor(String JavaDoc prefix, String JavaDoc uri,
72     String JavaDoc localName, Object JavaDoc parent)
73     throws ObjectCreationException {
74     if ((prefix != null) && (prefix.length() != 0)) {
75       return getFactoryFor(prefix).newObjectFor(prefix, uri, localName, parent);
76     }
77
78     ObjectFactoryIF fac;
79
80     for (int i = 0; i < _factories.size(); i++) {
81       fac = (ObjectFactoryIF) _factories.get(i);
82
83       try {
84         return fac.newObjectFor(prefix, uri, localName, parent);
85       } catch (Throwable JavaDoc e) {
86         // noop
87
}
88     }
89
90     if (localName.equalsIgnoreCase("vlad")) {
91       return CreationStatus.create(_vlad);
92     } else if (localName.equalsIgnoreCase("namespace")) {
93       return CreationStatus.create(new Namespace()).assigned(false);
94     } else if (localName.equalsIgnoreCase("ruleset")) {
95       return CreationStatus.create(new RuleSet()).assigned(false);
96     } else if (localName.equalsIgnoreCase("ruleSetRef")) {
97       return CreationStatus.create(new RuleSetRef()).assigned(false);
98     } else if (localName.equalsIgnoreCase("ruleRef")) {
99       return CreationStatus.create(new RuleRef()).assigned(false);
100     } else if (localName.equalsIgnoreCase("defs")) {
101       return CreationStatus.create(new Defs(this)).assigned(false);
102     }
103
104     String JavaDoc name;
105
106     if (prefix != null) {
107       name = prefix + ':' + localName;
108     } else {
109       name = localName;
110     }
111
112     throw new ObjectCreationException("Could not creation object for: " + name);
113   }
114
115   protected ObjectFactoryIF getFactoryFor(String JavaDoc prefix)
116     throws IllegalArgumentException JavaDoc {
117     ObjectFactoryIF fac = (ObjectFactoryIF) _comp.getFactoryFor(prefix);
118
119     if (fac == null) {
120       throw new IllegalStateException JavaDoc("No factory for prefix: " + prefix);
121     }
122
123     return fac;
124   }
125 }
126
Popular Tags