use of org.xwiki.model.reference.InvalidEntityReferenceException in project xwiki-platform by xwiki.
the class AbstractReferenceEntityReferenceResolver method normalizeReference.
/**
* Normalize the provided reference, filling missing names, and gaps in the parent chain.
*
* @param referenceToResolve the reference to normalize, if the first parameter is an entity reference, it is used
* to compute default names.
* @param parameters optional parameters,
* @return a normalized reference chain
*/
private EntityReference normalizeReference(EntityReference referenceToResolve, Object[] parameters) {
EntityReference normalizedReference = referenceToResolve;
EntityReference reference = normalizedReference;
while (reference != null) {
List<EntityType> types = EntityReferenceConstants.PARENT_TYPES.get(reference.getType());
if (reference.getParent() != null && !types.isEmpty() && !types.contains(reference.getParent().getType())) {
// The parent reference isn't the allowed parent: insert an allowed reference
EntityReference newReference = resolveDefaultReference(types.get(0), parameters).appendParent(reference.getParent());
normalizedReference = normalizedReference.replaceParent(reference.getParent(), newReference);
reference = newReference;
} else if (reference.getParent() == null && !types.isEmpty()) {
// The top reference isn't the allowed top level reference, add a parent reference
EntityReference newReference = resolveDefaultReference(types.get(0), parameters);
normalizedReference = normalizedReference.appendParent(newReference);
reference = newReference;
} else if (reference.getParent() != null && types.isEmpty()) {
// There's a parent but no one is allowed
throw new InvalidEntityReferenceException();
} else {
// Parent is ok, check next
reference = reference.getParent();
}
}
return normalizedReference;
}
use of org.xwiki.model.reference.InvalidEntityReferenceException in project xwiki-platform by xwiki.
the class DefaultReferenceEntityReferenceResolverTest method resolveDocumentReferenceWhenInvalidReference.
@Test
public void resolveDocumentReferenceWhenInvalidReference() {
try {
this.resolver.resolve(new EntityReference("page", EntityType.DOCUMENT, new EntityReference("filename", EntityType.ATTACHMENT)), EntityType.DOCUMENT);
fail("Should have thrown an exception here");
} catch (InvalidEntityReferenceException expected) {
assertEquals("Invalid reference [Document filename???page]", expected.getMessage());
}
}
Aggregations