public String build(final UrlMapping urlMapping, final boolean encodeUrl, final List<UIParameter> parameters)
{
String result = "";
if (urlMapping != null)
{
URLPatternParser parser = new URLPatternParser(urlMapping.getPattern());
List<String> pathParams = new ArrayList<String>();
List<QueryParameter> queryParams = new ArrayList<QueryParameter>();
if(parameters != null)
{
// TODO this logic should be in the components, not in the builder
if (parameters.size() == 1)
{
UIParameter firstParam = parameters.get(0);
if (((firstParam.getValue() != null)) && (firstParam.getName() == null))
{
if (firstParam.getValue() instanceof List<?>)
{
URL url = parser.getMappedURL(firstParam.getValue());
return url.toURL();
}
else if (firstParam.getValue().getClass().isArray())
{
// The Object[] cast here is required, otherwise Java treats
// getValue() as a single Object.
List<Object> list = Arrays.asList((Object[]) firstParam.getValue());
URL url = parser.getMappedURL(list);
return url.toURL();
}
}
}
for (UIParameter parameter : parameters)
{
String name = parameter.getName();
Object value = parameter.getValue();
if ((name == null) && (value != null))
{
pathParams.add(value.toString());
}
else
{
List<?> values = null;
if ((value != null) && value.getClass().isArray())
{
values = Arrays.asList((Object[]) value);
}
else if (value instanceof List<?>)
{
values = (List<?>) value;
}
else if (value != null)
{
values = Arrays.asList(value);
}
if (values != null)
{
for (Object object : values)
{
String tempValue = null;
if (object != null)
{
tempValue = object.toString();
}
queryParams.add(new QueryParameter(name, tempValue));
}
}
else
{
queryParams.add(new QueryParameter(name, null));
}
}
}
}
// build URL object for given path parameter set
URL mappedURL = parser.getMappedURL(pathParams.toArray());
// create encoded/unicode URL
String url = encodeUrl ? mappedURL.encode().toURL() : mappedURL.toURL();
// append query string