Search in sources :

Example 1 with InvalidEntityReferenceException

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;
}
Also used : EntityType(org.xwiki.model.EntityType) EntityReference(org.xwiki.model.reference.EntityReference) InvalidEntityReferenceException(org.xwiki.model.reference.InvalidEntityReferenceException)

Example 2 with InvalidEntityReferenceException

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());
    }
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) InvalidEntityReferenceException(org.xwiki.model.reference.InvalidEntityReferenceException) Test(org.junit.Test)

Aggregations

EntityReference (org.xwiki.model.reference.EntityReference)2 InvalidEntityReferenceException (org.xwiki.model.reference.InvalidEntityReferenceException)2 Test (org.junit.Test)1 EntityType (org.xwiki.model.EntityType)1