View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3    *
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
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
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.
15   */
16  
17  package edu.internet2.middleware.shibboleth.common.attribute.filtering.provider.match.basic;
18  
19  import java.util.regex.Pattern;
20  
21  /**
22   * Base class for match function that match a string value against a given regular expression.
23   */
24  public abstract class AbstractRegexMatchFunctor extends AbstractMatchFunctor {
25  
26      /** Regular expression to match. */
27      private Pattern regex;
28  
29      /**
30       * Gets the regular expression to match.
31       * 
32       * @return regular expression to match
33       */
34      public String getRegularExpression() {
35          return regex.pattern();
36      }
37  
38      /**
39       * Sets the regular expression to match.
40       * 
41       * @param expression regular expression to match
42       */
43      public void setRegularExpression(String expression) {
44          regex = Pattern.compile(expression);
45      }
46  
47      /**
48       * Matches the given value against the provided regular expression. {@link Object#toString()} is used to produce the
49       * string value to evaluate.
50       * 
51       * @param value the value to evaluate
52       * 
53       * @return true if the value matches the given match string, false if not
54       */
55      protected boolean isMatch(Object value) {
56          if (regex == null || value == null) {
57              return false;
58          }
59  
60          if (regex.matcher(value.toString()).matches()) {
61              return true;
62          }
63  
64          return false;
65      }
66  }