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 $
4
* $Date: 2004/02/21 17:10:29 $
6
* ====================================================================
7
* Copyright 2001-2004 The Apache Software Foundation
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
13
* http://www.apache.org/licenses/LICENSE-2.0
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.
22
package org.apache.commons.validator;
24
import java.io.Serializable;
25
import java.util.Collections;
26
import java.util.HashMap;
27
import java.util.Iterator;
32
* This contains the results of a set of validation rules processed
35
public class ValidatorResults implements Serializable {
38
* Map of validation results.
40
protected Map hResults = new HashMap();
43
* Merge another ValidatorResults into mine.
45
public void merge(ValidatorResults results) {
46
this.hResults.putAll(results.hResults);
50
* Add a the result of a validator action.
52
public void add(Field field, String validatorName, boolean result) {
53
this.add(field, validatorName, result, null);
57
* Add a the result of a validator action.
65
ValidatorResult validatorResult = this.getValidatorResult(field.getKey());
67
if (validatorResult == null) {
68
validatorResult = new ValidatorResult(field);
69
this.hResults.put(field.getKey(), validatorResult);
72
validatorResult.add(validatorName, result, value);
76
* Clear all results recorded by this object.
79
this.hResults.clear();
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.
87
public boolean empty() {
88
return this.isEmpty();
92
* Return <code>true</code> if there are no messages recorded
93
* in this collection, or <code>false</code> otherwise.
95
public boolean isEmpty() {
96
return this.hResults.isEmpty();
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.
104
* @param key The key generated from <code>Field</code> (this is often just
107
public ValidatorResult getValidatorResult(String key) {
108
return (ValidatorResult) this.hResults.get(key);
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.
117
public Iterator get() {
118
if (hResults.isEmpty()) {
119
return Collections.EMPTY_LIST.iterator();
122
return hResults.keySet().iterator();
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
131
* @deprecated Use getPropertyNames() instead.
133
public Iterator properties() {
134
return hResults.keySet().iterator();
138
* Return the set of property names for which at least one message has
140
* @return An unmodifiable Set of the property names.
142
public Set getPropertyNames() {
143
return Collections.unmodifiableSet(this.hResults.keySet());
147
* Get a <code>Map</code> of any <code>Object</code>s returned from
148
* validation routines.
150
public Map getResultValueMap() {
151
Map results = new HashMap();
153
for (Iterator i = hResults.keySet().iterator(); i.hasNext();) {
154
String propertyKey = (String) i.next();
155
ValidatorResult vr = this.getValidatorResult(propertyKey);
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);
164
Object result = rs.getResult();
166
if (result != null && !(result instanceof Boolean)) {
167
results.put(propertyKey, result);
2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.validator;
19
import java.io.Serializable;
20
import java.util.Collections;
21
import java.util.HashMap;
22
import java.util.Iterator;
27
* This contains the results of a set of validation rules processed
30
* @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
32
public class ValidatorResults implements Serializable {
35
* Map of validation results.
37
protected Map hResults = new HashMap();
40
* Merge another ValidatorResults into mine.
42
* @param results ValidatorResults to merge.
44
public void merge(ValidatorResults results) {
45
this.hResults.putAll(results.hResults);
49
* Add a the result of a validator action.
51
* @param field The field validated.
52
* @param validatorName The name of the validator.
53
* @param result The result of the validation.
55
public void add(Field field, String validatorName, boolean result) {
56
this.add(field, validatorName, result, null);
60
* Add a the result of a validator action.
62
* @param field The field validated.
63
* @param validatorName The name of the validator.
64
* @param result The result of the validation.
65
* @param value The value returned by the validator.
73
ValidatorResult validatorResult = this.getValidatorResult(field.getKey());
75
if (validatorResult == null) {
76
validatorResult = new ValidatorResult(field);
77
this.hResults.put(field.getKey(), validatorResult);
80
validatorResult.add(validatorName, result, value);
84
* Clear all results recorded by this object.
87
this.hResults.clear();
91
* Return <code>true</code> if there are no messages recorded
92
* in this collection, or <code>false</code> otherwise.
94
* @return Whether these results are empty.
96
public boolean isEmpty() {
97
return this.hResults.isEmpty();
101
* Gets the <code>ValidatorResult</code> associated
102
* with the key passed in. The key the <code>ValidatorResult</code>
103
* is stored under is the <code>Field</code>'s getKey method.
105
* @param key The key generated from <code>Field</code> (this is often just
108
* @return The result of a specified key.
110
public ValidatorResult getValidatorResult(String key) {
111
return (ValidatorResult) this.hResults.get(key);
115
* Return the set of property names for which at least one message has
117
* @return An unmodifiable Set of the property names.
119
public Set getPropertyNames() {
120
return Collections.unmodifiableSet(this.hResults.keySet());
124
* Get a <code>Map</code> of any <code>Object</code>s returned from
125
* validation routines.
127
* @return Map of objections returned by validators.
129
public Map getResultValueMap() {
130
Map results = new HashMap();
132
for (Iterator i = hResults.keySet().iterator(); i.hasNext();) {
133
String propertyKey = (String) i.next();
134
ValidatorResult vr = this.getValidatorResult(propertyKey);
136
for (Iterator x = vr.getActions(); x.hasNext();) {
137
String actionKey = (String)x.next();
138
Object result = vr.getResult(actionKey);
140
if (result != null && !(result instanceof Boolean)) {
141
results.put(propertyKey, result);