001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.validator.routines;
018
019import java.io.Serializable;
020import java.util.regex.Pattern;
021import java.util.regex.Matcher;
022
023/**
024 * <b>Regular Expression</b> validation (using JDK 1.4+ regex support).
025 * <p>
026 * Construct the validator either for a single regular expression or a set (array) of
027 * regular expressions. By default validation is <i>case sensitive</i> but constructors
028 * are provided to allow  <i>case in-sensitive</i> validation. For example to create
029 * a validator which does <i>case in-sensitive</i> validation for a set of regular
030 * expressions:
031 * <pre>
032 *         String[] regexs = new String[] {...};
033 *         RegexValidator validator = new RegexValidator(regexs, false);
034 * </pre>
035 * <p>
036 * <ul>
037 *   <li>Validate <code>true</code> or <code>false</code>:</li>
038 *   <ul>
039 *     <li><code>boolean valid = validator.isValid(value);</code></li>
040 *   </ul>
041 *   <li>Validate returning an aggregated String of the matched groups:</li>
042 *   <ul>
043 *     <li><code>String result = validator.validate(value);</code></li>
044 *   </ul>
045 *   <li>Validate returning the matched groups:</li>
046 *   <ul>
047 *     <li><code>String[] result = validator.match(value);</code></li>
048 *   </ul>
049 * </ul>
050 * <p>
051 * Cached instances pre-compile and re-use {@link Pattern}(s) - which according
052 * to the {@link Pattern} API are safe to use in a multi-threaded environment.
053 *
054 * @version $Revision: 1227719 $ $Date: 2012-01-05 18:45:51 +0100 (Do, 05 Jan 2012) $
055 * @since Validator 1.4
056 */
057public class RegexValidator implements Serializable {
058
059    private static final long serialVersionUID = -8832409930574867162L;
060
061    private final Pattern[] patterns;
062
063    /**
064     * Construct a <i>case sensitive</i> validator for a single
065     * regular expression.
066     *
067     * @param regex The regular expression this validator will
068     * validate against
069     */
070    public RegexValidator(String regex) {
071        this(regex, true);
072    }
073
074    /**
075     * Construct a validator for a single regular expression
076     * with the specified case sensitivity.
077     *
078     * @param regex The regular expression this validator will
079     * validate against
080     * @param caseSensitive when <code>true</code> matching is <i>case
081     * sensitive</i>, otherwise matching is <i>case in-sensitive</i>
082     */
083    public RegexValidator(String regex, boolean caseSensitive) {
084        this(new String[] {regex}, caseSensitive);
085    }
086
087    /**
088     * Construct a <i>case sensitive</i> validator that matches any one
089     * of the set of regular expressions.
090     *
091     * @param regexs The set of regular expressions this validator will
092     * validate against
093     */
094    public RegexValidator(String[] regexs) {
095        this(regexs, true);
096    }
097
098    /**
099     * Construct a validator that matches any one of the set of regular
100     * expressions with the specified case sensitivity.
101     *
102     * @param regexs The set of regular expressions this validator will
103     * validate against
104     * @param caseSensitive when <code>true</code> matching is <i>case
105     * sensitive</i>, otherwise matching is <i>case in-sensitive</i>
106     */
107    public RegexValidator(String[] regexs, boolean caseSensitive) {
108        if (regexs == null || regexs.length == 0) {
109            throw new IllegalArgumentException("Regular expressions are missing");
110        }
111        patterns = new Pattern[regexs.length];
112        int flags =  (caseSensitive ? 0: Pattern.CASE_INSENSITIVE);
113        for (int i = 0; i < regexs.length; i++) {
114            if (regexs[i] == null || regexs[i].length() == 0) {
115                throw new IllegalArgumentException("Regular expression[" + i + "] is missing");
116            }
117            patterns[i] =  Pattern.compile(regexs[i], flags);
118        }
119    }
120
121    /**
122     * Validate a value against the set of regular expressions.
123     *
124     * @param value The value to validate.
125     * @return <code>true</code> if the value is valid
126     * otherwise <code>false</code>.
127     */
128    public boolean isValid(String value) {
129        if (value == null) {
130            return false;
131        }
132        for (int i = 0; i < patterns.length; i++) {
133            if (patterns[i].matcher(value).matches()) {
134                return true;
135            }
136        }
137        return false;
138    }
139
140    /**
141     * Validate a value against the set of regular expressions
142     * returning the array of matched groups.
143     *
144     * @param value The value to validate.
145     * @return String array of the <i>groups</i> matched if
146     * valid or <code>null</code> if invalid
147     */
148    public String[] match(String value) {
149        if (value == null) {
150            return null;
151        }
152        for (int i = 0; i < patterns.length; i++) {
153            Matcher matcher = patterns[i].matcher(value);
154            if (matcher.matches()) {
155                int count = matcher.groupCount();
156                String[] groups = new String[count];
157                for (int j = 0; j < count; j++) {
158                    groups[j] = matcher.group(j+1);
159                }
160                return groups;
161            }
162        }
163        return null;
164    }
165
166
167    /**
168     * Validate a value against the set of regular expressions
169     * returning a String value of the aggregated groups.
170     *
171     * @param value The value to validate.
172     * @return Aggregated String value comprised of the
173     * <i>groups</i> matched if valid or <code>null</code> if invalid
174     */
175    public String validate(String value) {
176        if (value == null) {
177            return null;
178        }
179        for (int i = 0; i < patterns.length; i++) {
180            Matcher matcher = patterns[i].matcher(value);
181            if (matcher.matches()) {
182                int count = matcher.groupCount();
183                if (count == 1) {
184                    return matcher.group(1);
185                }
186                StringBuffer buffer = new StringBuffer();
187                for (int j = 0; j < count; j++) {
188                    String component = matcher.group(j+1);
189                    if (component != null) {
190                        buffer.append(component);
191                    }
192                }
193                return buffer.toString();
194            }
195        }
196        return null;
197    }
198
199    /**
200     * Provide a String representation of this validator.
201     * @return A String representation of this validator
202     */
203    public String toString() {
204        StringBuffer buffer = new StringBuffer();
205        buffer.append("RegexValidator{");
206        for (int i = 0; i < patterns.length; i++) {
207            if (i > 0) {
208                buffer.append(",");
209            }
210            buffer.append(patterns[i].pattern());
211        }
212        buffer.append("}");
213        return buffer.toString();
214    }
215
216}