2 * Copyright 2005-2010 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.
16 package org.wamblee.usermgt;
18 import org.wamblee.usermgt.UserMgtException.Reason;
21 * Validation of names based on a regular expression.
23 * @author Erik Brakkee
25 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.
55 * Pattern that names must comply to.
57 * Reason to report when validation fails.
61 public RegexpNameValidator(String aPattern, Reason aReason, String aMessage) {
68 * Convenience constructor with all string parameters. Useful for
69 * configuration in Spring.
78 public RegexpNameValidator(String aPattern, String aReason, String aMessage) {
79 this(aPattern, Reason.valueOf(aReason), aMessage);
85 * @see org.wamblee.usermgt.NameValidator#validate(java.lang.String)
87 public void validate(String aName) throws UserMgtException {
88 if (!aName.matches(pattern)) {
89 throw new UserMgtException(reason, message);