Stian, Good idea. I've added a getVariableNames() : List<String> method to the Template class.
Best regards, Jerome > -----Message d'origine----- > De : Stian Soiland [mailto:[EMAIL PROTECTED] > Envoyé : jeudi 7 juin 2007 12:55 > À : [email protected] > Objet : Re: Retrieve Template variable names? > > > On 7 Jun 2007, at 10:41, Stian Soiland wrote: > > > Do I have to re-parse the template myself? I guess looking for > > {([^}]+)} with regular expressions should be enough, but I wanted > > to check with the list first. > > I ended up with this simplified version (feel free to use): > > > /** > > * Find URI Template variable names according to > > * http://bitworking.org/projects/URI-Templates/ > > * > > * @param template The URI template to inspect > > * @return An ordered set of the URI template variable names > > */ > > public static LinkedHashSet<String> > uriTemplateVariables(String > > template) { > > LinkedHashSet<String> variableNames = new > LinkedHashSet<String>(); > > Pattern variablePattern = > Pattern.compile("\\{([^{}]+)\\}"); > > Matcher matcher = variablePattern.matcher(template); > > while (matcher.find()) { > > String variableName = matcher.group(1); > > variableNames.add(variableName); > > } > > return variableNames; > > } > > > @Test > > public void uriTemplateVariables() { > > String template = > "http://example.com/user/{user}/fish/{fish}? > > {gene}={1337}&user={user}"; > > LinkedHashSet<String> vars = > RestWorker.uriTemplateVariables > > (template); > > String[] expectedVars = {"user", "fish", > "gene", "1337"}; > > assertEquals(expectedVars.length, vars.size()); > > int i=0; > > for (String var : vars) { > > assertEquals(expectedVars[i++], var); > > } > > } > > > > @Test > > public void illegalTemplate() { > > String template = > "http://example.com/{{{user}}/fis}h/{{x}}/{}}"; > > LinkedHashSet<String> vars = > RestWorker.uriTemplateVariables > > (template); > > String[] expectedVars = {"user", "x"}; > > assertEquals(expectedVars.length, vars.size()); > > int i=0; > > for (String var : vars) { > > assertEquals(expectedVars[i++], var); > > } > > } > > > However this doesn't do any checking for illegal characters etc. > > -- > Stian Soiland, myGrid team > School of Computer Science > The University of Manchester > http://www.cs.man.ac.uk/~ssoiland/ >

