View Javadoc

    1   /*
    2    * $Id: ValidatorResults.java 376699 2006-02-10 14:27:03Z niallp $
    3    * $Rev: 376699 $
    4    * $Date: 2006-02-10 14:27:03 +0000 (Fri, 10 Feb 2006) $
    5    *
    6    * ====================================================================
    7    * Copyright 2001-2006 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       * @param results ValidatorResults to merge.
    46       */
    47      public void merge(ValidatorResults results) {
    48          this.hResults.putAll(results.hResults);
    49      }
    50
    51      /***
    52       * Add a the result of a validator action.
    53       *
    54       * @param field The field validated.
    55       * @param validatorName The name of the validator.
    56       * @param result The result of the validation.
    57       */
    58      public void add(Field field, String validatorName, boolean result) {
    59          this.add(field, validatorName, result, null);
    60      }
    61
    62      /***
    63       * Add a the result of a validator action.
    64       *
    65       * @param field The field validated.
    66       * @param validatorName The name of the validator.
    67       * @param result The result of the validation.
    68       * @param value The value returned by the validator.
    69       */
    70      public void add(
    71              Field field,
    72              String validatorName,
    73              boolean result,
    74              Object value) {
    75
    76          ValidatorResult validatorResult = this.getValidatorResult(field.getKey());
    77
    78          if (validatorResult == null) {
    79              validatorResult = new ValidatorResult(field);
    80              this.hResults.put(field.getKey(), validatorResult);
    81          }
    82
    83          validatorResult.add(validatorName, result, value);
    84      }
    85
    86      /***
    87       * Clear all results recorded by this object.
    88       */
    89      public void clear() {
    90          this.hResults.clear();
    91      }
    92
    93      /***
    94       * Return <code>true</code> if there are no messages recorded
    95       * in this collection, or <code>false</code> otherwise.
    96       *
    97       * @return Whether these results are empty.
    98       */
    99      public boolean isEmpty() {
    100         return this.hResults.isEmpty();
    101     }
    102
    103     /***
    104      * Gets the <code>ValidatorResult</code> associated
    105      * with the key passed in.  The key the <code>ValidatorResult</code>
    106      * is stored under is the <code>Field</code>'s getKey method.
    107      *
    108      * @param key The key generated from <code>Field</code> (this is often just
    109      * the field name).
    110      *
    111      * @return The result of a specified key.
    112      */
    113     public ValidatorResult getValidatorResult(String key) {
    114         return (ValidatorResult) this.hResults.get(key);
    115     }
    116
    117     /***
    118      * Return the set of property names for which at least one message has
    119      * been recorded.
    120      * @return An unmodifiable Set of the property names.
    121      */
    122     public Set getPropertyNames() {
    123         return Collections.unmodifiableSet(this.hResults.keySet());
    124     }
    125
    126     /***
    127      * Get a <code>Map</code> of any <code>Object</code>s returned from
    128      * validation routines.
    129      *
    130      * @return Map of objections returned by validators.
    131      */
    132     public Map getResultValueMap() {
    133         Map results = new HashMap();
    134
    135         for (Iterator i = hResults.keySet().iterator(); i.hasNext();) {
    136             String propertyKey = (String) i.next();
    137             ValidatorResult vr = this.getValidatorResult(propertyKey);
    138
    139             for (Iterator x = vr.getActions(); x.hasNext();) {
    140                 String actionKey = (String)x.next();
    141                 Object result = vr.getResult(actionKey);
    142
    143                 if (result != null && !(result instanceof Boolean)) {
    144                     results.put(propertyKey, result);
    145                 }
    146             }
    147         }
    148
    149         return results;
    150     }
    151
    152 }