private static String toPathParamRegex(Map<String, String> groups, AtomicInteger counter, String group) { Matcher m = PATH_PARAM_PARTS.matcher(group); U.must(m.matches(), "Invalid path parameter, expected {var} or {var:regex} syntax!"); String name = m.group(1); String regex = U.or(m.group(2), DEFAULT_GROUP_REGEX); return toPathParamRegex(groups, counter, name, regex); }
public static PathPattern from(String path) { final Map<String, String> groups = U.map(); final AtomicInteger counter = new AtomicInteger(); String regex = Str.replace(path, PATH_PARAM_REGEX, gr -> toPathParamRegex(groups, counter, gr[1])); if (regex.equals("/*")) { regex = "/" + toPathParamRegex(groups, counter, ANY, ".*"); } else if (regex.endsWith("/*")) { regex = Str.trimr(regex, "/*"); regex += U.frmt("(?:/%s)?", toPathParamRegex(groups, counter, ANY, ".*")); } Pattern pattern = Pattern.compile(regex); return new PathPattern(path, pattern, groups); }
private static String toPathParamRegex(Map<String, String> groups, AtomicInteger counter, String group) { Matcher m = PATH_PARAM_PARTS.matcher(group); U.must(m.matches(), "Invalid path parameter, expected {var} or {var:regex} syntax!"); String name = m.group(1); String regex = U.or(m.group(2), DEFAULT_GROUP_REGEX); return toPathParamRegex(groups, counter, name, regex); }
public static PathPattern from(String path) { final Map<String, String> groups = U.map(); final AtomicInteger counter = new AtomicInteger(); String regex = Str.replace(path, PATH_PARAM_REGEX, new Mapper<String[], String>() { @Override public String map(String[] gr) throws Exception { return toPathParamRegex(groups, counter, gr[1]); } }); if (regex.equals("/*")) { regex = "/" + toPathParamRegex(groups, counter, ANY, ".*"); } else if (regex.endsWith("/*")) { regex = Str.trimr(regex, "/*"); regex += U.frmt("(?:/%s)?", toPathParamRegex(groups, counter, ANY, ".*")); } Pattern pattern = Pattern.compile(regex); return new PathPattern(path, pattern, groups); }