ViewPattern { public Map<String, String> matches(final String in) { Assert.notNull(in); String url; int index; if ((index = in.indexOf('?')) != -1) { url = in.substring(0, index); } else { url = in; } final int count = StringUtils.countMatches(url, "."); if (count >= 2) { final int lastIndex = url.lastIndexOf("."); final String sub = url.substring(0, lastIndex); url = sub.replaceAll("\\.", "§") + url.substring(lastIndex); } if (parts.isEmpty()) { if (url.isEmpty()) { return new HashMap<>(); } else { return null; } } final Matcher matcher = pattern.matcher(url); if (!matcher.matches()) { return null; } final Map<String, String> variables = new HashMap<>(); for (final Map.Entry<String, Variable> entry : this.variableMap.entrySet()) { String value = matcher.group(entry.getKey()); if (value == null) { value = ""; } if (value.isEmpty()) { if (entry.getValue().getType() == Variable.TYPE_REQUIRED) { return null; } if (entry.getValue().hasDefaultValue()) { value = entry.getValue().getDefaultValue(); } else { continue; } } variables.put(entry.getKey(), value); } return variables; } ViewPattern(final String in); Map<String, String> matches(final String in); @Override String toString();  }
@Test void matches() { CoreConfig.debug = false; final String username = getRandomString(10) + "_"; final ViewPattern pattern1 = new ViewPattern("user/<username>"); Map<String, String> map = pattern1.matches("user/" + username); Assert.assertNotNull(map); Assert.assertTrue(map.containsKey("username")); Assert.assertEquals(map.get("username"), username); Assert.assertNull(pattern1.matches("user/Username/other")); final ViewPattern pattern2 = new ViewPattern("news/[page=0]"); map = pattern2.matches("news"); Assert.assertNotNull(map); Assert.assertTrue(map.containsKey("page")); Assert.assertEquals("0", map.get("page")); map = pattern2.matches("news/foo"); Assert.assertNotNull(map); Assert.assertTrue(map.containsKey("page")); Assert.assertEquals(map.get("page"), "foo"); Assert.assertNull(pattern2.matches("news/foo/bar")); final ViewPattern pattern3 = new ViewPattern("user/<username>/posts/[page]"); map = pattern3.matches("user/" + username + "/posts"); Assert.assertNotNull(map); Assert.assertTrue(map.containsKey("username")); Assert.assertEquals(username, map.get("username")); Assert.assertFalse("\"" + map.get("page") + "\" is not supposed to exist", map.containsKey("page")); map = pattern3.matches("user/" + username + "/posts/10"); Assert.assertNotNull(map); Assert.assertTrue(map.containsKey("username")); Assert.assertEquals(username, map.get("username")); Assert.assertTrue(map.containsKey("page")); Assert.assertEquals("10", map.get("page")); Assert.assertNull(pattern3.matches("user/")); Assert.assertNull(pattern3.matches("user/Username/posts/foo/bar")); final ViewPattern pattern4 = new ViewPattern("/test/file.extension.[extension]/"); map = pattern4.matches("/test/file.extension.com"); Assert.assertNotNull(map); Assert.assertTrue(map.containsKey("extension")); Assert.assertEquals("com", map.get("extension")); }