KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > validator > rules > IfDefined


1 package org.sapia.validator.rules;
2
3 import org.apache.commons.beanutils.PropertyUtils;
4 import org.sapia.util.xml.confix.ConfigurationException;
5 import org.sapia.util.xml.confix.ObjectHandlerIF;
6 import org.sapia.validator.Rule;
7 import org.sapia.validator.Validatable;
8 import org.sapia.validator.ValidationContext;
9
10 /**
11  * A rule implementation that will delegate its <code>validate()</code>
12  * call to its encapsulated rule, if the current object on the validation
13  * context's stack is not <code>null</code>, or if the accessor that is
14  * specified does not return <code>null</code>.
15  *
16  * @see #setAttribute(String)
17  *
18  * @author Yanick Duchesne
19  * <dl>
20  * <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>
21  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
22  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
23  * </dl>
24  */

25 public class IfDefined extends Rule implements ObjectHandlerIF{
26   private String JavaDoc _attribute;
27   private Validatable _validatable;
28
29   /**
30    * Constructor for If.
31    */

32   public IfDefined() {
33   }
34
35   /**
36    * Sets the name of the accessor that will be used to retreive
37    * a given value from the current object on the validation context's stack.
38    *
39    * @param attr the name of an accessor.
40    */

41   public void setAttribute(String JavaDoc attr) {
42     _attribute = attr;
43   }
44
45   /**
46    * @see org.sapia.util.xml.confix.ObjectHandlerIF#handleObject(String, Object)
47    */

48   public void handleObject(String JavaDoc anElementName, Object JavaDoc anObject)
49     throws ConfigurationException {
50     if (_validatable != null) {
51       throw new IllegalArgumentException JavaDoc(
52         qualifiedName() + " rule can only take one rule/ruleset");
53     }
54
55     if (!(anObject instanceof Validatable)) {
56       throw new IllegalArgumentException JavaDoc(
57         qualifiedName() + " rule only takes a validatable object");
58     }
59
60     _validatable = (Validatable) anObject;
61   }
62
63   /**
64    * @see org.sapia.validator.CompositeRule#validate(ValidationContext)
65    */

66   public void validate(ValidationContext ctx) {
67     Object JavaDoc item;
68
69     if (_attribute == null) {
70       item = ctx.get();
71     } else {
72       try {
73         if (ctx.get() == null) {
74           throw new IllegalStateException JavaDoc(
75             "No object on validation context stack at " + qualifiedName());
76         }
77
78         item = PropertyUtils.getProperty(ctx.get(), _attribute);
79       } catch (Throwable JavaDoc err) {
80         ctx.getStatus().error(this, err);
81
82         return;
83       }
84     }
85
86     if ((_validatable != null) && (item != null)) {
87       _validatable.validate(ctx);
88     }
89   }
90 }
91
Popular Tags