KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.validator;
2
3 import org.apache.commons.beanutils.PropertyUtils;
4
5 /**
6  * This abstract rule class provides the behavior that handles
7  * dynamic attributes (using the BeanUtils library from the
8  * Jakarta project).
9  * <p>
10  * This class allows to set the name of an accessor that will be
11  * used against the current object in the validation context
12  * stack. The accessor is invoked on the object, and the return
13  * value will be the object that is validated. If no accessor is
14  * specified, the current object on the stack will be validated.
15  * <p>
16  * If there is no object on the stack, or if the object returned
17  * by the accessor is <code>null</code>, an instance of this class
18  * can optionally throw an <code>IllegalStateException</code>; for this
19  * to occur, inheriting classes must call this class'
20  * <code>throwExceptionOnNull(true)</code> method.
21  * <p>
22  * Classes inheriting from this class must implement the <code>doValidate()</code>
23  * method, which is passed the object to validate. This implies that implementations
24  * must "know" what type of object to expect as a parameter.
25  *
26  * @see #validate(ValidationContext)
27  * @see #doValidate(Object)
28  * @see #throwExceptionOnNull(boolean)
29  *
30  * @author Yanick Duchesne
31  * <dl>
32  * <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>
33  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
34  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
35  * </dl>
36  */

37 public abstract class BeanRule extends Rule {
38   protected String JavaDoc _attribute;
39   private boolean _throw;
40   
41   /**
42    * Sets the name of the accessor that should be invoked on
43    * the current object in the validation context's stack.
44    *
45    * @param attr the name of the accessor to invoke.
46    * @see ValidationContext
47    */

48   public void setAttribute(String JavaDoc attr) {
49     _attribute = attr;
50   }
51
52   /**
53    * If called with <code>true</code> as an argument, indicates
54    * that this instance should throw an <code>IllegalStateException</code>
55    * if there is no object on the validation context's stack, or
56    * if the accessor invoked on the current object on stack returns
57    * <code>null</code>.
58    *
59    * @param throwExc if <code>true</code>, an <code>IllegalStateException</code>
60    * will be thrown by this instance's <code>validate()</code> method if
61    * there is no current object on stack, or if the accessor returns <code>null</code>.
62    */

63   protected void throwExceptionOnNull(boolean throwExc) {
64     _throw = throwExc;
65   }
66
67   /**
68    * If this instance was specified an accessor (through the
69    * <code>setAttribute()</code> method), the accessor is invoked
70    * on the current object in the given validation context.
71    * <p>
72    * If no accessor was specified, the current object remains.
73    * <p>
74    * If the current object is null (no object in stack), or if the accessor returns
75    * <code>null</code>, and if the instance's <code>throwExceptionOnNull(true)</code>
76    * method was called, then this method throws an <code>IllegalStateException</code>.
77    * <p>
78    * If no problem is detected, this method calls its <code>doValidate()<code> method,
79    * passing it either: a) the current object on the stack; or b) the object returned
80    * by the specified accessor.
81    * <p>
82    * If the <code>doValidate()</code> method returns <code>false</code>, this instance
83    * signals a validation error to the validation context's status.
84    *
85    * @see #setAttribute(String)
86    * @see #doValidate(Object)
87    *
88    * @see org.sapia.validator.Rule#validate(ValidationContext)
89    */

90   public final void validate(ValidationContext context) {
91     Object JavaDoc toValidate;
92
93     if (_attribute == null) {
94       toValidate = context.get();
95
96       if ((toValidate == null) && _throw) {
97         throw new IllegalStateException JavaDoc("No object on validation context stack at " + qualifiedName());
98       }
99     } else {
100       try {
101         toValidate = PropertyUtils.getProperty(context.get(), _attribute);
102
103         if ((toValidate == null) && _throw) {
104           throw new IllegalStateException JavaDoc("Attribute " + _attribute
105             + " evaluates to null on rule " + qualifiedName());
106         }
107       } catch (Throwable JavaDoc err) {
108         context.getStatus().error(this, err);
109
110         return;
111       }
112     }
113
114     if (!doValidate(toValidate)) {
115       context.getStatus().error(this);
116     }
117   }
118
119   /**
120    * This template method must be implemented by inheriting classes, and is called
121    * by this class' <code>validate()</code>.
122    * <p>
123    * Inheriting classes should implement their validation logic in this method, and
124    * return <code>false</code> if the validation fails.
125    *
126    * @param toValidate the object to validate.
127    *
128    * @return <code>false</code> if the validation logic implemented by this method
129    * detects a validation error.
130    */

131   protected abstract boolean doValidate(Object JavaDoc toValidate);
132 }
133
Popular Tags