KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > validator > Vlad


1 package org.sapia.validator;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileNotFoundException JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.util.Collections JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Locale JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.sapia.util.xml.ProcessingException;
14 import org.sapia.util.xml.confix.ConfigurationException;
15 import org.sapia.util.xml.confix.Dom4jProcessor;
16 import org.sapia.util.xml.confix.ObjectHandlerIF;
17 import org.sapia.validator.config.ConfigException;
18 import org.sapia.validator.config.Def;
19 import org.sapia.validator.config.VladObjectFactory;
20
21 /**
22  * An instance of this class encapsulates rule sets and rules. An
23  * instance of this class is used as such:
24  *
25  * <pre>
26  * Vlad v = new Vlad()
27  * .loadDefs("someRuleDefs.xml")
28  * .loadDefs("someOtherRuleDefs.xml")
29  * .load("someRuleSets.xml")
30  * .load("someOtherRuleSets.xml");
31  * </pre>
32  *
33  * @see org.sapia.validator.RuleSet
34  * @see org.sapia.validator.Rule
35  *
36  * @author Yanick Duchesne
37  * <dl>
38  * <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>
39  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
40  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
41  * </dl>
42  */

43 public class Vlad implements ObjectHandlerIF {
44   public static final String JavaDoc VLAD_RULES_XML = "vlad_rules.xml";
45   private Map JavaDoc _rules = new HashMap JavaDoc();
46   private Map JavaDoc _ruleSets = new HashMap JavaDoc();
47   private VladObjectFactory _factory;
48   private Map JavaDoc _globals = new HashMap JavaDoc();
49   
50   /**
51    * Constructor for RuleConfig.
52    */

53   public Vlad() {
54     _factory = new VladObjectFactory(this);
55     
56     InputStream JavaDoc is = loadResource(VLAD_RULES_XML);
57     
58     if (is != null) {
59       Dom4jProcessor p = new Dom4jProcessor(_factory);
60       
61       try {
62         p.process(is);
63       } catch (ProcessingException e) {
64         e.printStackTrace();
65       }
66     }
67   }
68   
69   /**
70    * Adds a global object to this instance. Global values are kept as
71    * part of this instance and passed to every validation context (created
72    * when the <code>validate</code> method of this instance is called).
73    * <p>
74    * Use of this method should be done prior to using this instance for validation,
75    * at initialization time.
76    *
77    * @param name the name under which the given value should be kept.
78    * @param value an <code>Object</code>.
79    *
80    * @see ValidationContext
81    */

82   public synchronized void addGlobal(String JavaDoc name, Object JavaDoc value){
83     Map JavaDoc globals = new HashMap JavaDoc(_globals);
84     globals.put(name, value);
85     _globals = globals;
86   }
87   
88   /**
89    * @param values a <code>Map</code> of values to add to this instance's
90    * global values.
91    */

92   public synchronized void addGlobals(Map JavaDoc values){
93     Map JavaDoc globals = new HashMap JavaDoc(_globals);
94     globals.putAll(values);
95     _globals = globals;
96   }
97   
98   /**
99    * @param name the name of an expected global value.
100    *
101    * @return the <code>Object</code> corresponding to the given name.
102    */

103   public Object JavaDoc getGlobal(String JavaDoc name){
104     return _globals.get(name);
105   }
106   
107   /**
108    * @return an unmodifiable <code>Map</code> corresponding to the global values
109    * that this instance holds.
110    */

111   public Map JavaDoc getGlobals(){
112     return Collections.unmodifiableMap(_globals);
113   }
114   
115   
116   /**
117    * Adds a rule set to this instance.
118    *
119    * @param set a <code>RuleSet</code>.
120    */

121   public void addRuleSet(RuleSet set) {
122     _ruleSets.put(set.getId(), set);
123   }
124   
125   /**
126    * Returns the rule set corresponding to the given identifier.
127    *
128    * @return a <code>RuleSet</code>.
129    * @param id the identifier of the desired <code>RuleSet</code>.
130    *
131    * @throws IllegalArgumentException if no rule set was found for
132    * the given identifier.
133    */

134   public RuleSet getRuleSet(String JavaDoc id)
135   throws IllegalArgumentException JavaDoc {
136     RuleSet set = (RuleSet) _ruleSets.get(id);
137     
138     if (set == null) {
139       throw new IllegalArgumentException JavaDoc("No rule set for: " + id);
140     }
141     
142     return set;
143   }
144   
145   /**
146    * Adds a rule to this instance.
147    *
148    * @param a <code>Rule</code>.
149    */

150   public void add(Rule rule) {
151     if (rule.getId() == null) {
152       throw new IllegalArgumentException JavaDoc("rule " + rule.getClass().getName()
153       + " does not have an ID");
154     }
155     
156     _rules.put(rule.getId(), rule);
157   }
158   
159   /**
160    * Adds the given namespace to this instance.
161    *
162    * @param ns a <code>Namespace</code>.
163    */

164   public void addNamespace(Namespace ns) {
165     if (ns.getPrefix() == null) {
166       throw new IllegalArgumentException JavaDoc(
167        "Attribute 'prefix' not defined on 'namepsace' element");
168     }
169     
170     Def d;
171     
172     for (int i = 0; i < ns.getRuleDefs().size(); i++) {
173       d = (Def) ns.getRuleDefs().get(i);
174       _factory.registerDef(ns.getPrefix(), d.getName(), d);
175     }
176   }
177   
178   /**
179    * Returns the rule corresponding to the given ID.
180    *
181    * @param id the identifier of the desired <code>Rule</code>.
182    * @return a <code>Rule</code>.
183    * @throws IllegalArgumentException if no rule was found for the given
184    * identifier.
185    */

186   public Rule getRule(String JavaDoc id)
187   throws IllegalArgumentException JavaDoc {
188     Rule rule = (Rule) _rules.get(id);
189     
190     if (rule == null) {
191       throw new IllegalArgumentException JavaDoc("No rule for: " + id);
192     }
193     
194     return rule;
195   }
196   
197   /**
198    * Validates the given object, using the ruleset whose identifier
199    * is given, for the given <code>Locale</code>. The latter is used
200    * to select the proper error messages (in the language that closest
201    * match the given Locale).
202    *
203    * @param ruleSetId the identifier of a <code>RuleSet</code>.
204    * @param obj an object to validate.
205    * @param locale a <code>Locale</code>.
206    *
207    * @return a <code>Status</code>.
208    */

209   public Status validate(String JavaDoc ruleSetId, Object JavaDoc obj, Locale JavaDoc locale) {
210     RuleSet set = getRuleSet(ruleSetId);
211     ValidationContext ctx = new ValidationContext(_globals, obj, this, locale);
212
213     set.validate(ctx);
214     
215     return ctx.getStatus();
216   }
217   
218   /**
219   * Validates the given object, using the ruleset whose identifier
220   * is given, for the given <code>Locale</code>. The latter is used
221   * to select the proper error messages (in the language that closest
222   * matches the given Locale).
223   *
224   * @param ruleSetId the identifier of a <code>RuleSet</code>.
225   * @param obj an object to validate.
226   * @param locale a <code>Locale</code>.
227   * @param contextMap a <code>Map</code> with additional context objects which can be acquired
228   * from validation code.
229   *
230   * @return a <code>Status</code>.
231   */

232   public Status validate(String JavaDoc ruleSetId, Object JavaDoc obj, Locale JavaDoc locale, Map JavaDoc contextMap) {
233     RuleSet set = getRuleSet(ruleSetId);
234     ValidationContext ctx = new ValidationContext(_globals, obj, this, locale, contextMap);
235     
236     set.validate(ctx);
237     
238     return ctx.getStatus();
239   }
240   /**
241    * Loads a ruleset definition file as a resource. The file must in this
242    * case be present in the classpath.
243    *
244    * @param resource the name of a resource.
245    * @throws IOException if an IO error occurs while loading the resource, or
246    * if no resource exists for the given name.
247    * @throws ConfigException if a problem occurs while initializing this
248    * instance with the given resource's content.
249    */

250   public Vlad load(String JavaDoc resource)
251   throws IOException JavaDoc, ConfigException {
252     InputStream JavaDoc is = loadResource(resource);
253     
254     if (is == null) {
255       throw new FileNotFoundException JavaDoc(resource);
256     }
257     
258     return load(is);
259   }
260   
261   /**
262    * Loads a ruleset definition corresponding to the given
263    * file.
264    *
265    * @param f a <code>File</code>
266    * @throws IOException if an IO error occurs while loading the file.
267    * @throws ConfigException if a problem occurs while initializing this
268    * instance with the given file's content.
269    */

270   public Vlad load(File JavaDoc f) throws IOException JavaDoc, ConfigException {
271     return load(new FileInputStream JavaDoc(f));
272   }
273   
274   /**
275    * Loads a ruleset definition corresponding to the given
276    * stream.
277    *
278    * @param is an <code>InputStream</code>
279    * @throws IOException if an IO error occurs while loading the configuration stream.
280    * @throws ConfigException if a problem occurs while initializing this
281    * instance with the given stream's content.
282    */

283   public Vlad load(InputStream JavaDoc is)
284   throws IOException JavaDoc, ConfigException {
285     Dom4jProcessor p = new Dom4jProcessor(_factory);
286     
287     try {
288       p.process(is);
289       
290       return this;
291     } catch (ProcessingException e) {
292       throw new ConfigException("Could not process configuration", e);
293     }
294   }
295   
296   /**
297    * Loads a rule definition corresponding to the given
298    * file.
299    *
300    * @param f a <code>File</code>
301    * @throws IOException if an IO error occurs while loading the file.
302    * @throws ConfigException if a problem occurs while initializing this
303    * instance with the given file's content.
304    */

305   public Vlad loadDefs(File JavaDoc f)
306   throws IOException JavaDoc, ConfigException {
307     return loadDefs(new FileInputStream JavaDoc(f));
308   }
309   
310   
311   /**
312    * Loads a rule definition file as a resource. The file must in this
313    * case be present in the classpath.
314    *
315    * @param resource the name of a resource.
316    * @throws IOException if an IO error occurs while loading the resource, or
317    * if no resource exists for the given name.
318    * @throws ConfigException if a problem occurs while initializing this
319    * instance with the given resource's content.
320    */

321   
322   public Vlad loadDefs(String JavaDoc resource)
323   throws IOException JavaDoc, ConfigException {
324     InputStream JavaDoc is = loadResource(resource);
325     
326     if (is == null) {
327       throw new FileNotFoundException JavaDoc(resource);
328     }
329     
330     return loadDefs(is);
331   }
332   
333   /**
334    * Loads a rule definition corresponding to the given
335    * stream.
336    *
337    * @param is an <code>InputStream</code>
338    * @throws IOException if an IO error occurs while loading the configuration stream.
339    * @throws ConfigException if a problem occurs while initializing this
340    * instance with the given stream's content.
341    */

342   public Vlad loadDefs(InputStream JavaDoc is)
343   throws IOException JavaDoc, ConfigException {
344     if (is != null) {
345       Dom4jProcessor p = new Dom4jProcessor(_factory);
346       
347       try {
348         p.process(is);
349       } catch (ProcessingException e) {
350         throw new ConfigException("Could not load definitions", e);
351       }
352     }
353     
354     return this;
355   }
356   
357   /**
358    * @see org.sapia.util.xml.confix.ObjectHandlerIF#handleObject(String, Object)
359    */

360   public void handleObject(String JavaDoc name, Object JavaDoc ruleOrRuleSet)
361   throws ConfigurationException {
362     if (ruleOrRuleSet instanceof Rule) {
363       add((Rule) ruleOrRuleSet);
364     } else if (ruleOrRuleSet instanceof RuleSet) {
365       addRuleSet((RuleSet) ruleOrRuleSet);
366     } else {
367       throw new ConfigurationException("Unexpected element: " + name);
368     }
369   }
370   
371   private InputStream JavaDoc loadResource(String JavaDoc name) {
372     
373     InputStream JavaDoc is = Thread.currentThread().getContextClassLoader()
374     .getResourceAsStream(name);
375     
376     if (is == null) {
377       is = getClass().getClassLoader().getResourceAsStream(name);
378     }
379     
380     if (is == null) {
381       is = Vlad.class.getResourceAsStream(name);
382     }
383     
384     if (is == null) {
385       is = ClassLoader.getSystemResourceAsStream(name);
386     }
387     
388     return is;
389   }
390 }
391
Popular Tags