Search in sources :

Example 6 with EntityType

use of org.xwiki.model.EntityType in project xwiki-platform by xwiki.

the class DefaultSymbolScheme method initialize.

/**
 * Initialize internal data structures.
 */
public void initialize() {
    // Dynamically create the escape/replacement maps.
    // The characters to escape are all the characters that are separators between the current type and its parent
    // type + the escape symbol itself.
    this.escapes = new HashMap<>();
    this.replacements = new HashMap<>();
    String escape = Character.toString(getEscapeSymbol());
    for (Map.Entry<EntityType, Map<EntityType, Character>> entry : SEPARATORS.entrySet()) {
        EntityType type = entry.getKey();
        Map<EntityType, Character> separators = entry.getValue();
        List<String> charactersToEscape = new ArrayList<>();
        List<String> replacementCharacters = new ArrayList<>();
        for (Character characterToEscape : separators.values()) {
            charactersToEscape.add(Character.toString(characterToEscape));
            replacementCharacters.add(escape + Character.toString(characterToEscape));
        }
        charactersToEscape.add(escape);
        replacementCharacters.add(escape + escape);
        String[] escapesArray = new String[charactersToEscape.size()];
        this.escapes.put(type, charactersToEscape.toArray(escapesArray));
        String[] replacementsArray = new String[replacementCharacters.size()];
        this.replacements.put(type, replacementCharacters.toArray(replacementsArray));
    }
}
Also used : EntityType(org.xwiki.model.EntityType) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with EntityType

use of org.xwiki.model.EntityType in project xwiki-platform by xwiki.

the class SolrEntityReferenceResolver method getEntityReference.

private EntityReference getEntityReference(SolrDocument solrDocument, EntityType expectedEntityType, Object... parameters) {
    EntityReference wikiReference = getWikiReference(solrDocument, parameters);
    EntityReference spaceReference = getSpaceReference(solrDocument, wikiReference, parameters);
    EntityReference documentReference = getDocumentReferenceWithLocale(solrDocument, spaceReference, parameters);
    String indexedEntityType = getFieldStringValue(solrDocument, FieldUtils.TYPE);
    EntityType actualEntityType = StringUtils.isEmpty(indexedEntityType) ? expectedEntityType : EntityType.valueOf(indexedEntityType);
    switch(actualEntityType) {
        case ATTACHMENT:
            return getAttachmentReference(solrDocument, documentReference, parameters);
        case OBJECT:
            return getObjectReference(solrDocument, documentReference, parameters);
        case OBJECT_PROPERTY:
            EntityReference objectReference = getObjectReference(solrDocument, documentReference, parameters);
            return getObjectPropertyReference(solrDocument, objectReference, parameters);
        default:
            return documentReference;
    }
}
Also used : EntityType(org.xwiki.model.EntityType) EntityReference(org.xwiki.model.reference.EntityReference)

Example 8 with EntityType

use of org.xwiki.model.EntityType 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 9 with EntityType

use of org.xwiki.model.EntityType in project xwiki-platform by xwiki.

the class AbstractStringEntityReferenceResolver method resolve.

@Override
public EntityReference resolve(String entityReferenceRepresentation, EntityType type, Object... parameters) {
    Map<Character, EntityType> typeSetup = getTypeSetup(type);
    // Check if the type require anything specific
    if (typeSetup == null) {
        return getEscapedReference(entityReferenceRepresentation, type, parameters);
    }
    // Handle the case when the passed representation is null. In this case we consider it similar to passing
    // an empty string.
    StringBuilder representation;
    if (entityReferenceRepresentation == null) {
        representation = new StringBuilder();
    } else {
        representation = new StringBuilder(entityReferenceRepresentation);
    }
    EntityReference reference = null;
    EntityType currentType = type;
    while (typeSetup != null && !typeSetup.isEmpty()) {
        // Search all characters for a non escaped separator. If found, then consider the part after the
        // character as the reference name and continue parsing the part before the separator.
        EntityType parentType = null;
        int i = representation.length();
        while (--i >= 0) {
            char currentChar = representation.charAt(i);
            int nextIndex = i - 1;
            char nextChar = 0;
            if (nextIndex >= 0) {
                nextChar = representation.charAt(nextIndex);
            }
            if (typeSetup.containsKey(currentChar)) {
                int numberOfEscapeChars = getNumberOfCharsBefore(getSymbolScheme().getEscapeSymbol(), representation, nextIndex);
                if (numberOfEscapeChars % 2 == 0) {
                    parentType = typeSetup.get(currentChar);
                    break;
                } else {
                    // Unescape the character
                    representation.delete(nextIndex, i);
                    --i;
                }
            } else if (nextChar == getSymbolScheme().getEscapeSymbol()) {
                // Unescape the character
                representation.delete(nextIndex, i);
                --i;
            }
        }
        reference = appendNewReference(reference, getNewReference(i, representation, currentType, parameters));
        if (parentType != null) {
            currentType = parentType;
        } else {
            currentType = typeSetup.values().iterator().next();
        }
        typeSetup = getTypeSetup(currentType);
    }
    // Handle last entity reference's name
    reference = appendNewReference(reference, getEscapedReference(representation, currentType, parameters));
    return reference;
}
Also used : EntityType(org.xwiki.model.EntityType) EntityReference(org.xwiki.model.reference.EntityReference)

Example 10 with EntityType

use of org.xwiki.model.EntityType in project xwiki-platform by xwiki.

the class SolrFieldStringEntityReferenceResolver method resolve.

@Override
public EntityReference resolve(String entityReferenceRepresentation, EntityType type, Object... parameters) {
    EntityType[] entityTypesForType = ENTITY_TYPES.get(type);
    if (entityTypesForType == null) {
        throw new RuntimeException("No parsing definition found for Entity Type [" + type + "]");
    }
    EntityReference entityReference = null;
    int entityTypeOffset = 0;
    int offset = entityReferenceRepresentation.length() - 1;
    while (offset >= 0) {
        // Small hack in order to be able to resolve nested spaces. Basically we never read the WIKI component from
        // the passed entity reference representation (we assume it is a space). We fill the WIKI entity reference
        // after this while loop.
        entityTypeOffset = Math.min(entityTypeOffset, entityTypesForType.length - 2);
        StringBuilder entityName = new StringBuilder();
        offset = readEntityName(entityReferenceRepresentation, entityName, offset);
        EntityReference parent = getNewEntityReference(entityName.reverse().toString(), entityTypesForType[entityTypeOffset++], parameters);
        entityReference = entityReference == null ? parent : entityReference.appendParent(parent);
    }
    // Handle the case when the passed string representation is relative. Use the provided parameters in this case.
    for (int i = entityTypeOffset; i < entityTypesForType.length; i++) {
        EntityReference parent = resolveDefaultReference(entityTypesForType[i], parameters);
        if (parent != null) {
            entityReference = entityReference == null ? parent : entityReference.appendParent(parent);
        } else {
            // We cannot skip reference components.
            break;
        }
    }
    return entityReference;
}
Also used : EntityType(org.xwiki.model.EntityType) EntityReference(org.xwiki.model.reference.EntityReference)

Aggregations

EntityType (org.xwiki.model.EntityType)16 EntityReference (org.xwiki.model.reference.EntityReference)11 Namespace (org.xwiki.component.namespace.Namespace)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SolrDocument (org.apache.solr.common.SolrDocument)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 InvalidEntityReferenceException (org.xwiki.model.reference.InvalidEntityReferenceException)1 CreateResourceReferenceException (org.xwiki.resource.CreateResourceReferenceException)1 EntityResourceReference (org.xwiki.resource.entity.EntityResourceReference)1 SolrIndexerException (org.xwiki.search.solr.internal.api.SolrIndexerException)1 Right (org.xwiki.security.authorization.Right)1 DefaultTestAccessRule (org.xwiki.security.authorization.testwikis.internal.entities.DefaultTestAccessRule)1