001/* $Id: SimpleRegexMatcher.java 992060 2010-09-02 19:09:47Z simonetripodi $
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
019package org.apache.commons.digester;
020
021import org.apache.commons.logging.Log;
022import 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
037public class SimpleRegexMatcher extends RegexMatcher {
038    
039    // --------------------------------------------------------- Fields
040    
041    /** Default log (class wide) */
042    private static final Log baseLog = LogFactory.getLog(SimpleRegexMatcher.class);
043    
044    /** Custom log (can be set per object) */
045    private Log log = baseLog;
046    
047    // --------------------------------------------------------- Properties
048    
049    /** 
050     * Gets the <code>Log</code> implementation.
051     */
052    public Log getLog() {
053        return log;
054    }
055    
056    /**
057     * Sets the current <code>Log</code> implementation used by this class.
058     */
059    public void setLog(Log log) {
060        this.log = log;
061    }
062    
063    // --------------------------------------------------------- Public Methods
064    
065    /** 
066     * Matches using simple regex algorithm.
067     * 
068     *
069     * @param basePattern the standard digester path representing the element
070     * @param regexPattern the regex pattern the path will be tested against
071     * @return true if the given pattern matches the given path
072     */
073    @Override
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, int baseAt, int regexAt) {
089        if (log.isTraceEnabled()) {
090            log.trace("Base: " + basePattern);
091            log.trace("Regex: " + regexPattern);
092            log.trace("Base@" + baseAt);
093            log.trace("Regex@" + regexAt);
094        }
095        
096        // check bounds
097        if (regexAt >= regexPattern.length()) {
098            // maybe we've got a match
099            if (baseAt >= basePattern.length()) {
100                // ok!
101                return true;
102            }
103            // run out early
104            return false;
105            
106        } else {
107            if (baseAt >= basePattern.length()) {
108                // run out early
109                return false;
110            }
111        }
112        
113        // ok both within bounds
114        char regexCurrent = regexPattern.charAt(regexAt);
115        switch (regexCurrent) {
116            case '*':
117                // this is the tricky case
118                // check for terminal 
119                if (++regexAt >= regexPattern.length()) {
120                    // this matches anything let - so return true
121                    return true;
122                }
123                // go through every subsequent apperance of the next character
124                // and so if the rest of the regex matches
125                char nextRegex = regexPattern.charAt(regexAt);
126                if (log.isTraceEnabled()) {
127                    log.trace("Searching for next '" + nextRegex + "' char");
128                }
129                int nextMatch = basePattern.indexOf(nextRegex, baseAt);
130                while (nextMatch != -1) {
131                    if (log.isTraceEnabled()) {
132                        log.trace("Trying '*' match@" + nextMatch);
133                    }
134                    if (match(basePattern, regexPattern, nextMatch, regexAt)) {
135                        return true;
136                    }
137                    nextMatch = basePattern.indexOf(nextRegex, nextMatch + 1);
138                }
139                log.trace("No matches found.");
140                return false;
141                
142            case '?':
143                // this matches anything
144                return match(basePattern, regexPattern, ++baseAt, ++regexAt);
145            
146            default:
147                if (log.isTraceEnabled()) {
148                    log.trace("Camparing " + regexCurrent + " to " + basePattern.charAt(baseAt));
149                }
150                if (regexCurrent == basePattern.charAt(baseAt)) {
151                    // still got more to go
152                    return match(basePattern, regexPattern, ++baseAt, ++regexAt);
153                }
154                return false;
155        }
156    }
157}