package org.theonefx.wcframework.mvc.wcweb; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import org.theonefx.wcframework.utils.StringUtils; public abstract class PathMatcher implements Comparable<PathMatcher> { protected static final Pattern expPattern = Pattern.compile("\\$\\{(.*?)\\}"); private final String expression; protected final String[][] paramterNames; protected final Pattern[] patterns; protected final String[] split; protected PathMatcher(String expression) { this.expression = expression; split = expression.split("/"); patterns = new Pattern[split.length]; paramterNames = new String[split.length][]; List<String> list = new ArrayList<String>(); for (int i = 0; i < split.length; i++) { String s = split[i]; Matcher matcher = expPattern.matcher(s); int found = 0; while (matcher.find()) { found++; String name = matcher.group(1); list.add(name); } if (found > 0) { String newExp = matcher.replaceAll("(.*)"); patterns[i] = Pattern.compile(newExp); } else { patterns[i] = null; } paramterNames[i] = list.toArray(new String[list.size()]); } } protected abstract int getLevel(); public abstract boolean match(String name, HttpServletRequest req); @Override public int compareTo(PathMatcher o) { return getLevel() - o.getLevel(); } protected String getExpression() { return expression; } public boolean sameAs(Object obj) { if (obj == null || !(obj instanceof PathMatcher)) { return false; } if (this == obj) { return true; } if (this.getClass() == obj.getClass()) { PathMatcher that = (PathMatcher) obj; if (StringUtils.equal(expression, that.expression, true)) { return true; } else { return false; } } else { return false; } } }