View Javadoc

1   /*
2    * $Id: RegexValidator.java,v 1.7 2004/04/25 10:02:36 eelco12 Exp $
3    * $Revision: 1.7 $
4    * $Date: 2004/04/25 10:02:36 $
5    *
6    * ====================================================================
7    * Copyright (c) 2003, Open Edge B.V.
8    * All rights reserved.
9    * Redistribution and use in source and binary forms, with or without 
10   * modification, are permitted provided that the following conditions are met:
11   * Redistributions of source code must retain the above copyright notice, 
12   * this list of conditions and the following disclaimer. Redistributions 
13   * in binary form must reproduce the above copyright notice, this list of 
14   * conditions and the following disclaimer in the documentation and/or other 
15   * materials provided with the distribution. Neither the name of OpenEdge B.V. 
16   * nor the names of its contributors may be used to endorse or promote products 
17   * derived from this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package nl.openedge.baritus.validation.impl;
32  
33  import java.util.regex.Matcher;
34  import java.util.regex.Pattern;
35  
36  import nl.openedge.baritus.FormBeanContext;
37  import nl.openedge.baritus.util.ValueUtils;
38  import nl.openedge.baritus.validation.AbstractFieldValidator;
39  import nl.openedge.baritus.validation.ValidationActivationRule;
40  
41  import org.infohazard.maverick.flow.ControllerContext;
42  
43  /***
44   * Tests for a match against a regex pattern.
45   * if property 'mode' is MODE_VALID_IF_MATCHES (which is the default), isValid returns
46   * true if the input matches the pattern. If property mode is MODE_INVALID_IF_MATCHES
47   * (i.e. else), isValid returns false if the input matches the pattern.
48   * 
49   * @author Eelco Hillenius
50   */
51  public class RegexValidator extends AbstractFieldValidator
52  {
53  	/*** when in this mode, isValid will return true if the input matches the pattern */
54  	public final static int MODE_VALID_IF_MATCHES = 0;
55  	
56  	/*** when in this mode, isValid will return false if the input matches the pattern */
57  	public final static int MODE_INVALID_IF_MATCHES = 1;
58  
59  	/* defaults to valid if matches */
60  	private int mode = MODE_VALID_IF_MATCHES;
61  	
62  	/* the regexp pattern to match against */
63  	private Pattern pattern = null;
64  	
65  	private String errorMessageKey = "invalid.input";
66  
67  	/***
68  	 * construct without parameters
69  	 */
70  	public RegexValidator()
71  	{
72  		
73  	}
74  	
75  	/***
76  	 * construct with pattern
77  	 * @param pattern
78  	 */
79  	public RegexValidator(Pattern pattern)
80  	{
81  		setPattern(pattern);
82  	}
83  	
84  	/***
85  	 * construct with errorMessageKey and pattern
86  	 * @param errorMessageKey 
87  	 * @param pattern
88  	 */
89  	public RegexValidator(String errorMessageKey, Pattern pattern)
90  	{
91  		setErrorMessageKey(errorMessageKey);
92  		setPattern(pattern);
93  	}
94  	
95  	/***
96  	 * construct with errorMessageKey, rule and pattern 
97  	 * @param errorMessageKey
98  	 * @param rule
99  	 * @param pattern
100 	 */
101 	public RegexValidator(String errorMessageKey, ValidationActivationRule rule, Pattern pattern)
102 	{
103 		setErrorMessageKey(errorMessageKey);
104 		setValidationRule(rule);
105 		setPattern(pattern);
106 	}
107 	
108 	/***
109 	 * construct with pattern and mode
110 	 * @param pattern
111 	 * @param mode
112 	 */
113 	public RegexValidator(Pattern pattern, int mode)
114 	{
115 		setPattern(pattern);
116 		setMode(mode);
117 	}
118 	
119 	/***
120 	 * construct with message prefix, pattern and mode
121 	 * @param errorMessageKey 
122 	 * @param pattern
123 	 * @param mode
124 	 */
125 	public RegexValidator(String errorMessageKey, Pattern pattern, int mode)
126 	{
127 		setErrorMessageKey(errorMessageKey);
128 		setPattern(pattern);
129 		setMode(mode);
130 	}
131 	
132 	/***
133 	 * construct with errorMessageKey, rule, pattern and mode 
134 	 * @param errorMessageKey
135 	 * @param rule
136 	 * @param pattern
137 	 * @param mode
138 	 */
139 	public RegexValidator(
140 		String errorMessageKey, ValidationActivationRule rule, Pattern pattern, int mode)
141 	{
142 		setErrorMessageKey(errorMessageKey);
143 		setValidationRule(rule);
144 		setPattern(pattern);
145 		setMode(mode);
146 	}
147 
148 	/***
149 	 * @see nl.openedge.baritus.validation.FieldValidator#isValid(org.infohazard.maverick.flow.ControllerContext, nl.openedge.baritus.FormBeanContext, java.lang.String, java.lang.Object)
150 	 */
151 	public boolean isValid(
152 		ControllerContext cctx,
153 		FormBeanContext formBeanContext,
154 		String fieldName,
155 		Object value)
156 	{
157 		if(pattern == null) throw new RuntimeException("pattern is not provided!");
158 		
159 		String toMatch = ValueUtils.convertToString(value); // convert to String
160 		Matcher matcher = pattern.matcher(toMatch); // create a matcher
161 
162 		boolean valid = (mode == MODE_VALID_IF_MATCHES) ? matcher.matches() : !matcher.matches();
163 
164 		if(!valid)
165 		{
166 			setErrorMessage(formBeanContext, fieldName, getErrorMessageKey(), 
167 				new Object[]{getFieldName(formBeanContext, fieldName), value});
168 		}
169 
170 		return valid;
171 	}
172 	
173 	//---------------------------------- PROPERTIES ----------------------------------------
174 
175 	/***
176 	 * get mode
177 	 * @return int
178 	 */
179 	public int getMode()
180 	{
181 		return mode;
182 	}
183 
184 	/***
185 	 * get pattern
186 	 * @return Pattern
187 	 */
188 	public Pattern getPattern()
189 	{
190 		return pattern;
191 	}
192 
193 	/***
194 	 * set mode
195 	 * @param i mode
196 	 */
197 	public void setMode(int i)
198 	{
199 		mode = i;
200 	}
201 
202 	/***
203 	 * set pattern
204 	 * @param pattern regex pattern
205 	 */
206 	public void setPattern(Pattern pattern)
207 	{
208 		this.pattern = pattern;
209 	}
210 	
211 	/***
212 	 * Get key of error message.
213 	 * @return String key of error message
214 	 */
215 	public String getErrorMessageKey()
216 	{
217 		return errorMessageKey;
218 	}
219 
220 	/***
221 	 * Set key of error message.
222 	 * @param string key of error message
223 	 */
224 	public void setErrorMessageKey(String string)
225 	{
226 		errorMessageKey = string;
227 	}
228 
229 }