KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
7
8 /**
9  * Selects the value corresponding to the accessor specified on an instance
10  * of this class and pushes it on the validation context stack.
11  *
12  * @author Yanick Duchesne
13  * <dl>
14  * <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>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
17  * </dl>
18  */

19 public class Select extends Rule implements ObjectHandlerIF {
20   private String JavaDoc _attribute;
21   private Validatable _validatable;
22
23   /**
24    * Constructor for Select.
25    */

26   public Select() {
27     super();
28   }
29
30   /**
31    * Specifies the name of the accessor to invoke, and whose return
32    * value should be pushed onto the validation context's stack.
33    *
34    * @param attr the name of an accessor.
35    */

36   public void setAttribute(String JavaDoc attr) {
37     _attribute = attr;
38   }
39
40   /**
41    * @see org.sapia.util.xml.confix.ObjectHandlerIF#handleObject(String, Object)
42    */

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

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