1 17 18 package org.apache.commons.digester; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 35 36 public class SimpleRegexMatcher extends RegexMatcher { 37 38 40 41 private static final Log baseLog = LogFactory.getLog(SimpleRegexMatcher.class); 42 43 44 private Log log = baseLog; 45 46 48 51 public Log getLog() { 52 return log; 53 } 54 55 58 public void setLog(Log log) { 59 this.log = log; 60 } 61 62 64 72 public boolean match(String basePattern, String regexPattern) { 73 if (basePattern == null || regexPattern == null) { 75 return false; 76 } 77 return match(basePattern, regexPattern, 0, 0); 78 } 79 80 82 86 private boolean match(String basePattern, String 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 if (regexAt >= regexPattern.length()) { 96 if (baseAt >= basePattern.length()) { 98 return true; 100 } 101 return false; 103 104 } else { 105 if (baseAt >= basePattern.length()) { 106 return false; 108 } 109 } 110 111 char regexCurrent = regexPattern.charAt(regexAt); 113 switch (regexCurrent) { 114 case '*': 115 if (++regexAt >= regexPattern.length()) { 118 return true; 120 } 121 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 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 return match(basePattern, regexPattern, ++baseAt, ++regexAt); 151 } 152 return false; 153 } 154 } 155 } 156 | Popular Tags |