View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.util;
20  
21  import java.util.regex.Pattern;
22  import java.util.regex.Matcher;
23  
24  /**
25   * {@code PatternMatcher} implementation that uses standard {@link java.util.regex} objects.
26   *
27   * @see Pattern
28   * @since 1.0
29   */
30  public class RegExPatternMatcher implements PatternMatcher {
31  
32      /**
33       * Simple implementation that merely uses the default pattern comparison logic provided by the
34       * JDK.
35       * <p/>This implementation essentially executes the following:
36       * <pre>
37       * Pattern p = Pattern.compile(pattern);
38       * Matcher m = p.matcher(source);
39       * return m.matches();</pre>
40       * @param pattern the pattern to match against
41       * @param source  the source to match
42       * @return {@code true} if the source matches the required pattern, {@code false} otherwise.
43       */
44      public boolean matches(String pattern, String source) {
45          if (pattern == null) {
46              throw new IllegalArgumentException("pattern argument cannot be null.");
47          }
48          Pattern p = Pattern.compile(pattern);
49          Matcher m = p.matcher(source);
50          return m.matches();
51      }
52  }