001: /* $Id: SimpleRegexMatcher.java 471661 2006-11-06 08:09:25Z skitching $
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package org.apache.commons.digester;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: /**
025: * <p>Simple regex pattern matching algorithm.</p>
026: *
027: * <p>This uses just two wildcards:
028: * <ul>
029: * <li><code>*</code> matches any sequence of none, one or more characters
030: * <li><code>?</code> matches any one character
031: * </ul>
032: * Escaping these wildcards is not supported .</p>
033: *
034: * @since 1.5
035: */
036:
037: public class SimpleRegexMatcher extends RegexMatcher {
038:
039: // --------------------------------------------------------- Fields
040:
041: /** Default log (class wide) */
042: private static final Log baseLog = LogFactory
043: .getLog(SimpleRegexMatcher.class);
044:
045: /** Custom log (can be set per object) */
046: private Log log = baseLog;
047:
048: // --------------------------------------------------------- Properties
049:
050: /**
051: * Gets the <code>Log</code> implementation.
052: */
053: public Log getLog() {
054: return log;
055: }
056:
057: /**
058: * Sets the current <code>Log</code> implementation used by this class.
059: */
060: public void setLog(Log log) {
061: this .log = log;
062: }
063:
064: // --------------------------------------------------------- Public Methods
065:
066: /**
067: * Matches using simple regex algorithm.
068: *
069: *
070: * @param basePattern the standard digester path representing the element
071: * @param regexPattern the regex pattern the path will be tested against
072: * @return true if the given pattern matches the given path
073: */
074: public boolean match(String basePattern, String regexPattern) {
075: // check for nulls
076: if (basePattern == null || regexPattern == null) {
077: return false;
078: }
079: return match(basePattern, regexPattern, 0, 0);
080: }
081:
082: // --------------------------------------------------------- Implementations Methods
083:
084: /**
085: * Implementation of regex matching algorithm.
086: * This calls itself recursively.
087: */
088: private boolean match(String basePattern, String regexPattern,
089: int baseAt, int regexAt) {
090: if (log.isTraceEnabled()) {
091: log.trace("Base: " + basePattern);
092: log.trace("Regex: " + regexPattern);
093: log.trace("Base@" + baseAt);
094: log.trace("Regex@" + regexAt);
095: }
096:
097: // check bounds
098: if (regexAt >= regexPattern.length()) {
099: // maybe we've got a match
100: if (baseAt >= basePattern.length()) {
101: // ok!
102: return true;
103: }
104: // run out early
105: return false;
106:
107: } else {
108: if (baseAt >= basePattern.length()) {
109: // run out early
110: return false;
111: }
112: }
113:
114: // ok both within bounds
115: char regexCurrent = regexPattern.charAt(regexAt);
116: switch (regexCurrent) {
117: case '*':
118: // this is the tricky case
119: // check for terminal
120: if (++regexAt >= regexPattern.length()) {
121: // this matches anything let - so return true
122: return true;
123: }
124: // go through every subsequent apperance of the next character
125: // and so if the rest of the regex matches
126: char nextRegex = regexPattern.charAt(regexAt);
127: if (log.isTraceEnabled()) {
128: log
129: .trace("Searching for next '" + nextRegex
130: + "' char");
131: }
132: int nextMatch = basePattern.indexOf(nextRegex, baseAt);
133: while (nextMatch != -1) {
134: if (log.isTraceEnabled()) {
135: log.trace("Trying '*' match@" + nextMatch);
136: }
137: if (match(basePattern, regexPattern, nextMatch, regexAt)) {
138: return true;
139: }
140: nextMatch = basePattern.indexOf(nextRegex,
141: nextMatch + 1);
142: }
143: log.trace("No matches found.");
144: return false;
145:
146: case '?':
147: // this matches anything
148: return match(basePattern, regexPattern, ++baseAt, ++regexAt);
149:
150: default:
151: if (log.isTraceEnabled()) {
152: log.trace("Camparing " + regexCurrent + " to "
153: + basePattern.charAt(baseAt));
154: }
155: if (regexCurrent == basePattern.charAt(baseAt)) {
156: // still got more to go
157: return match(basePattern, regexPattern, ++baseAt,
158: ++regexAt);
159: }
160: return false;
161: }
162: }
163: }
|