2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.wamblee.usermgt;
19 import org.wamblee.usermgt.UserMgtException.Reason;
22 * Validation of names based on a regular expression.
24 public class RegexpNameValidator implements NameValidator {
27 * Convenience pattern for an id.
29 public static final String ID_PATTERN = "[a-zA-Z]+[a-zA-Z0-9]*";
32 * Convenience pattern for a password consisting of at least 6 characters.
34 public static final String PASSWORD_PATTERN = ".{6}.*";
39 private String _pattern;
42 * Reason to use when validation fails.
44 private Reason _reason;
49 private String _message;
52 * Validates a regular expression.
53 * @param aPattern Pattern that names must comply to.
54 * @param aReason Reason to report when validation fails.
55 * @param aMessage Message to report.
57 public RegexpNameValidator(String aPattern, Reason aReason, String aMessage) {
64 * Convenience constructor with all string parameters. Useful for configuration
66 * @param aPattern Pattern to use.
67 * @param aReason Reason.
68 * @param aMessage Message.
70 public RegexpNameValidator(String aPattern, String aReason, String aMessage) {
71 this(aPattern, Reason.valueOf(aReason), aMessage);
75 * @see org.wamblee.usermgt.NameValidator#validate(java.lang.String)
77 public void validate(String aName) throws UserMgtException {
78 if ( !aName.matches(_pattern)) {
79 throw new UserMgtException(_reason, _message);