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 * @author Erik Brakkee
26 public class RegexpNameValidator implements NameValidator {
29 * Convenience pattern for an id.
31 public static final String ID_PATTERN = "[a-zA-Z]+[a-zA-Z0-9]*";
34 * Convenience pattern for a password consisting of at least 6 characters.
36 public static final String PASSWORD_PATTERN = ".{6}.*";
41 private String _pattern;
44 * Reason to use when validation fails.
46 private Reason _reason;
51 private String _message;
54 * Validates a regular expression.
55 * @param aPattern Pattern that names must comply to.
56 * @param aReason Reason to report when validation fails.
57 * @param aMessage Message to report.
59 public RegexpNameValidator(String aPattern, Reason aReason, String aMessage) {
66 * Convenience constructor with all string parameters. Useful for configuration
68 * @param aPattern Pattern to use.
69 * @param aReason Reason.
70 * @param aMessage Message.
72 public RegexpNameValidator(String aPattern, String aReason, String aMessage) {
73 this(aPattern, Reason.valueOf(aReason), aMessage);
77 * @see org.wamblee.usermgt.NameValidator#validate(java.lang.String)
79 public void validate(String aName) throws UserMgtException {
80 if ( !aName.matches(_pattern)) {
81 throw new UserMgtException(_reason, _message);