PathElement { public String resolve(String name, String path) { Matcher matcher = pattern.matcher(path); int start = 0; if (path.charAt(0) == '/') start++; for (Group group : groups) { if (matcher.find(start) && group.name.equals(name)) { String s = matcher.group(group.group); if ("".equals(s)) return null; return s; } } return null; } 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 testResolve() throws ParseException { assertEquals("foo", PathElement.pathElement("{path}").resolve("path", "/foo")); assertEquals("foo", PathElement.pathElement("{path}").resolve("path", "foo")); assertEquals("foo-foo", PathElement.pathElement("{path}bar").resolve("path", "foo-foobar")); assertEquals("foo/bar", PathElement.pathElement("{path: .*}").resolve("path", "/foo/bar")); assertEquals("foo/foo-bar", PathElement.pathElement("{path: .*}/bar").resolve("path", "foo/foo-bar/bar")); assertEquals("foo", PathElement.pathElement("{alphapath: [a-zA-Z]{3}}/{numpath: [0-9]*}").resolve("alphapath", "foo/123")); assertEquals("123", PathElement.pathElement("{alphapath: [a-zA-Z]{3}}/{numpath: [0-9]*}").resolve("numpath", "foo/123")); assertEquals("{foo}/{bar}", PathElement.pathElement("something/{weird: \\{foo\\}\\/\\{bar\\}}/else").resolve("weird", "something/{foo}/{bar}/else")); assertEquals("bar", PathElement.pathElement("foo/{required}/{optional: .*}").resolve("required", "foo/bar/")); assertNull(PathElement.pathElement("foo/{required}/{optional: .*}").resolve("optional", "foo/bar")); assertEquals("blah", PathElement.pathElement("foo/{required}/{optional: .*}").resolve("optional", "foo/bar/blah")); }