    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.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        public boolean match(String basePattern, String regexPattern) {
    074            // check for nulls
    075            if (basePattern == null || regexPattern == null) {
    076                return false;
    077            }
    078            return match(basePattern, regexPattern, 0, 0);
    079        }
    080
    081        // --------------------------------------------------------- Implementations Methods
    082
    083        /**
    084         * Implementation of regex matching algorithm.
    085         * This calls itself recursively.
    086         */
    087        private boolean match(String basePattern, String regexPattern, int baseAt, int regexAt) {
    088            if (log.isTraceEnabled()) {
    089                log.trace("Base: " + basePattern);
    090                log.trace("Regex: " + regexPattern);
    091                log.trace("Base@" + baseAt);
    092                log.trace("Regex@" + regexAt);
    093            }
    094
    095            // check bounds
    096            if (regexAt >= regexPattern.length()) {
    097                // maybe we've got a match
    098                if (baseAt >= basePattern.length()) {
    099                    // ok!
    100                    return true;
    101                }
    102                // run out early
    103                return false;
    104
    105            } else {
    106                if (baseAt >= basePattern.length()) {
    107                    // run out early
    108                    return false;
    109                }
    110            }
    111
    112            // ok both within bounds
    113            char regexCurrent = regexPattern.charAt(regexAt);
    114            switch (regexCurrent) {
    115                case '*':
    116                    // this is the tricky case
    117                    // check for terminal
    118                    if (++regexAt >= regexPattern.length()) {
    119                        // this matches anything let - so return true
    120                        return true;
    121                    }
    122                    // go through every subsequent apperance of the next character
    123                    // and so if the rest of the regex matches
    124                    char nextRegex = regexPattern.charAt(regexAt);
    125                    if (log.isTraceEnabled()) {
    126                        log.trace("Searching for next '" + nextRegex + "' char");
    127                    }
    128                    int nextMatch = basePattern.indexOf(nextRegex, baseAt);
    129                    while (nextMatch != -1) {
    130                        if (log.isTraceEnabled()) {
    131                            log.trace("Trying '*' match@" + nextMatch);
    132                        }
    133                        if (match(basePattern, regexPattern, nextMatch, regexAt)) {
    134                            return true;
    135                        }
    136                        nextMatch = basePattern.indexOf(nextRegex, nextMatch + 1);
    137                    }
    138                    log.trace("No matches found.");
    139                    return false;
    140
    141                case '?':
    142                    // this matches anything
    143                    return match(basePattern, regexPattern, ++baseAt, ++regexAt);
    144
    145                default:
    146                    if (log.isTraceEnabled()) {
    147                        log.trace("Camparing " + regexCurrent + " to " + basePattern.charAt(baseAt));
    148                    }
    149                    if (regexCurrent == basePattern.charAt(baseAt)) {
    150                        // still got more to go
    151                        return match(basePattern, regexPattern, ++baseAt, ++regexAt);
    152                    }
    153                    return false;
    154            }
    155        }
    156    }