View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2006 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  package com.mindtree.techworks.insight.receiver;
25  
26  import java.util.regex.Pattern;
27  
28  /**
29   * The <code>WildCardMatcher<code> class matches wildcard expression that are
30   * normally file names.
31   * @author m1003308(Siva Sajja)
32   * @version 1.0, 06/01/03
33   * 
34   */
35  
36  public final class WildCardMatcher {
37  	
38  	/**
39  	 * Array of character constants
40  	 */
41  	private static char[] WILDCARD_CHARS = {'*', '?','<', '>'};
42  	private static char[] SPECIAL_CHARS = {'.', '[', ']', '$', '^', '+', '{', '}', '(', ')'};
43  	
44  	/**
45  	 * Array of replacement strings
46  	 */
47  	private static final String[][] REPLACEMENT_REGEXS = {
48  		{"\\*", ".\\*"},
49  		{"\\<", "\\["},
50  		{"\\>", "\\]"},
51  	};
52  	
53  	/**
54  	 * The normal regular expression pattern
55  	 */
56  	private Pattern wildCardPattern;
57  	
58  	/**
59  	 * Constructor for this class
60  	 * @param regexp the regular expression to match against
61  	 */
62  	public WildCardMatcher(String regexp){
63  		regexp = convertWildCardsToRegex(regexp);
64  		this.wildCardPattern = Pattern.compile(regexp);
65  	}
66  	
67  	/**
68  	 * Matches the give string with the regular expression passed with the 
69  	 * constructor
70  	 * @param name	The name to be matched with the regular expression.
71  	 * @return	true if it matches the expresson, false otherwise
72  	 */
73  	public boolean matches(String name){
74  		return this.wildCardPattern.matcher(name).matches();
75  	}
76  	
77  	/**
78  	 * Checks for any wild card characters in the given string.
79  	 * @param input	The input string.
80  	 * @return	true if the input string has any wildcard characters, false otherwise
81  	 */
82  	public static boolean hasWildCardEntries(String input){
83  		for(int i = 0; i < WILDCARD_CHARS.length; i++){
84  			if(input.indexOf(WILDCARD_CHARS[i]) != -1)
85  				return true;
86  		}
87  		return false;
88  	}
89  	
90  	/**
91  	 * Converts the wildcard characters in the given string to a java specific
92  	 * regular expresson.
93  	 * @param pattern wild card pattern.
94  	 * @return	the java specific regular expression for the given wild card.
95  	 */
96  	private String convertWildCardsToRegex(String pattern){
97  		String resultString = pattern;
98  		if(hasWildCardEntries(pattern)){
99  			resultString = convertSpecialChars(pattern);
100 			for (int i =0; i < REPLACEMENT_REGEXS.length; i++) {
101 				resultString = resultString.replaceAll(REPLACEMENT_REGEXS[i][0], REPLACEMENT_REGEXS[i][1]);
102 			}
103 		}
104 		return resultString;
105 	}	
106 	
107 	/**
108 	 * Escapes any special non wildcard characters.
109 	 * @param regexp the wild card pattern.
110 	 * @return	the converted pattern.
111 	 */
112 	private static String convertSpecialChars(String regexp){
113 		for(int i = 0; i < SPECIAL_CHARS.length; i++){
114 			regexp = regexp.replaceAll("\\" + SPECIAL_CHARS[i], "\\\\" + SPECIAL_CHARS[i]);
115 		}
116 		return regexp;
117 	}
118 }