~ubuntu-branches/ubuntu/oneiric/libcommons-validator-java/oneiric

« back to all changes in this revision

Viewing changes to src/share/org/apache/commons/validator/ValidatorResults.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath, Kumar Appaiah, Varun Hiremath
  • Date: 2007-09-16 00:57:46 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070916005746-r5xwnjm12ng9fbdf
Tags: 1:1.3.1-1
[ Kumar Appaiah ]
* New upstream release.
* New uploaders: Varun Hiremath and Kumar Appaiah, removed Wolfgang Baer
* Use upstream's conf directory for configuration and DTDs.
* Use a custom ant.properties to avoid downloads.
* Use DEB_UPSTREAM_VERSION in rules instead of versions in rules for symlinking.

[ Varun Hiremath ]
* debian/control:
  + Add rhino to Build-Depends-Indep and Depends.
  + Add XS-Vcs-{Svn, Browser} headers.
  + Depend on java-gcj-compat instead of kaffe.
* debian/compat: switch to 5
* Remove debian/patches and remove RELEASE-NOTES.txt in debian/rules.
* Add debian/orig-tar.sh to remove CRLF line terminators from upstream files.
* debian/rules: implement get-orig-source
* debian/watch: switch to version 3 and call debian/orig-tar.sh

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
}
 
1
/*
 
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
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
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.
 
16
 */
 
17
package org.apache.commons.validator;
 
18
 
 
19
import java.io.Serializable;
 
20
import java.util.Collections;
 
21
import java.util.HashMap;
 
22
import java.util.Iterator;
 
23
import java.util.Map;
 
24
import java.util.Set;
 
25
 
 
26
/**
 
27
 * This contains the results of a set of validation rules processed 
 
28
 * on a JavaBean.
 
29
 *
 
30
 * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
 
31
 */
 
32
public class ValidatorResults implements Serializable {
 
33
 
 
34
    /**
 
35
     * Map of validation results.
 
36
     */
 
37
    protected Map hResults = new HashMap();
 
38
 
 
39
    /**
 
40
     * Merge another ValidatorResults into mine.
 
41
     *
 
42
     * @param results ValidatorResults to merge.
 
43
     */
 
44
    public void merge(ValidatorResults results) {
 
45
        this.hResults.putAll(results.hResults);
 
46
    }
 
47
 
 
48
    /**
 
49
     * Add a the result of a validator action.
 
50
     *
 
51
     * @param field The field validated.
 
52
     * @param validatorName The name of the validator.
 
53
     * @param result The result of the validation.
 
54
     */
 
55
    public void add(Field field, String validatorName, boolean result) {
 
56
        this.add(field, validatorName, result, null);
 
57
    }
 
58
 
 
59
    /**
 
60
     * Add a the result of a validator action.
 
61
     *
 
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.
 
66
     */
 
67
    public void add(
 
68
            Field field,
 
69
            String validatorName,
 
70
            boolean result,
 
71
            Object value) {
 
72
 
 
73
        ValidatorResult validatorResult = this.getValidatorResult(field.getKey());
 
74
 
 
75
        if (validatorResult == null) {
 
76
            validatorResult = new ValidatorResult(field);
 
77
            this.hResults.put(field.getKey(), validatorResult);
 
78
        }
 
79
 
 
80
        validatorResult.add(validatorName, result, value);
 
81
    }
 
82
 
 
83
    /**
 
84
     * Clear all results recorded by this object.
 
85
     */
 
86
    public void clear() {
 
87
        this.hResults.clear();
 
88
    }
 
89
 
 
90
    /**
 
91
     * Return <code>true</code> if there are no messages recorded
 
92
     * in this collection, or <code>false</code> otherwise.
 
93
     *
 
94
     * @return Whether these results are empty.
 
95
     */
 
96
    public boolean isEmpty() {
 
97
        return this.hResults.isEmpty();
 
98
    }
 
99
 
 
100
    /**
 
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.
 
104
     *
 
105
     * @param key The key generated from <code>Field</code> (this is often just
 
106
     * the field name).
 
107
     *
 
108
     * @return The result of a specified key.
 
109
     */
 
110
    public ValidatorResult getValidatorResult(String key) {
 
111
        return (ValidatorResult) this.hResults.get(key);
 
112
    }
 
113
 
 
114
    /**
 
115
     * Return the set of property names for which at least one message has
 
116
     * been recorded.
 
117
     * @return An unmodifiable Set of the property names.
 
118
     */
 
119
    public Set getPropertyNames() {
 
120
        return Collections.unmodifiableSet(this.hResults.keySet());
 
121
    }
 
122
 
 
123
    /**
 
124
     * Get a <code>Map</code> of any <code>Object</code>s returned from
 
125
     * validation routines.
 
126
     *
 
127
     * @return Map of objections returned by validators.
 
128
     */
 
129
    public Map getResultValueMap() {
 
130
        Map results = new HashMap();
 
131
 
 
132
        for (Iterator i = hResults.keySet().iterator(); i.hasNext();) {
 
133
            String propertyKey = (String) i.next();
 
134
            ValidatorResult vr = this.getValidatorResult(propertyKey);
 
135
 
 
136
            for (Iterator x = vr.getActions(); x.hasNext();) {
 
137
                String actionKey = (String)x.next();
 
138
                Object result = vr.getResult(actionKey);
 
139
 
 
140
                if (result != null && !(result instanceof Boolean)) {
 
141
                    results.put(propertyKey, result);
 
142
                }
 
143
            }
 
144
        }
 
145
 
 
146
        return results;
 
147
    }
 
148
 
 
149
}