KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > forms > FormUtil


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: FormUtil.java,v 1.3 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.forms;
21
22 import java.util.*;
23
24 /**
25  * <p>Simple Form related utilities
26  *
27  * @author Christian Cryder [christianc@granitepeaks.com]
28  */

29 public class FormUtil {
30
31     /**
32      * <p>This method allows you to assert that a String value falls within a given
33      * min/max length range. If min or max is -1, that particular aspect will not
34      * be evaluated.
35      *
36      * <p>Example usage: <br>
37      * String user = assertMinMax((String) map.get(USER), "User Name", 5, 30);
38      *
39      * <p>This would retrieve a username from a map, validate it to ensure that its
40      * between 5 and 30 characters in length, and then return the adjusted value (if it
41      * was null in the map, it will come back as "", which is convenient for further
42      * custom evaluation)
43      *
44      * <p>Todo:
45      * <ul>
46      * <li>The error message generated by this method is not localized yet
47      * </li>
48      * </ul>
49      *
50      * @param field the field to be evaluated
51      * @param fieldDescr a description of the field to be included in any error messages
52      * @param min the minimum length of the String, or -1 if there is no min
53      * @param max the maximum length of the String, or -1 if there is no max
54      * @return the field value (non-null, adjusted and trimmed for further custom evaluation)
55      * @throws a ValidationException if the field is not valid
56      */

57     public static String JavaDoc assertMinMax(String JavaDoc field, String JavaDoc fieldDescr, int min, int max) throws ValidationException {
58         if (field==null) field = "";
59         else field = field.trim();
60         if (min!=-1) assertTrue(fieldDescr+" must be at least "+min+" chars in length -- Please re-enter!", field.length()>=min);
61         if (max!=-1) assertTrue(fieldDescr+" may not exceed "+max+" chars in length -- Please re-enter!", field.length()<=max);
62         return field;
63     }
64
65     /**
66      * This function simply evaluates a given boolean expression and throws a ValidationException
67      * using the specified error message if its not valid
68      *
69      * @param errmsg the error message to be used if the expression is invalid
70      * @param expression the expression to be evaluated
71      * @throws ValidationException if the expression is not true
72      */

73     public static void assertTrue(String JavaDoc errmsg, boolean expression) throws ValidationException {
74         if (!expression) throw new ValidationException(null, errmsg);
75     }
76
77
78 }
79
Popular Tags