KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > SimpleRegexMatcher


1 /* $Id: SimpleRegexMatcher.java 179716 2005-06-03 04:06:00Z skitching $
2  *
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.commons.digester;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 /**
24  * <p>Simple regex pattern matching algorithm.</p>
25  *
26  * <p>This uses just two wildcards:
27  * <ul>
28  * <li><code>*</code> matches any sequence of none, one or more characters
29  * <li><code>?</code> matches any one character
30  * </ul>
31  * Escaping these wildcards is not supported .</p>
32  *
33  * @since 1.5
34  */

35
36 public class SimpleRegexMatcher extends RegexMatcher {
37     
38     // --------------------------------------------------------- Fields
39

40     /** Default log (class wide) */
41     private static final Log baseLog = LogFactory.getLog(SimpleRegexMatcher.class);
42     
43     /** Custom log (can be set per object) */
44     private Log log = baseLog;
45     
46     // --------------------------------------------------------- Properties
47

48     /**
49      * Gets the <code>Log</code> implementation.
50      */

51     public Log getLog() {
52         return log;
53     }
54     
55     /**
56      * Sets the current <code>Log</code> implementation used by this class.
57      */

58     public void setLog(Log log) {
59         this.log = log;
60     }
61     
62     // --------------------------------------------------------- Public Methods
63

64     /**
65      * Matches using simple regex algorithm.
66      *
67      *
68      * @param basePattern the standard digester path representing the element
69      * @param regexPattern the regex pattern the path will be tested against
70      * @return true if the given pattern matches the given path
71      */

72     public boolean match(String JavaDoc basePattern, String JavaDoc regexPattern) {
73         // check for nulls
74
if (basePattern == null || regexPattern == null) {
75             return false;
76         }
77         return match(basePattern, regexPattern, 0, 0);
78     }
79     
80     // --------------------------------------------------------- Implementations Methods
81

82     /**
83      * Implementation of regex matching algorithm.
84      * This calls itself recursively.
85      */

86     private boolean match(String JavaDoc basePattern, String JavaDoc regexPattern, int baseAt, int regexAt) {
87         if (log.isTraceEnabled()) {
88             log.trace("Base: " + basePattern);
89             log.trace("Regex: " + regexPattern);
90             log.trace("Base@" + baseAt);
91             log.trace("Regex@" + regexAt);
92         }
93         
94         // check bounds
95
if (regexAt >= regexPattern.length()) {
96             // maybe we've got a match
97
if (baseAt >= basePattern.length()) {
98                 // ok!
99
return true;
100             }
101             // run out early
102
return false;
103             
104         } else {
105             if (baseAt >= basePattern.length()) {
106                 // run out early
107
return false;
108             }
109         }
110         
111         // ok both within bounds
112
char regexCurrent = regexPattern.charAt(regexAt);
113         switch (regexCurrent) {
114             case '*':
115                 // this is the tricky case
116
// check for terminal
117
if (++regexAt >= regexPattern.length()) {
118                     // this matches anything let - so return true
119
return true;
120                 }
121                 // go through every subsequent apperance of the next character
122
// and so if the rest of the regex matches
123
char nextRegex = regexPattern.charAt(regexAt);
124                 if (log.isTraceEnabled()) {
125                     log.trace("Searching for next '" + nextRegex + "' char");
126                 }
127                 int nextMatch = basePattern.indexOf(nextRegex, baseAt);
128                 while (nextMatch != -1) {
129                     if (log.isTraceEnabled()) {
130                         log.trace("Trying '*' match@" + nextMatch);
131                     }
132                     if (match(basePattern, regexPattern, nextMatch, regexAt)) {
133                         return true;
134                     }
135                     nextMatch = basePattern.indexOf(nextRegex, nextMatch + 1);
136                 }
137                 log.trace("No matches found.");
138                 return false;
139                 
140             case '?':
141                 // this matches anything
142
return match(basePattern, regexPattern, ++baseAt, ++regexAt);
143             
144             default:
145                 if (log.isTraceEnabled()) {
146                     log.trace("Camparing " + regexCurrent + " to " + basePattern.charAt(baseAt));
147                 }
148                 if (regexCurrent == basePattern.charAt(baseAt)) {
149                     // still got more to go
150
return match(basePattern, regexPattern, ++baseAt, ++regexAt);
151                 }
152                 return false;
153         }
154     }
155 }
156
Popular Tags