RouteCompilerImpl implements RouteCompiler { public static String compileRegex(Route route, List<String> variables) { HashMap<String, String> requirements = route.getRequirements(); Iterator<String> variablesIterator = variables.iterator(); String patternString = quote(route.getPath()); while (variablesIterator.hasNext()) { String varName = variablesIterator.next(); String regex = "[^/]+"; if (requirements.containsKey(varName)) { regex = requirements.get(varName); } patternString = patternString.replaceFirst( String.format("\\\\\\{%s\\\\\\}", varName), java.util.regex.Matcher.quoteReplacement(String.format("(?<%s>%s)", varName, regex)) ); } return patternString; }  CompiledRoute compile(Route route); static List<String> detectVariables(Route route); static String compileRegex(Route route, List<String> variables); static String quote(String str);  }
@Test public void testCompileRegex() { Route route = new Route("github.com/mr5/{repository}", Activity.class); List<String> variables = RouteCompilerImpl.detectVariables(route); String regex = RouteCompilerImpl.compileRegex(route, variables); assertEquals("-> assert regex equals", "github\\.com/mr5/(?<repository>[^/]+)", regex); route.bind("repository", "\\w+"); regex = RouteCompilerImpl.compileRegex(route, variables); assertEquals("-> assert regex equals", "github\\.com/mr5/(?<repository>\\w+)", regex); route = new Route("github.com/{vendor}/{repository}", Activity.class); route.bind("vendor", "\\w+"); route.bind("repository", "\\w+"); variables = RouteCompilerImpl.detectVariables(route); regex = RouteCompilerImpl.compileRegex(route, variables); assertEquals("-> assert regex equals", "github\\.com/(?<vendor>\\w+)/(?<repository>\\w+)", regex); }