View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.filter;
22  
23  import java.util.regex.Pattern;
24  
25  import net.sf.jour.log.Logger;
26  
27  /**
28   * @author michaellif
29   */
30  public abstract class BasicFilter extends MatchFilter {
31  
32  	protected static final Logger log = Logger.getLogger();
33  
34  	public static Pattern getGlobPattern(String globPattern) {
35  		char[] gPat = globPattern.toCharArray();
36  		char[] rPat = new char[gPat.length * 2 + 2];
37  		boolean inBrackets = false;
38  
39  		int j = 0;
40  
41  		// Should match full string
42  		// "foo" should not match "1foo".
43  		rPat[j++] = '^';
44  
45  		for (int i = 0; i < gPat.length; i++) {
46  			switch (gPat[i]) {
47  			case '*':
48  
49  				if (!inBrackets) {
50  					rPat[j++] = '.';
51  				}
52  
53  				rPat[j++] = '*';
54  
55  				break;
56  
57  			case '?':
58  				rPat[j++] = inBrackets ? '?' : '.';
59  
60  				break;
61  
62  			case '[':
63  				inBrackets = true;
64  				rPat[j++] = gPat[i];
65  
66  				if (i < (gPat.length - 1)) {
67  					switch (gPat[i + 1]) {
68  					case '!':
69  					case '^':
70  						rPat[j++] = '^';
71  						i++;
72  
73  						break;
74  
75  					case ']':
76  						// vlads: [] should be escaped for arrays
77  						rPat[j - 1] = '\\';
78  						rPat[j++] = '[';
79  						rPat[j++] = '\\';
80  
81  						rPat[j++] = gPat[++i];
82  
83  						break;
84  					}
85  				}
86  
87  				break;
88  
89  			case ']':
90  				rPat[j++] = gPat[i];
91  				inBrackets = false;
92  
93  				break;
94  
95  			case '\\':
96  				rPat[j++] = '\\';
97  
98  				if ((i < (gPat.length - 1)) && ("*?[]".indexOf(gPat[i + 1]) >= 0)) {
99  					rPat[j++] = gPat[++i];
100 				} else {
101 					rPat[j++] = '\\';
102 				}
103 
104 				break;
105 
106 			default:
107 
108 				if (!Character.isLetterOrDigit(gPat[i])) {
109 					rPat[j++] = '\\';
110 				}
111 
112 				rPat[j++] = gPat[i];
113 
114 				break;
115 			}
116 		}
117 
118 		// Should match full string
119 		// "foo" should not match "foo2".
120 		rPat[j++] = '$';
121 
122 		// System.out.println(new String(rPat, 0, j));
123 
124 		return Pattern.compile(new String(rPat, 0, j),  Pattern.CASE_INSENSITIVE);
125 	}
126 
127 	protected static boolean accept(Pattern  pattern, String str) {
128 		if (str == null) {
129 			return false;
130 		}
131 		return pattern.matcher(str).matches();
132 	}
133 
134 }