001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019 package org.apache.shiro.util;
020
021 import java.util.regex.Pattern;
022 import java.util.regex.Matcher;
023
024 /**
025 * {@code PatternMatcher} implementation that uses standard {@link java.util.regex} objects.
026 *
027 * @see Pattern
028 * @author Les Hazlewood
029 * @since 1.0
030 */
031 public class RegExPatternMatcher implements PatternMatcher {
032
033 /**
034 * Simple implementation that merely uses the default pattern comparison logic provided by the
035 * JDK.
036 * <p/>This implementation essentially executes the following:
037 * <pre>
038 * Pattern p = Pattern.compile(pattern);
039 * Matcher m = p.matcher(source);
040 * return m.matches();</pre>
041 * @param pattern the pattern to match against
042 * @param source the source to match
043 * @return {@code true} if the source matches the required pattern, {@code false} otherwise.
044 */
045 public boolean matches(String pattern, String source) {
046 if (pattern == null) {
047 throw new IllegalArgumentException("pattern argument cannot be null.");
048 }
049 Pattern p = Pattern.compile(pattern);
050 Matcher m = p.matcher(source);
051 return m.matches();
052 }
053 }