Search in sources :

Example 1 with ResourceType

use of org.xwiki.rendering.listener.reference.ResourceType in project xwiki-platform by xwiki.

the class LinkLabelGeneratorChainingListener method endLink.

/**
 * {@inheritDoc}
 *
 * @see org.xwiki.rendering.listener.chaining.AbstractChainingListener#endLink(
 *      org.xwiki.rendering.listener.reference.ResourceReference , boolean, java.util.Map)
 * @since 2.5RC1
 */
@Override
public void endLink(ResourceReference reference, boolean freestanding, Map<String, String> parameters) {
    // behaviour
    if (getEmptyBlockState().isCurrentContainerBlockEmpty()) {
        // get the link label
        // FIXME: this should be generated by the link label generator, for all cases, so that the
        // link label can be changed with whichever generator, that can handle all cases
        String linkLabel = reference.getReference();
        ResourceType resourceType = reference.getType();
        if (ResourceType.DOCUMENT.equals(resourceType) || ResourceType.SPACE.equals(resourceType)) {
            linkLabel = linkLabelGenerator.generate(reference);
        }
        // create the span around the label signaling that this is a generated label
        Map<String, String> formatParams = new HashMap<String, String>();
        formatParams.put("class", "wikigeneratedlinkcontent");
        // the same as this.format. TODO: decide which one is more semantic.
        super.beginFormat(Format.NONE, formatParams);
        // parse the linkLabel with a stream parser and an inline filter
        WrappingListener inlineFilterListener = new InlineFilterListener();
        inlineFilterListener.setWrappedListener(getListenerChain().getNextListener(getClass()));
        try {
            linkLabelParser.parse(new StringReader(linkLabel), inlineFilterListener);
        } catch (ParseException e) {
            // couldn't parse it, send it raw (interesting)
            super.onRawText(linkLabel, linkLabelParser.getSyntax());
        }
        super.endFormat(Format.NONE, formatParams);
    }
    // end the link
    super.endLink(reference, freestanding, parameters);
}
Also used : InlineFilterListener(org.xwiki.rendering.listener.InlineFilterListener) HashMap(java.util.HashMap) WrappingListener(org.xwiki.rendering.listener.WrappingListener) StringReader(java.io.StringReader) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ParseException(org.xwiki.rendering.parser.ParseException)

Example 2 with ResourceType

use of org.xwiki.rendering.listener.reference.ResourceType in project xwiki-platform by xwiki.

the class XWikiDocument method rename.

/**
 * Same as {@link #rename(DocumentReference, List, XWikiContext)} but the list of documents having the current
 * document as their parent is passed in parameter.
 *
 * @param newDocumentReference the new document reference
 * @param backlinkDocumentReferences the list of references of documents to parse and for which links will be
 *            modified to point to the new document reference
 * @param childDocumentReferences the list of references of document whose parent field will be set to the new
 *            document reference
 * @param context the ubiquitous XWiki Context
 * @throws XWikiException in case of an error
 * @since 2.2M2
 */
public void rename(DocumentReference newDocumentReference, List<DocumentReference> backlinkDocumentReferences, List<DocumentReference> childDocumentReferences, XWikiContext context) throws XWikiException {
    // If the user is trying to rename to the same name... In that case, simply exits for efficiency.
    if (isNew() || getDocumentReference().equals(newDocumentReference)) {
        return;
    }
    // Grab the xwiki object, it gets used a few times.
    XWiki xwiki = context.getWiki();
    // Step 1: Copy the document and all its translations under a new document with the new reference.
    xwiki.copyDocument(getDocumentReference(), newDocumentReference, false, context);
    // Step 2: For each child document, update its parent reference.
    if (childDocumentReferences != null) {
        for (DocumentReference childDocumentReference : childDocumentReferences) {
            XWikiDocument childDocument = xwiki.getDocument(childDocumentReference, context);
            String compactReference = getCompactEntityReferenceSerializer().serialize(newDocumentReference);
            childDocument.setParent(compactReference);
            String saveMessage = localizePlainOrKey("core.comment.renameParent", compactReference);
            childDocument.setAuthorReference(context.getUserReference());
            xwiki.saveDocument(childDocument, saveMessage, true, context);
        }
    }
    // Step 3: For each backlink to rename, parse the backlink document and replace the links with the new name.
    for (DocumentReference backlinkDocumentReference : backlinkDocumentReferences) {
        XWikiDocument backlinkRootDocument = xwiki.getDocument(backlinkDocumentReference, context);
        // Update default locale instance
        renameLinks(backlinkRootDocument, getDocumentReference(), newDocumentReference, context);
        // Update translations
        for (Locale locale : backlinkRootDocument.getTranslationLocales(context)) {
            XWikiDocument backlinkDocument = backlinkRootDocument.getTranslatedDocument(locale, context);
            renameLinks(backlinkDocument, getDocumentReference(), newDocumentReference, context);
        }
    }
    // Get new document
    XWikiDocument newDocument = xwiki.getDocument(newDocumentReference, context);
    // document's location.
    if (Utils.getContextComponentManager().hasComponent(BlockRenderer.class, getSyntax().toIdString())) {
        // Only support syntax for which a renderer is provided
        LinkedResourceHelper linkedResourceHelper = Utils.getComponent(LinkedResourceHelper.class);
        DocumentReference oldDocumentReference = getDocumentReference();
        XDOM newDocumentXDOM = newDocument.getXDOM();
        List<Block> blocks = linkedResourceHelper.getBlocks(newDocumentXDOM);
        // FIXME: Duplicate code. See org.xwiki.refactoring.internal.DefaultLinkRefactoring#updateRelativeLinks in
        // xwiki-platform-refactoring-default
        boolean modified = false;
        for (Block block : blocks) {
            ResourceReference resourceReference = linkedResourceHelper.getResourceReference(block);
            if (resourceReference == null) {
                // Skip invalid blocks.
                continue;
            }
            ResourceType resourceType = resourceReference.getType();
            // TODO: support ATTACHMENT as well.
            if (!ResourceType.DOCUMENT.equals(resourceType) && !ResourceType.SPACE.equals(resourceType)) {
                // We are currently only interested in Document or Space references.
                continue;
            }
            // current link, use the old document's reference to fill in blanks.
            EntityReference oldLinkReference = getResourceReferenceEntityReferenceResolver().resolve(resourceReference, null, oldDocumentReference);
            // new link, use the new document's reference to fill in blanks.
            EntityReference newLinkReference = getResourceReferenceEntityReferenceResolver().resolve(resourceReference, null, newDocumentReference);
            // If the new and old link references don`t match, then we must update the relative link.
            if (!newLinkReference.equals(oldLinkReference)) {
                modified = true;
                // Serialize the old (original) link relative to the new document's location, in compact form.
                String serializedLinkReference = getCompactWikiEntityReferenceSerializer().serialize(oldLinkReference, newDocumentReference);
                // Update the reference in the XDOM.
                linkedResourceHelper.setResourceReferenceString(block, serializedLinkReference);
            }
        }
        // Set the new content and save document if needed
        if (modified) {
            newDocument.setContent(newDocumentXDOM);
            newDocument.setAuthorReference(context.getUserReference());
            xwiki.saveDocument(newDocument, context);
        }
    }
    // Step 5: Delete the old document
    xwiki.deleteDocument(this, context);
    // Step 6: The current document needs to point to the renamed document as otherwise it's pointing to an
    // invalid XWikiDocument object as it's been deleted...
    clone(newDocument);
}
Also used : Locale(java.util.Locale) XDOM(org.xwiki.rendering.block.XDOM) XWiki(com.xpn.xwiki.XWiki) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ToString(org.suigeneris.jrcs.util.ToString) LinkedResourceHelper(com.xpn.xwiki.internal.render.LinkedResourceHelper) EntityReference(org.xwiki.model.reference.EntityReference) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) MacroBlock(org.xwiki.rendering.block.MacroBlock) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference)

Example 3 with ResourceType

use of org.xwiki.rendering.listener.reference.ResourceType in project xwiki-platform by xwiki.

the class DefaultLinkedResourceHelper method getResourceType.

@Override
public ResourceType getResourceType(Block block) {
    // Determine the reference string and reference type for each block type.
    ResourceType resourceType = null;
    if (block instanceof LinkBlock) {
        LinkBlock linkBlock = (LinkBlock) block;
        ResourceReference linkReference = linkBlock.getReference();
        resourceType = linkReference.getType();
    } else if (block instanceof MacroBlock) {
        // We still have to look at the reference string to see if it is a valid include (i.e. non-recursive).
        String referenceString = block.getParameter(REFERENCE_MACRO_PARAMETER);
        if (StringUtils.isBlank(referenceString)) {
            referenceString = block.getParameter(DOCUMENT_MACRO_PARAMETER);
        }
        if (StringUtils.isBlank(referenceString)) {
            // Skip it.
            return null;
        }
        // FIXME: this may be SPACE once we start hiding "WebHome" from macro reference parameters.
        resourceType = ResourceType.DOCUMENT;
    }
    return resourceType;
}
Also used : LinkBlock(org.xwiki.rendering.block.LinkBlock) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 4 with ResourceType

use of org.xwiki.rendering.listener.reference.ResourceType in project xwiki-platform by xwiki.

the class DefaultOldRendering method refactorDocumentLinks.

/**
 * @since 2.2M1
 */
private void refactorDocumentLinks(XWikiDocument document, DocumentReference oldDocumentReference, DocumentReference newDocumentReference, XWikiContext context) throws XWikiException {
    // FIXME: Duplicate code. See org.xwiki.refactoring.internal.DefaultLinkRefactoring#renameLinks in
    // xwiki-platform-refactoring-default
    DocumentReference currentDocumentReference = document.getDocumentReference();
    XDOM xdom = document.getXDOM();
    List<Block> blocks = linkedResourceHelper.getBlocks(xdom);
    for (Block block : blocks) {
        ResourceReference resourceReference = linkedResourceHelper.getResourceReference(block);
        if (resourceReference == null) {
            // Skip invalid blocks.
            continue;
        }
        ResourceType resourceType = resourceReference.getType();
        // TODO: support ATTACHMENT as well.
        if (!ResourceType.DOCUMENT.equals(resourceType) && !ResourceType.SPACE.equals(resourceType)) {
            // We are currently only interested in Document or Space references.
            continue;
        }
        // Resolve the resource reference.
        EntityReference linkEntityReference = resourceReferenceResolver.resolve(resourceReference, null, currentDocumentReference);
        // Resolve the document of the reference.
        DocumentReference linkTargetDocumentReference = defaultReferenceDocumentReferenceResolver.resolve(linkEntityReference);
        EntityReference newTargetReference = newDocumentReference;
        ResourceType newResourceType = resourceType;
        // If the link was resolved to a space...
        if (EntityType.SPACE.equals(linkEntityReference.getType())) {
            if (XWiki.DEFAULT_SPACE_HOMEPAGE.equals(newDocumentReference.getName())) {
                // If the new document reference is also a space (non-terminal doc), be careful to keep it
                // serialized as a space still (i.e. without ".WebHome") and not serialize it as a doc by mistake
                // (i.e. with ".WebHome").
                newTargetReference = newDocumentReference.getLastSpaceReference();
            } else {
                // If the new target is a non-terminal document, we can not use a "space:" resource type to access
                // it anymore. To fix it, we need to change the resource type of the link reference "doc:".
                newResourceType = ResourceType.DOCUMENT;
            }
        }
        // If the link targets the old (renamed) document reference, we must update it.
        if (linkTargetDocumentReference.equals(oldDocumentReference)) {
            String newReferenceString = this.compactEntityReferenceSerializer.serialize(newTargetReference, currentDocumentReference);
            // Update the reference in the XDOM.
            linkedResourceHelper.setResourceReferenceString(block, newReferenceString);
            linkedResourceHelper.setResourceType(block, newResourceType);
        }
    }
    document.setContent(xdom);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) EntityReference(org.xwiki.model.reference.EntityReference) Block(org.xwiki.rendering.block.Block) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 5 with ResourceType

use of org.xwiki.rendering.listener.reference.ResourceType in project xwiki-platform by xwiki.

the class DefaultLinkRefactoring method renameLink.

private boolean renameLink(Block block, DocumentReference currentDocumentReference, DocumentReference oldTarget, DocumentReference newTarget) throws IllegalArgumentException {
    boolean modified = false;
    ResourceReference resourceReference = linkedResourceHelper.getResourceReference(block);
    if (resourceReference == null) {
        // Skip invalid blocks.
        throw new IllegalArgumentException();
    }
    ResourceType resourceType = resourceReference.getType();
    // TODO: support ATTACHMENT as well.
    if (!ResourceType.DOCUMENT.equals(resourceType) && !ResourceType.SPACE.equals(resourceType)) {
        // We are currently only interested in Document or Space references.
        throw new IllegalArgumentException();
    }
    // Resolve the resource reference.
    EntityReference linkEntityReference = resourceReferenceResolver.resolve(resourceReference, null, currentDocumentReference);
    // Resolve the document of the reference.
    DocumentReference linkTargetDocumentReference = defaultReferenceDocumentReferenceResolver.resolve(linkEntityReference);
    EntityReference newTargetReference = newTarget;
    ResourceType newResourceType = resourceType;
    // If the link was resolved to a space...
    if (EntityType.SPACE.equals(linkEntityReference.getType())) {
        if (XWiki.DEFAULT_SPACE_HOMEPAGE.equals(newTarget.getName())) {
            // If the new document reference is also a space (non-terminal doc), be careful to keep it
            // serialized as a space still (i.e. without ".WebHome") and not serialize it as a doc by mistake
            // (i.e. with ".WebHome").
            newTargetReference = newTarget.getLastSpaceReference();
        } else {
            // If the new target is a non-terminal document, we can not use a "space:" resource type to access
            // it anymore. To fix it, we need to change the resource type of the link reference "doc:".
            newResourceType = ResourceType.DOCUMENT;
        }
    }
    // If the link targets the old (renamed) document reference, we must update it.
    if (linkTargetDocumentReference.equals(oldTarget)) {
        modified = true;
        String newReferenceString = this.compactEntityReferenceSerializer.serialize(newTargetReference, currentDocumentReference);
        // Update the reference in the XDOM.
        linkedResourceHelper.setResourceReferenceString(block, newReferenceString);
        linkedResourceHelper.setResourceType(block, newResourceType);
    }
    return modified;
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

ResourceType (org.xwiki.rendering.listener.reference.ResourceType)8 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 DocumentReference (org.xwiki.model.reference.DocumentReference)5 EntityReference (org.xwiki.model.reference.EntityReference)5 LinkBlock (org.xwiki.rendering.block.LinkBlock)4 XDOM (org.xwiki.rendering.block.XDOM)4 Block (org.xwiki.rendering.block.Block)3 ToString (org.suigeneris.jrcs.util.ToString)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)2 MacroBlock (org.xwiki.rendering.block.MacroBlock)2 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)2 XWiki (com.xpn.xwiki.XWiki)1 LinkedResourceHelper (com.xpn.xwiki.internal.render.LinkedResourceHelper)1 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)1 SectionBlock (org.xwiki.rendering.block.SectionBlock)1 InlineFilterListener (org.xwiki.rendering.listener.InlineFilterListener)1