001 // Copyright 2006, 2007, 2008 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry5.ioc.internal;
016
017 import java.util.regex.Pattern;
018
019 /**
020 * Used when matching identifiers. In the early days of T5 IoC, matching was based on shell-style glob matches (a '*'
021 * could represent zero or more characters). But that was limiting so now we check to see if the provided pattern looks
022 * like a glob (just characters and asterisks, for compatibility with older code) and, if not, we assume it is a regular
023 * expression.
024 */
025 public class GlobPatternMatcher
026 {
027 private final Pattern pattern;
028
029 private final static Pattern oldStyleGlob =
030 Pattern.compile("[a-z\\*]+", Pattern.CASE_INSENSITIVE);
031
032 public GlobPatternMatcher(String pattern)
033 {
034 this.pattern = compilePattern(pattern);
035 }
036
037 private static Pattern compilePattern(String pattern)
038 {
039 return Pattern.compile(createRegexpFromGlob(pattern), Pattern.CASE_INSENSITIVE);
040 }
041
042 private static String createRegexpFromGlob(String pattern)
043 {
044 return oldStyleGlob.matcher(pattern).matches()
045 ? pattern.replace("*", ".*")
046 : pattern;
047 }
048
049
050 public boolean matches(String input)
051 {
052 return pattern.matcher(input).matches();
053 }
054 }