/*
* Assert that we have the proper number of parameters.
*/
if (getParameterCount() != parameters.length)
{
throw new PrettyException("Invalid number of path parameters supplied for pattern: " + originalPattern
+ ", expected <" + getParameterCount() + ">, but got <" + parameters.length + ">");
}
/*
* Build the result URL
*/
int paramIndex = 0;
List<String> resultSegments = new ArrayList<String>();
for (Segment segment : pathSegments)
{
String template = segment.getTemplate();
Matcher parameterMatcher = Segment.getTemplateMatcher(template);
StringBuffer sb = new StringBuffer();
while (parameterMatcher.find())
{
/*
* We need to escape $ and \ because they have a special meaning when
* used in Matcher#appendReplacement(). From the docs:
*
* Note that backslashes (\) and dollar signs ($) in the replacement string
* may cause the results to be different than if it were being treated as a
* literal replacement string. Dollar signs may be treated as references to
* captured subsequences as described above, and backslashes are used to
* escape literal characters in the replacement string.
*/
String replacement = parameters[paramIndex].toString()
.replace("$", "\\$")
.replace("\\", "\\\\");
parameterMatcher.appendReplacement(sb, replacement);
paramIndex++;
}
parameterMatcher.appendTail(sb);
resultSegments.add(sb.toString());
}
result = new URL(resultSegments, urlPattern.getMetadata());
}
else if (getParameterCount() > 0)
{
throw new PrettyException("Invalid number of parameters supplied: " + originalPattern + ", expected <"
+ getParameterCount() + ">, got <0>");
}
return result;
}