View Javadoc
1 package org.apache.turbine.util; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.util.Hashtable; 58 import java.util.Iterator; 59 import java.util.List; 60 import java.util.Vector; 61 62 /*** 63 * Used for adding and accessing messages that relate to a specific 64 * form and field. Allows to query for messages by form name and 65 * field name. Used together with FormMessage class. 66 * 67 * @author <a href="mailto:neeme@one.lv">Neeme Praks</a> 68 * @version $Id: FormMessages.java,v 1.1.1.1 2001/08/16 05:09:39 jvanzyl Exp $ 69 */ 70 public class FormMessages 71 { 72 private Hashtable forms_messages; 73 private Hashtable fields_messages; 74 private Hashtable messages_fields; 75 private Hashtable forms_fields; 76 77 /*** 78 * Constructor. 79 */ 80 public FormMessages() 81 { 82 forms_messages = new Hashtable(); 83 fields_messages = new Hashtable(); 84 messages_fields = new Hashtable(); 85 forms_fields = new Hashtable(); 86 } 87 88 /*** 89 * Sets a message for a field of a form. The message is given as 90 * a long representing a return code. 91 * 92 * @param formName A String with the form name. 93 * @param fieldName A String with the field name. 94 * @param returnCode A long with the return code. 95 */ 96 public void setMessage(String formName, 97 String fieldName, 98 long returnCode) 99 { 100 setMessage(formName, fieldName, String.valueOf(returnCode) ); 101 } 102 103 /*** 104 * Sets a message for a field of a form. The message is given as 105 * a String. 106 * 107 * @param formName A String with the form name. 108 * @param fieldName A String with the field name. 109 * @param messageName A String with the message. 110 */ 111 public void setMessage(String formName, 112 String fieldName, 113 String messageName) 114 { 115 fieldName = formName + "-" + fieldName; 116 addValue(forms_messages, formName, messageName); 117 addValue(fields_messages, fieldName, messageName); 118 addValue(messages_fields, messageName, fieldName); 119 addValue(forms_fields, formName, fieldName); 120 } 121 122 /*** 123 * Adds a pair key/value to a table, making sure not to add 124 * duplicate keys. 125 * 126 * @param table A Hastable. 127 * @param key A String with the key. 128 * @param value A String with value. 129 */ 130 private void addValue(Hashtable table, 131 String key, 132 String value) 133 { 134 Vector values; 135 136 if (!table.containsKey(key)) 137 { 138 values = new Vector(); 139 values.addElement(value); 140 table.put(key, values); 141 } 142 else 143 { 144 values = ((Vector) table.get(key)); 145 if (!values.contains(value)) 146 values.addElement(value); 147 } 148 } 149 150 /*** 151 * Gets a pair key/value from a table. 152 * 153 * @param table A Hastable. 154 * @param key A String with the key. 155 * @return A Vector with the pair key/value, or null. 156 */ 157 private final Vector getValues(Hashtable table, String key) 158 { 159 return (Vector)table.get(key); 160 } 161 162 /*** 163 * Gets all form messages for a given form. 164 * 165 * @param formName A String with the form name. 166 * @return A FormMessage[]. 167 */ 168 public FormMessage[] getFormMessages(String formName) 169 { 170 Vector messages, fields; 171 String messageName, fieldName; 172 messages = getValues(forms_messages, formName); 173 if (messages != null) 174 { 175 FormMessage[] result = new FormMessage[messages.size()]; 176 for (int i = 0; i < messages.size(); i++) 177 { 178 result[i] = new FormMessage( formName ); 179 messageName = (String) messages.elementAt(i); 180 result[i].setMessage( messageName ); 181 fields = getValues(messages_fields, messageName); 182 for (int j = 0; j < fields.size(); j++) 183 { 184 fieldName = (String) fields.elementAt(j); 185 if (formHasField( formName, fieldName )) 186 { 187 result[i].setFieldName( fieldName ); 188 } 189 } 190 } 191 return result; 192 } 193 return null; 194 } 195 196 /*** 197 * Get form messages for a given form and field. 198 * 199 * @param formName A String with the form name. 200 * @param fieldName A String with the field name. 201 * @return A FormMessage[]. 202 */ 203 public FormMessage[] getFormMessages(String formName, String fieldName) 204 { 205 String key = formName + "-" + fieldName; 206 207 Vector messages = getValues(fields_messages, key); 208 String messageName; 209 210 if (messages != null) 211 { 212 FormMessage[] result = new FormMessage[messages.size()]; 213 for (int i = 0; i < messages.size(); i++) 214 { 215 result[i] = new FormMessage( formName, fieldName ); 216 messageName = (String) messages.elementAt(i); 217 result[i].setMessage( messageName ); 218 } 219 return result; 220 } 221 return null; 222 } 223 224 /*** 225 * Check whether a form as a field. 226 * 227 * @param formName A String with the form name. 228 * @param fieldName A String with the field name. 229 * @return True if form has the field. 230 */ 231 private boolean formHasField(String formName, 232 String fieldName) 233 { 234 List fields = getValues(forms_fields, formName); 235 for (Iterator iter = fields.iterator(); iter.hasNext(); ) 236 { 237 if (fieldName.equals(iter.next().toString())) 238 { 239 return true; 240 } 241 } 242 return false; 243 } 244 }

This page was automatically generated by Maven