KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > util > RegularExpressionValidator


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package com.izforge.izpack.util;
21
22 import java.util.Map JavaDoc;
23
24 import org.apache.regexp.RE;
25
26 import com.izforge.izpack.panels.ProcessingClient;
27 import com.izforge.izpack.panels.RuleInputField;
28 import com.izforge.izpack.panels.Validator;
29
30 /**
31  * A validator to enforce non-empty fields.
32  *
33  * This validator can be used for rule input fields in the UserInputPanel to make sure that the
34  * user's entry matches a specified regular expression.
35  *
36  * @author Mike Cunneen <mike dot cunneen at screwfix dot com>
37  */

38 public class RegularExpressionValidator implements Validator
39 {
40
41     public static final String JavaDoc STR_PATTERN_DEFAULT = "[a-zA-Z0-9._-]{3,}@[a-zA-Z0-9._-]+([.][a-zA-Z0-9_-]+)*[.][a-zA-Z0-9._-]{2,4}";
42
43     private static final String JavaDoc PATTERN_PARAM = "pattern";
44
45     public boolean validate(ProcessingClient client)
46     {
47
48         String JavaDoc patternString;
49
50         RuleInputField field = (RuleInputField) client;
51         if (field.hasParams())
52         {
53             Map JavaDoc paramMap = field.getValidatorParams();
54             patternString = (String JavaDoc) paramMap.get(PATTERN_PARAM);
55
56         }
57         else
58         {
59             patternString = STR_PATTERN_DEFAULT;
60         }
61
62         RE pattern = new RE(patternString);
63         return pattern.match(((RuleInputField) client).getText());
64     }
65
66 }
67
Popular Tags