Coverage report

  %line %branch
org.apache.turbine.util.FormMessages
0% 
0% 

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

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.