View Javadoc

1   /*
2    * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorResults.java,v 1.10 2004/02/21 17:10:29 rleland Exp $
3    * $Revision: 1.10 $
4    * $Date: 2004/02/21 17:10:29 $
5    *
6    * ====================================================================
7    * Copyright 2001-2004 The Apache Software Foundation
8    *
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   *
13   *     http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   */
21  
22  package org.apache.commons.validator;
23  
24  import java.io.Serializable;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.Map;
29  import java.util.Set;
30  
31  /***
32   * This contains the results of a set of validation rules processed 
33   * on a JavaBean.
34   */
35  public class ValidatorResults implements Serializable {
36  
37      /***
38       * Map of validation results.
39       */
40      protected Map hResults = new HashMap();
41  
42      /***
43       * Merge another ValidatorResults into mine.
44       */
45      public void merge(ValidatorResults results) {
46          this.hResults.putAll(results.hResults);
47      }
48  
49      /***
50       * Add a the result of a validator action.
51       */
52      public void add(Field field, String validatorName, boolean result) {
53          this.add(field, validatorName, result, null);
54      }
55  
56      /***
57       * Add a the result of a validator action.
58       */
59      public void add(
60              Field field,
61              String validatorName,
62              boolean result,
63              Object value) {
64  
65          ValidatorResult validatorResult = this.getValidatorResult(field.getKey());
66  
67          if (validatorResult == null) {
68              validatorResult = new ValidatorResult(field);
69              this.hResults.put(field.getKey(), validatorResult);
70          }
71  
72          validatorResult.add(validatorName, result, value);
73      }
74  
75      /***
76       * Clear all results recorded by this object.
77       */
78      public void clear() {
79          this.hResults.clear();
80      }
81  
82      /***
83       * Return <code>true</code> if there are no messages recorded
84       * in this collection, or <code>false</code> otherwise.
85       * @deprecated Use isEmpty() instead.
86       */
87      public boolean empty() {
88          return this.isEmpty();
89      }
90  
91      /***
92       * Return <code>true</code> if there are no messages recorded
93       * in this collection, or <code>false</code> otherwise.
94       */
95      public boolean isEmpty() {
96          return this.hResults.isEmpty();
97      }
98  
99      /***
100      * Gets the <code>ValidatorResult</code> associated
101      * with the key passed in.  The key the <code>ValidatorResult</code>
102      * is stored under is the <code>Field</code>'s getKey method.
103      *
104      * @param key The key generated from <code>Field</code> (this is often just
105      * the field name).
106      */
107     public ValidatorResult getValidatorResult(String key) {
108         return (ValidatorResult) this.hResults.get(key);
109     }
110 
111     /***
112      * Return the set of all recorded messages, without distinction
113      * by which property the messages are associated with.  If there are
114      * no messages recorded, an empty enumeration is returned.
115      * @deprecated Use getPropertyNames() instead.
116      */
117     public Iterator get() {
118         if (hResults.isEmpty()) {
119             return Collections.EMPTY_LIST.iterator();
120         }
121 
122         return hResults.keySet().iterator();
123     }
124 
125     /***
126      * Return the set of property names for which at least one message has
127      * been recorded.  If there are no messages, an empty Iterator is returned.
128      * If you have recorded global messages, the String value of
129      * <code>ActionMessages.GLOBAL_MESSAGE</code> will be one of the returned
130      * property names.
131      * @deprecated Use getPropertyNames() instead.
132      */
133     public Iterator properties() {
134         return hResults.keySet().iterator();
135     }
136 
137     /***
138      * Return the set of property names for which at least one message has
139      * been recorded.
140      * @return An unmodifiable Set of the property names.
141      */
142     public Set getPropertyNames() {
143         return Collections.unmodifiableSet(this.hResults.keySet());
144     }
145 
146     /***
147      * Get a <code>Map</code> of any <code>Object</code>s returned from
148      * validation routines.
149      */
150     public Map getResultValueMap() {
151         Map results = new HashMap();
152 
153         for (Iterator i = hResults.keySet().iterator(); i.hasNext();) {
154             String propertyKey = (String) i.next();
155             ValidatorResult vr = this.getValidatorResult(propertyKey);
156 
157             Map actions = vr.getActionMap();
158             for (Iterator x = actions.keySet().iterator(); x.hasNext();) {
159                 String actionKey = (String) x.next();
160                 ValidatorResult.ResultStatus rs =
161                         (ValidatorResult.ResultStatus) actions.get(actionKey);
162 
163                 if (rs != null) {
164                     Object result = rs.getResult();
165 
166                     if (result != null && !(result instanceof Boolean)) {
167                         results.put(propertyKey, result);
168                     }
169                 }
170             }
171         }
172 
173         return results;
174     }
175 
176 }