KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > validator > ValidatorResults


1 /*
2  * $Id: ValidatorResults.java 155434 2005-02-26 13:16:41Z dirkv $
3  * $Rev$
4  * $Date: 2005-02-26 05:16:41 -0800 (Sat, 26 Feb 2005) $
5  *
6  * ====================================================================
7  * Copyright 2001-2005 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 JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
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 JavaDoc {
36
37     /**
38      * Map of validation results.
39      */

40     protected Map JavaDoc hResults = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc validatorName,
62             boolean result,
63             Object JavaDoc 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      */

86     public boolean isEmpty() {
87         return this.hResults.isEmpty();
88     }
89
90     /**
91      * Gets the <code>ValidatorResult</code> associated
92      * with the key passed in. The key the <code>ValidatorResult</code>
93      * is stored under is the <code>Field</code>'s getKey method.
94      *
95      * @param key The key generated from <code>Field</code> (this is often just
96      * the field name).
97      */

98     public ValidatorResult getValidatorResult(String JavaDoc key) {
99         return (ValidatorResult) this.hResults.get(key);
100     }
101
102     /**
103      * Return the set of property names for which at least one message has
104      * been recorded.
105      * @return An unmodifiable Set of the property names.
106      */

107     public Set JavaDoc getPropertyNames() {
108         return Collections.unmodifiableSet(this.hResults.keySet());
109     }
110
111     /**
112      * Get a <code>Map</code> of any <code>Object</code>s returned from
113      * validation routines.
114      */

115     public Map JavaDoc getResultValueMap() {
116         Map JavaDoc results = new HashMap JavaDoc();
117
118         for (Iterator JavaDoc i = hResults.keySet().iterator(); i.hasNext();) {
119             String JavaDoc propertyKey = (String JavaDoc) i.next();
120             ValidatorResult vr = this.getValidatorResult(propertyKey);
121
122             Map JavaDoc actions = vr.getActionMap();
123             for (Iterator JavaDoc x = actions.keySet().iterator(); x.hasNext();) {
124                 String JavaDoc actionKey = (String JavaDoc) x.next();
125                 ValidatorResult.ResultStatus rs =
126                         (ValidatorResult.ResultStatus) actions.get(actionKey);
127
128                 if (rs != null) {
129                     Object JavaDoc result = rs.getResult();
130
131                     if (result != null && !(result instanceof Boolean JavaDoc)) {
132                         results.put(propertyKey, result);
133                     }
134                 }
135             }
136         }
137
138         return results;
139     }
140
141 }
142
Popular Tags