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  //Java1.3 import java.util.regex.Pattern;
24  import org.apache.log4j.Logger;
25  import org.apache.regexp.RE;
26  import org.apache.regexp.RESyntaxException;
27  
28  /***
29   * @author michaellif  TODO To change the template for this generated type
30   *         comment go to Window - Preferences - Java - Code Style - Code
31   *         Templates
32   */
33  public abstract class BasicFilter extends MatchFilter {
34  
35      protected static final Logger log = Logger.getLogger(BasicFilter.class);
36      
37      /***
38       * DOCUMENT ME!
39       *
40       * @param globPattern DOCUMENT ME!
41       */
42      public static RE getGlobPattern(String globPattern) {
43          char[] gPat = globPattern.toCharArray();
44          char[] rPat = new char[gPat.length * 2 + 2];
45          boolean inBrackets = false;
46          StringBuffer buf = new StringBuffer();
47          int j = 0;
48  
49  		// Should match full string
50  		// "foo" should not match "1foo".
51  		rPat[j++] = '^';
52  		
53          for (int i = 0; i < gPat.length; i++) {
54              switch (gPat[i]) {
55              case '*':
56  
57                  if (!inBrackets) {
58                      rPat[j++] = '.';
59                  }
60  
61                  rPat[j++] = '*';
62  
63                  break;
64  
65              case '?':
66                  rPat[j++] = inBrackets ? '?' : '.';
67  
68                  break;
69  
70              case '[':
71                  inBrackets = true;
72                  rPat[j++] = gPat[i];
73  
74                  if (i < (gPat.length - 1)) {
75                      switch (gPat[i + 1]) {
76                      case '!':
77                      case '^':
78                          rPat[j++] = '^';
79                          i++;
80  
81                          break;
82  
83                      case ']':
84                          // vlads: [] should be escaped for arrays
85                          rPat[j-1] = '//';
86                          rPat[j++] = '[';
87                          rPat[j++] = '//';
88                          
89                          rPat[j++] = gPat[++i];
90  
91                          break;
92                      }
93                  }
94  
95                  break;
96  
97              case ']':
98                  rPat[j++] = gPat[i];
99                  inBrackets = false;
100 
101                 break;
102 
103             case '//':
104                 rPat[j++] = '//';
105 
106                 if ((i < (gPat.length - 1)) &&
107                         ("*?[]".indexOf(gPat[i + 1]) >= 0)) {
108                     rPat[j++] = gPat[++i];
109                 } else {
110                     rPat[j++] = '//';
111                 }
112 
113                 break;
114 
115             default:
116 
117                 if (!Character.isLetterOrDigit(gPat[i])) {
118                     rPat[j++] = '//';
119                 }
120 
121                 rPat[j++] = gPat[i];
122 
123                 break;
124             }
125         }
126 
127 		// Should match full string
128 		// "foo" should not match "foo2".
129 		rPat[j++] = '$';
130 
131 		//System.out.println(new String(rPat, 0, j));
132 		
133         try {
134             //Java1.3
135             //return Pattern.compile(new String(rPat, 0, j),
136             //        Pattern.CASE_INSENSITIVE);
137             return new RE(new String(rPat, 0, j),
138             			 RE.MATCH_CASEINDEPENDENT);
139         } catch (RESyntaxException e) {
140             // e.printStackTrace();
141             log.error("Regexpr Error in [" 
142                     + new String(rPat, 0, j) 
143                     + "] for " + globPattern, e);
144             return new RE("!!!TODO");
145         }
146     }
147 
148     /***
149      * DOCUMENT ME!
150      *
151      * @param str DOCUMENT ME!
152      *
153      * @return DOCUMENT ME!
154      */
155     protected static boolean accept(RE pattern, String str) {
156         if (str == null) {
157             return false;
158         }
159 		//Java1.3
160         //return pattern.matcher(str).matches();
161 		return pattern.match(str);
162     }
163 
164 }