View Javadoc

    1   package org.apache.turbine.util;
    2
    3
    4   /*
    5    * Licensed to the Apache Software Foundation (ASF) under one
    6    * or more contributor license agreements.  See the NOTICE file
    7    * distributed with this work for additional information
    8    * regarding copyright ownership.  The ASF licenses this file
    9    * to you under the Apache License, Version 2.0 (the
    10   * "License"); you may not use this file except in compliance
    11   * with the License.  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,
    16   * software distributed under the License is distributed on an
    17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    18   * KIND, either express or implied.  See the License for the
    19   * specific language governing permissions and limitations
    20   * under the License.
    21   */
    22
    23
    24  import java.util.Hashtable;
    25  import java.util.Iterator;
    26  import java.util.List;
    27  import java.util.Vector;
    28
    29  /**
    30   * Used for adding and accessing messages that relate to a specific
    31   * form and field.  Allows to query for messages by form name and
    32   * field name.  Used together with FormMessage class.
    33   *
    34   * @author <a href="mailto:neeme@one.lv">Neeme Praks</a>
    35   * @version $Id: FormMessages.java 1073174 2011-02-21 22:18:45Z tv $
    36   */
    37  public class FormMessages
    38  {
    39      private final Hashtable<String, Vector<String>> forms_messages;
    40      private final Hashtable<String, Vector<String>> fields_messages;
    41      private final Hashtable<String, Vector<String>> messages_fields;
    42      private final Hashtable<String, Vector<String>> forms_fields;
    43
    44      /**
    45       * Constructor.
    46       */
    47      public FormMessages()
    48      {
    49          forms_messages = new Hashtable<String, Vector<String>>();
    50          fields_messages = new Hashtable<String, Vector<String>>();
    51          messages_fields = new Hashtable<String, Vector<String>>();
    52          forms_fields = new Hashtable<String, Vector<String>>();
    53      }
    54
    55      /**
    56       * Sets a message for a field of a form.  The message is given as
    57       * a long representing a return code.
    58       *
    59       * @param formName A String with the form name.
    60       * @param fieldName A String with the field name.
    61       * @param returnCode A long with the return code.
    62       */
    63      public void setMessage(String formName,
    64                             String fieldName,
    65                             long returnCode)
    66      {
    67          setMessage(formName, fieldName, String.valueOf(returnCode));
    68      }
    69
    70      /**
    71       * Sets a message for a field of a form.  The message is given as
    72       * a String.
    73       *
    74       * @param formName A String with the form name.
    75       * @param fieldName A String with the field name.
    76       * @param messageName A String with the message.
    77       */
    78      public void setMessage(String formName,
    79                             String fieldName,
    80                             String messageName)
    81      {
    82          fieldName = formName + "-" + fieldName;
    83          addValue(forms_messages, formName, messageName);
    84          addValue(fields_messages, fieldName, messageName);
    85          addValue(messages_fields, messageName, fieldName);
    86          addValue(forms_fields, formName, fieldName);
    87      }
    88
    89      /**
    90       * Adds a pair key/value to a table, making sure not to add
    91       * duplicate keys.
    92       *
    93       * @param table A Hashtable.
    94       * @param key A String with the key.
    95       * @param value A String with value.
    96       */
    97      private void addValue(Hashtable<String, Vector<String>> table,
    98                            String key,
    99                            String value)
    100     {
    101         Vector<String> values;
    102
    103         if (!table.containsKey(key))
    104         {
    105             values = new Vector<String>();
    106             values.addElement(value);
    107             table.put(key, values);
    108         }
    109         else
    110         {
    111             values = table.get(key);
    112             if (!values.contains(value))
    113                 values.addElement(value);
    114         }
    115     }
    116
    117     /**
    118      * Gets a pair key/value from a table.
    119      *
    120      * @param table A Hashtable.
    121      * @param key A String with the key.
    122      * @return A Vector with the pair key/value, or null.
    123      */
    124     private final Vector<String> getValues(Hashtable<String, Vector<String>> table, String key)
    125     {
    126         return table.get(key);
    127     }
    128
    129     /**
    130      * Gets all form messages for a given form.
    131      *
    132      * @param formName A String with the form name.
    133      * @return A FormMessage[].
    134      */
    135     public FormMessage[] getFormMessages(String formName)
    136     {
    137         Vector<String> messages, fields;
    138         String messageName, fieldName;
    139         messages = getValues(forms_messages, formName);
    140         if (messages != null)
    141         {
    142             FormMessage[] result = new FormMessage[messages.size()];
    143             for (int i = 0; i < messages.size(); i++)
    144             {
    145                 result[i] = new FormMessage(formName);
    146                 messageName = messages.elementAt(i);
    147                 result[i].setMessage(messageName);
    148                 fields = getValues(messages_fields, messageName);
    149                 for (int j = 0; j < fields.size(); j++)
    150                 {
    151                     fieldName = fields.elementAt(j);
    152                     if (formHasField(formName, fieldName))
    153                     {
    154                         result[i].setFieldName(fieldName);
    155                     }
    156                 }
    157             }
    158             return result;
    159         }
    160         return null;
    161     }
    162
    163     /**
    164      * Get form messages for a given form and field.
    165      *
    166      * @param formName A String with the form name.
    167      * @param fieldName A String with the field name.
    168      * @return A FormMessage[].
    169      */
    170     public FormMessage[] getFormMessages(String formName, String fieldName)
    171     {
    172         String key = formName + "-" + fieldName;
    173
    174         Vector<String> messages = getValues(fields_messages, key);
    175         String messageName;
    176
    177         if (messages != null)
    178         {
    179             FormMessage[] result = new FormMessage[messages.size()];
    180             for (int i = 0; i < messages.size(); i++)
    181             {
    182                 result[i] = new FormMessage(formName, fieldName);
    183                 messageName = messages.elementAt(i);
    184                 result[i].setMessage(messageName);
    185             }
    186             return result;
    187         }
    188         return null;
    189     }
    190
    191     /**
    192      * Check whether a form as a field.
    193      *
    194      * @param formName A String with the form name.
    195      * @param fieldName A String with the field name.
    196      * @return True if form has the field.
    197      */
    198     private boolean formHasField(String formName,
    199                                  String fieldName)
    200     {
    201         List fields = getValues(forms_fields, formName);
    202         for (Iterator iter = fields.iterator(); iter.hasNext();)
    203         {
    204             if (fieldName.equals(iter.next().toString()))
    205             {
    206                 return true;
    207             }
    208         }
    209         return false;
    210     }
    211 }