if (specifiedParameters == null || specifiedParameters.size() == 0) {
if (fragmentSignature.hasParameters()) {
// Fragment signature requires parameters, but we haven't specified them!
throw new TemplateProcessingException(
"Cannot resolve fragment. Signature \"" + fragmentSignature.getStringRepresentation() + "\" " +
"declares parameters, but fragment selection did not specify any parameters.");
}
return null;
}
final boolean parametersAreSynthetic =
FragmentSelectionUtils.parameterNamesAreSynthetic(specifiedParameters.keySet());
if (parametersAreSynthetic && !fragmentSignature.hasParameters()) {
throw new TemplateProcessingException(
"Cannot resolve fragment. Signature \"" + fragmentSignature.getStringRepresentation() + "\" " +
"declares no parameters, but fragment selection did specify parameters in a synthetic manner " +
"(without names), which is not correct due to the fact parameters cannot be assigned names " +
"unless signature specifies these names.");
}
if (parametersAreSynthetic) {
// No need to match parameter names, just apply the ones from the signature
final List<String> signatureParameterNames = fragmentSignature.getParameterNames();
if (signatureParameterNames.size() != specifiedParameters.size()) {
throw new TemplateProcessingException(
"Cannot resolve fragment. Signature \"" + fragmentSignature.getStringRepresentation() + "\" " +
"declares " + signatureParameterNames.size() + " parameters, but fragment selection specifies " +
specifiedParameters.size() + " parameters. Fragment selection does not correctly match.");
}
final Map<String,Object> processedParameters = new HashMap<String, Object>(signatureParameterNames.size() + 1, 1.0f);
int index = 0;
for (final String parameterName : signatureParameterNames) {
final String syntheticParameterName =
FragmentSelectionUtils.getSyntheticParameterNameForIndex(index++);
final Object parameterValue = specifiedParameters.get(syntheticParameterName);
processedParameters.put(parameterName, parameterValue);
}
return processedParameters;
}
if (!fragmentSignature.hasParameters()) {
// Parameters in fragment selection are not synthetic, and fragment signature has no parameters,
// so we just use the "specified parameters".
return specifiedParameters;
}
// Parameters are not synthetic and signature does specify parameters, so their names should match (all
// the parameters specified at the fragment signature should be specified at the fragment selection,
// though fragment selection can specify more parameters, not present at the signature.
final List<String> parameterNames = fragmentSignature.getParameterNames();
for (final String parameterName : parameterNames) {
if (!specifiedParameters.containsKey(parameterName)) {
throw new TemplateProcessingException(
"Cannot resolve fragment. Signature \"" + fragmentSignature.getStringRepresentation() + "\" " +
"declares parameter \"" + parameterName + "\", which is not specified at the fragment " +
"selection.");
}
}