001/* 002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved. 003 * 004 * http://www.izforge.com/izpack/ 005 * http://developer.berlios.de/projects/izpack/ 006 * 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020package com.izforge.izpack.util; 021 022import java.util.Map; 023 024import org.apache.regexp.RE; 025 026import com.izforge.izpack.panels.ProcessingClient; 027import com.izforge.izpack.panels.RuleInputField; 028import com.izforge.izpack.panels.Validator; 029 030/** 031 * A validator to enforce non-empty fields. 032 * 033 * This validator can be used for rule input fields in the UserInputPanel to make sure that the 034 * user's entry matches a specified regular expression. 035 * 036 * @author Mike Cunneen <mike dot cunneen at screwfix dot com> 037 */ 038public class RegularExpressionValidator implements Validator 039{ 040 041 public static final String STR_PATTERN_DEFAULT = "[a-zA-Z0-9._-]{3,}@[a-zA-Z0-9._-]+([.][a-zA-Z0-9_-]+)*[.][a-zA-Z0-9._-]{2,4}"; 042 043 private static final String PATTERN_PARAM = "pattern"; 044 045 public boolean validate(ProcessingClient client) 046 { 047 048 String patternString; 049 050 RuleInputField field = (RuleInputField) client; 051 if (field.hasParams()) 052 { 053 Map paramMap = field.getValidatorParams(); 054 patternString = (String) paramMap.get(PATTERN_PARAM); 055 056 } 057 else 058 { 059 patternString = STR_PATTERN_DEFAULT; 060 } 061 062 RE pattern = new RE(patternString); 063 return pattern.match(((RuleInputField) client).getText()); 064 } 065 066}