KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.validator.rules;
2
3 import org.apache.commons.beanutils.PropertyUtils;
4 import org.sapia.validator.CompositeRule;
5 import org.sapia.validator.ValidationContext;
6
7 import java.util.Collection JavaDoc;
8 import java.util.Iterator JavaDoc;
9
10 /**
11  * Aggregates one-to-many nested validatable(s), iterating over the items
12  * in a collection/iterator/array and passing each item to the
13  * validate method of the nested validatable(s).
14  *
15  * @see org.sapia.validator.Validatable
16  * @see org.sapia.validator.Validatable#validate(ValidationContext)
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 ForEach extends CompositeRule {
26   private String JavaDoc _attribute;
27   private boolean _ignoreNull;
28
29   /**
30    * Constructor for ForEach.
31    */

32   public ForEach() {
33     super();
34   }
35
36   /**
37    * Sets the name of the accessor that will be used to retreive
38    * a collection or array to validate from the current object on the
39    * validation context's stack.
40    *
41    * @param attr the name of an accessor.
42    */

43   public void setAttribute(String JavaDoc attr) {
44     _attribute = attr;
45   }
46
47   /**
48    * @see org.sapia.validator.CompositeRule#validate(ValidationContext)
49    */

50   public void validate(ValidationContext ctx) {
51     Object JavaDoc obj;
52     Iterator JavaDoc items;
53
54     if (_attribute == null) {
55       obj = ctx.get();
56
57       if (obj == null) {
58         throw new IllegalStateException JavaDoc("No object on validation context stack at " + qualifiedName());
59       }
60     } else {
61       try {
62         if (ctx.get() == null) {
63           throw new IllegalStateException JavaDoc(
64             "No object on validation context stack at " + qualifiedName());
65         }
66
67         obj = PropertyUtils.getProperty(ctx.get(), _attribute);
68
69         if (obj == null) {
70           throw new IllegalStateException JavaDoc("Attribute " + _attribute
71             + " evaluates to null at " + qualifiedName());
72         }
73       } catch (Throwable JavaDoc err) {
74         ctx.getStatus().error(this, err);
75
76         return;
77       }
78     }
79
80     if (obj instanceof Collection JavaDoc) {
81       items = ((Collection JavaDoc) obj).iterator();
82     } else if (obj instanceof Object JavaDoc[]) {
83       items = new ArrayIterator((Object JavaDoc[]) obj);
84     } else if (obj instanceof Iterator JavaDoc) {
85       items = (Iterator JavaDoc) obj;
86     } else {
87       throw new IllegalStateException JavaDoc(
88         "forEach rule processes instances of java.util.Collection, java.util.Iterator, or arrays at " + qualifiedName());
89     }
90
91     if (items == null) {
92       return;
93     }
94
95     Object JavaDoc item;
96     boolean valid = true;
97
98     for (; items.hasNext();) {
99       item = items.next();
100       ctx.push(item);
101       super.validate(ctx);
102
103       if (ctx.getStatus().isError() && _stop) {
104         ctx.pop();
105
106         break;
107       }
108
109       ctx.pop();
110     }
111   }
112
113   static class ArrayIterator implements Iterator JavaDoc {
114     int _count;
115     Object JavaDoc[] _array;
116
117     ArrayIterator(Object JavaDoc[] array) {
118       _array = array;
119     }
120
121     /**
122      * @see java.util.Iterator#hasNext()
123      */

124     public boolean hasNext() {
125       return _count < _array.length;
126     }
127
128     /**
129      * @see java.util.Iterator#next()
130      */

131     public Object JavaDoc next() {
132       return _array[_count++];
133     }
134
135     /**
136      * @see java.util.Iterator#remove()
137      */

138     public void remove() {
139     }
140   }
141 }
142
Popular Tags