@Test(expected = IllegalArgumentException.class) public void pathTemplateCreateShouldFailIfTwoTemplateWithSameNameAreDetected() throws Exception { try { RegexPathTemplate.create("http: } catch (Exception e) { assertEquals("Template name 'template-name' is already defined!", e.getMessage()); throw e; } }
public static RegexPathTemplate create(String urlTemplate) { StringBuffer baseUrl = new StringBuffer(); Map<String, PathTemplate> templates = new HashMap<String, PathTemplate>(); CurlyBraceTokenizer t = new CurlyBraceTokenizer(urlTemplate); while (t.hasNext()) { String tok = t.next(); if (CurlyBraceTokenizer.insideBraces(tok)) { tok = CurlyBraceTokenizer.stripBraces(tok); int index = tok.indexOf(':'); String name; Pattern validationPattern; if(index > -1) { name = tok.substring(0, index); validationPattern = Pattern.compile("^" + tok.substring(index + 1) + "$"); }else{ name = tok; validationPattern = DEFAULT_VALIDATION_PATTERN; } Validate.isTrue(TEMPLATE_NAME_PATTERN.matcher(name).matches(), "Template name '%s' doesn't match the expected format: %s", name, TEMPLATE_NAME_PATTERN); Validate.isFalse(templates.containsKey(name), "Template name '%s' is already defined!", name); templates.put(name, new PathTemplate(name, validationPattern)); baseUrl.append("{").append(name).append("}"); } else { baseUrl.append(tok); } } String url = baseUrl.toString(); isTrue(!Urls.hasQueryString(url), "Given url contains a query string: %s", url); return new RegexPathTemplate(url, templates); }
RegexPathTemplate implements PathTemplate { public static RegexPathTemplate create(String urlTemplate) { StringBuffer baseUrl = new StringBuffer(); Map<String, PathTemplate> templates = new HashMap<String, PathTemplate>(); CurlyBraceTokenizer t = new CurlyBraceTokenizer(urlTemplate); while (t.hasNext()) { String tok = t.next(); if (CurlyBraceTokenizer.insideBraces(tok)) { tok = CurlyBraceTokenizer.stripBraces(tok); int index = tok.indexOf(':'); String name; Pattern validationPattern; if(index > -1) { name = tok.substring(0, index); validationPattern = Pattern.compile("^" + tok.substring(index + 1) + "$"); }else{ name = tok; validationPattern = DEFAULT_VALIDATION_PATTERN; } Validate.isTrue(TEMPLATE_NAME_PATTERN.matcher(name).matches(), "Template name '%s' doesn't match the expected format: %s", name, TEMPLATE_NAME_PATTERN); Validate.isFalse(templates.containsKey(name), "Template name '%s' is already defined!", name); templates.put(name, new PathTemplate(name, validationPattern)); baseUrl.append("{").append(name).append("}"); } else { baseUrl.append(tok); } } String url = baseUrl.toString(); isTrue(!Urls.hasQueryString(url), "Given url contains a query string: %s", url); return new RegexPathTemplate(url, templates); } }
RegexPathTemplate implements PathTemplate { public static RegexPathTemplate create(String urlTemplate) { StringBuffer baseUrl = new StringBuffer(); Map<String, PathTemplate> templates = new HashMap<String, PathTemplate>(); CurlyBraceTokenizer t = new CurlyBraceTokenizer(urlTemplate); while (t.hasNext()) { String tok = t.next(); if (CurlyBraceTokenizer.insideBraces(tok)) { tok = CurlyBraceTokenizer.stripBraces(tok); int index = tok.indexOf(':'); String name; Pattern validationPattern; if(index > -1) { name = tok.substring(0, index); validationPattern = Pattern.compile("^" + tok.substring(index + 1) + "$"); }else{ name = tok; validationPattern = DEFAULT_VALIDATION_PATTERN; } Validate.isTrue(TEMPLATE_NAME_PATTERN.matcher(name).matches(), "Template name '%s' doesn't match the expected format: %s", name, TEMPLATE_NAME_PATTERN); Validate.isFalse(templates.containsKey(name), "Template name '%s' is already defined!", name); templates.put(name, new PathTemplate(name, validationPattern)); baseUrl.append("{").append(name).append("}"); } else { baseUrl.append(tok); } } String url = baseUrl.toString(); isTrue(!Urls.hasQueryString(url), "Given url contains a query string: %s", url); return new RegexPathTemplate(url, templates); } private  RegexPathTemplate(String urlTemplate, Map<String, PathTemplate> templates); }
RegexPathTemplate implements PathTemplate { public static RegexPathTemplate create(String urlTemplate) { StringBuffer baseUrl = new StringBuffer(); Map<String, PathTemplate> templates = new HashMap<String, PathTemplate>(); CurlyBraceTokenizer t = new CurlyBraceTokenizer(urlTemplate); while (t.hasNext()) { String tok = t.next(); if (CurlyBraceTokenizer.insideBraces(tok)) { tok = CurlyBraceTokenizer.stripBraces(tok); int index = tok.indexOf(':'); String name; Pattern validationPattern; if(index > -1) { name = tok.substring(0, index); validationPattern = Pattern.compile("^" + tok.substring(index + 1) + "$"); }else{ name = tok; validationPattern = DEFAULT_VALIDATION_PATTERN; } Validate.isTrue(TEMPLATE_NAME_PATTERN.matcher(name).matches(), "Template name '%s' doesn't match the expected format: %s", name, TEMPLATE_NAME_PATTERN); Validate.isFalse(templates.containsKey(name), "Template name '%s' is already defined!", name); templates.put(name, new PathTemplate(name, validationPattern)); baseUrl.append("{").append(name).append("}"); } else { baseUrl.append(tok); } } String url = baseUrl.toString(); isTrue(!Urls.hasQueryString(url), "Given url contains a query string: %s", url); return new RegexPathTemplate(url, templates); } private  RegexPathTemplate(String urlTemplate, Map<String, PathTemplate> templates); PathBuilder getBuilder(Charset charset); static RegexPathTemplate create(String urlTemplate); }
RegexPathTemplate implements PathTemplate { public static RegexPathTemplate create(String urlTemplate) { StringBuffer baseUrl = new StringBuffer(); Map<String, PathTemplate> templates = new HashMap<String, PathTemplate>(); CurlyBraceTokenizer t = new CurlyBraceTokenizer(urlTemplate); while (t.hasNext()) { String tok = t.next(); if (CurlyBraceTokenizer.insideBraces(tok)) { tok = CurlyBraceTokenizer.stripBraces(tok); int index = tok.indexOf(':'); String name; Pattern validationPattern; if(index > -1) { name = tok.substring(0, index); validationPattern = Pattern.compile("^" + tok.substring(index + 1) + "$"); }else{ name = tok; validationPattern = DEFAULT_VALIDATION_PATTERN; } Validate.isTrue(TEMPLATE_NAME_PATTERN.matcher(name).matches(), "Template name '%s' doesn't match the expected format: %s", name, TEMPLATE_NAME_PATTERN); Validate.isFalse(templates.containsKey(name), "Template name '%s' is already defined!", name); templates.put(name, new PathTemplate(name, validationPattern)); baseUrl.append("{").append(name).append("}"); } else { baseUrl.append(tok); } } String url = baseUrl.toString(); isTrue(!Urls.hasQueryString(url), "Given url contains a query string: %s", url); return new RegexPathTemplate(url, templates); } private  RegexPathTemplate(String urlTemplate, Map<String, PathTemplate> templates); PathBuilder getBuilder(Charset charset); static RegexPathTemplate create(String urlTemplate);  }