public PathMetaData build(final String path, final boolean rootPath) {
StringBuffer uriTemplate = null;
PathMetaData pathMetaData = new PathMetaData();
pathMetaData.setRootPath(rootPath);
if(path == null || path.isEmpty() || path.equals(ROOT_PATH)) {
uriTemplate = new StringBuffer();
uriTemplate.append(TAIL);
pathMetaData.setEmpty(true);
} else {
List<PathVariableMetaData> pathVariableMetaData = extractVariables(path);
List<String> pathVariableNames = populateVariableNames(pathVariableMetaData);
uriTemplate = new StringBuffer();
uriTemplate.append(TEMPLATE_START);
uriTemplate.append(TEMPLATE_START);
uriTemplate.append(path);
uriTemplate.append(TEMPLATE_END);
for (String pathVariableName : pathVariableNames) {
String variableMatcher = VARIABLE_START + pathVariableName + VARIABLE_END;
int startIndex = uriTemplate.indexOf(variableMatcher);
int lastIndex = startIndex + variableMatcher.length();
uriTemplate.replace(startIndex, lastIndex, VARIABLE);
}
uriTemplate.append(TAIL);
uriTemplate.append(TEMPLATE_END);
pathMetaData.setPathVariables(pathVariableMetaData);
}
pathMetaData.setTemplate(uriTemplate.toString());
return pathMetaData;
}