PathElement { public static PathElement pathElement(String path) throws PatternSyntaxException { String replacedCurlySegment = replaceEnclosedCurlyBraces(path); String[] split = TEMPLATE_PARAM_PATTERN.split(replacedCurlySegment); Matcher withPathParam = TEMPLATE_PARAM_PATTERN.matcher(replacedCurlySegment); int i = 0; StringBuilder buffer = new StringBuilder(); if (i < split.length) buffer.append(split[i++]); int groupNumber = 1; List<Group> groups = new ArrayList<Group>(); while (withPathParam.find()) { String name = withPathParam.group(1); buffer.append("("); if (withPathParam.group(3) == null) { buffer.append(TEMPLATE_PARAM_DEFAULT_REGEX); groups.add(new Group(name, groupNumber++)); } else { String expr = withPathParam.group(3); expr = recoverEnclosedCurlyBraces(expr); buffer.append(expr); groups.add(new Group(name, groupNumber++)); groupNumber += groupCount(expr); } buffer.append(")"); if (i < split.length) buffer.append(split[i++]); } return new PathElement(path, groups, buffer.toString()); } PathElement(String value, List<Group> groups, String regex); static PathElement pathElement(String path); String getValue(); boolean isTemplate(); boolean matches(String path); String resolve(String name, String path);  }
@Test public void testBadTemplateExpression() { try { PathElement.pathElement("{bad *}"); fail(); } catch (PatternSyntaxException e) { } try { PathElement.pathElement("{foo: *}/something/{bad: {{{}"); fail(); } catch (PatternSyntaxException e) { } }