Search in sources :

Example 1 with LinkBlock

use of org.xwiki.rendering.block.LinkBlock in project xwiki-platform by xwiki.

the class DefaultLinkedResourceHelper method setResourceReferenceString.

@Override
public void setResourceReferenceString(Block block, String newReferenceString) {
    if (block instanceof LinkBlock) {
        LinkBlock linkBlock = (LinkBlock) block;
        ResourceReference linkReference = linkBlock.getReference();
        linkReference.setReference(newReferenceString);
    } else if (block instanceof MacroBlock) {
        if (StringUtils.isNotBlank(block.getParameter(DOCUMENT_MACRO_PARAMETER))) {
            // Backwards compatibility check.
            block.setParameter(DOCUMENT_MACRO_PARAMETER, newReferenceString);
        } else {
            block.setParameter(REFERENCE_MACRO_PARAMETER, newReferenceString);
        }
    }
}
Also used : LinkBlock(org.xwiki.rendering.block.LinkBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 2 with LinkBlock

use of org.xwiki.rendering.block.LinkBlock in project xwiki-platform by xwiki.

the class DefaultGadgetSource method getDashboardSourceMetadata.

@Override
public List<Block> getDashboardSourceMetadata(String source, MacroTransformationContext context) {
    DocumentReference sourceDoc = getSourceDocumentReference(source);
    String classParameterName = "class";
    GroupBlock metadataContainer = new GroupBlock();
    metadataContainer.setParameter(classParameterName, DashboardMacro.METADATA);
    // generate anchors for the urls
    XWikiContext xContext = getXWikiContext();
    String editURL = xContext.getWiki().getURL(sourceDoc, "save", "", "", xContext);
    LinkBlock editURLBlock = new LinkBlock(Collections.<Block>emptyList(), new ResourceReference(editURL, ResourceType.URL), false);
    editURLBlock.setParameter(classParameterName, DashboardMacro.EDIT_URL);
    metadataContainer.addChild(editURLBlock);
    String removeURL = xContext.getWiki().getURL(sourceDoc, "objectremove", "", "", xContext);
    LinkBlock removeURLBlock = new LinkBlock(Collections.<Block>emptyList(), new ResourceReference(removeURL, ResourceType.URL), false);
    removeURLBlock.setParameter(classParameterName, DashboardMacro.REMOVE_URL);
    metadataContainer.addChild(removeURLBlock);
    String addURL = xContext.getWiki().getURL(sourceDoc, "objectadd", "", "", xContext);
    LinkBlock addURLBlock = new LinkBlock(Collections.<Block>emptyList(), new ResourceReference(addURL, ResourceType.URL), false);
    addURLBlock.setParameter(classParameterName, DashboardMacro.ADD_URL);
    metadataContainer.addChild(addURLBlock);
    // and create divs for the source metadata
    GroupBlock sourcePageBlock = new GroupBlock();
    sourcePageBlock.addChild(new WordBlock(sourceDoc.getName()));
    sourcePageBlock.setParameter(classParameterName, DashboardMacro.SOURCE_PAGE);
    metadataContainer.addChild(sourcePageBlock);
    GroupBlock sourceSpaceBlock = new GroupBlock();
    // Extract the full Space Reference (in order to support Nested Spaces) and set it in the XDOM
    sourceSpaceBlock.addChild(new WordBlock(this.localReferenceSerializer.serialize(sourceDoc.getLastSpaceReference())));
    sourceSpaceBlock.setParameter(classParameterName, DashboardMacro.SOURCE_SPACE);
    metadataContainer.addChild(sourceSpaceBlock);
    GroupBlock sourceWikiBlock = new GroupBlock();
    sourceWikiBlock.addChild(new WordBlock(sourceDoc.getWikiReference().getName()));
    sourceWikiBlock.setParameter(classParameterName, DashboardMacro.SOURCE_WIKI);
    metadataContainer.addChild(sourceWikiBlock);
    String sourceURL = xContext.getWiki().getURL(sourceDoc, "view", "", "", xContext);
    LinkBlock sourceURLBlock = new LinkBlock(Collections.<Block>emptyList(), new ResourceReference(sourceURL, ResourceType.URL), false);
    sourceURLBlock.setParameter(classParameterName, DashboardMacro.SOURCE_URL);
    metadataContainer.addChild(sourceURLBlock);
    return Collections.<Block>singletonList(metadataContainer);
}
Also used : LinkBlock(org.xwiki.rendering.block.LinkBlock) WordBlock(org.xwiki.rendering.block.WordBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) XWikiContext(com.xpn.xwiki.XWikiContext) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) WordBlock(org.xwiki.rendering.block.WordBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 3 with LinkBlock

use of org.xwiki.rendering.block.LinkBlock in project xwiki-platform by xwiki.

the class DefaultLinkRefactoringTest method updateRelativeLinksAcrossWikis.

@Test
public void updateRelativeLinksAcrossWikis() throws Exception {
    DocumentReference oldReference = new DocumentReference("wiki1", "A", "B");
    DocumentReference newReference = new DocumentReference("wiki2", "X", "Y");
    XWikiDocument newDocument = mock(XWikiDocument.class);
    when(this.xcontext.getWiki().getDocument(newReference, this.xcontext)).thenReturn(newDocument);
    when(newDocument.getDocumentReference()).thenReturn(newReference);
    when(newDocument.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
    this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
    XDOM xdom = mock(XDOM.class);
    when(newDocument.getXDOM()).thenReturn(xdom);
    ResourceReference docLinkReference = new ResourceReference("C", ResourceType.DOCUMENT);
    LinkBlock docLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false);
    ResourceReference spaceLinkReference = new ResourceReference("Z", ResourceType.SPACE);
    LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false);
    when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(docLinkBlock, spaceLinkBlock));
    DocumentReference originalDocLinkReference = new DocumentReference("C", oldReference.getLastSpaceReference());
    when(this.resourceReferenceResolver.resolve(docLinkReference, null, oldReference)).thenReturn(originalDocLinkReference);
    DocumentReference newDocLinkReference = new DocumentReference("C", newReference.getLastSpaceReference());
    when(this.resourceReferenceResolver.resolve(docLinkReference, null, newReference)).thenReturn(newDocLinkReference);
    SpaceReference originalSpaceReference = new SpaceReference("wiki1", "Z");
    when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, oldReference)).thenReturn(originalSpaceReference);
    SpaceReference newSpaceReference = new SpaceReference("wiki2", "Z");
    when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, newReference)).thenReturn(newSpaceReference);
    when(this.compactEntityReferenceSerializer.serialize(originalDocLinkReference, newReference)).thenReturn("wiki1:A.C");
    when(this.compactEntityReferenceSerializer.serialize(originalSpaceReference, newReference)).thenReturn("wiki1:Z");
    this.mocker.getComponentUnderTest().updateRelativeLinks(oldReference, newReference);
    // Document link block is updated.
    assertEquals("wiki1:A.C", docLinkBlock.getReference().getReference());
    assertEquals(ResourceType.DOCUMENT, docLinkBlock.getReference().getType());
    // Space link is also updated, since they were referring entities on a different wiki.
    assertEquals("wiki1:Z", spaceLinkBlock.getReference().getReference());
    assertEquals(ResourceType.SPACE, spaceLinkBlock.getReference().getType());
    verifyDocumentSave(newDocument, "Updated the relative links.", true);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XDOM(org.xwiki.rendering.block.XDOM) LinkBlock(org.xwiki.rendering.block.LinkBlock) SpaceReference(org.xwiki.model.reference.SpaceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 4 with LinkBlock

use of org.xwiki.rendering.block.LinkBlock in project xwiki-platform by xwiki.

the class DefaultLinkRefactoringTest method updateRelativeLinks.

@Test
public void updateRelativeLinks() throws Exception {
    DocumentReference oldReference = new DocumentReference("wiki", "A", "B");
    DocumentReference newReference = new DocumentReference("wiki", "X", "Y");
    XWikiDocument newDocument = mock(XWikiDocument.class);
    when(this.xcontext.getWiki().getDocument(newReference, this.xcontext)).thenReturn(newDocument);
    when(newDocument.getDocumentReference()).thenReturn(newReference);
    when(newDocument.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
    this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
    XDOM xdom = mock(XDOM.class);
    when(newDocument.getXDOM()).thenReturn(xdom);
    ResourceReference docLinkReference = new ResourceReference("C", ResourceType.DOCUMENT);
    LinkBlock docLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false);
    ResourceReference spaceLinkReference = new ResourceReference("Z", ResourceType.SPACE);
    LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false);
    when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(docLinkBlock, spaceLinkBlock));
    DocumentReference originalDocLinkReference = new DocumentReference("C", oldReference.getLastSpaceReference());
    when(this.resourceReferenceResolver.resolve(docLinkReference, null, oldReference)).thenReturn(originalDocLinkReference);
    DocumentReference newDocLinkReference = new DocumentReference("C", newReference.getLastSpaceReference());
    when(this.resourceReferenceResolver.resolve(docLinkReference, null, newReference)).thenReturn(newDocLinkReference);
    SpaceReference originalSpaceReference = new SpaceReference("wiki", "Z");
    when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, oldReference)).thenReturn(originalSpaceReference);
    when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, newReference)).thenReturn(originalSpaceReference);
    when(this.compactEntityReferenceSerializer.serialize(originalDocLinkReference, newReference)).thenReturn("A.C");
    this.mocker.getComponentUnderTest().updateRelativeLinks(oldReference, newReference);
    // Document link block is updated.
    assertEquals("A.C", docLinkBlock.getReference().getReference());
    assertEquals(ResourceType.DOCUMENT, docLinkBlock.getReference().getType());
    // Space link block stays the same, since they were on the same wiki.
    assertEquals("Z", spaceLinkBlock.getReference().getReference());
    assertEquals(ResourceType.SPACE, spaceLinkBlock.getReference().getType());
    verifyDocumentSave(newDocument, "Updated the relative links.", true);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XDOM(org.xwiki.rendering.block.XDOM) LinkBlock(org.xwiki.rendering.block.LinkBlock) SpaceReference(org.xwiki.model.reference.SpaceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 5 with LinkBlock

use of org.xwiki.rendering.block.LinkBlock in project xwiki-platform by xwiki.

the class DefaultLinkRefactoringTest method renameNonTerminalToTerminalDocumentLinks.

@Test
public void renameNonTerminalToTerminalDocumentLinks() throws Exception {
    DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
    XWikiDocument document = mock(XWikiDocument.class);
    when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document);
    when(document.getDocumentReference()).thenReturn(documentReference);
    when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
    this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
    // From a non-terminal document to a terminal document.
    DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "WebHome");
    DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y");
    XDOM xdom = mock(XDOM.class);
    when(document.getXDOM()).thenReturn(xdom);
    ResourceReference docLinkReference = new ResourceReference("A.WebHome", ResourceType.DOCUMENT);
    LinkBlock documentLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false);
    ResourceReference spaceLinkReference = new ResourceReference("A", ResourceType.SPACE);
    LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false);
    when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(documentLinkBlock, spaceLinkBlock));
    // Doc link
    when(this.resourceReferenceResolver.resolve(docLinkReference, null, documentReference)).thenReturn(oldLinkTarget);
    when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget);
    when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y");
    // Space link
    SpaceReference spaceReference = oldLinkTarget.getLastSpaceReference();
    when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, documentReference)).thenReturn(spaceReference);
    when(this.defaultReferenceDocumentReferenceResolver.resolve(spaceReference)).thenReturn(oldLinkTarget);
    when(this.compactEntityReferenceSerializer.serialize(newLinkTarget.getLastSpaceReference(), documentReference)).thenReturn("X");
    this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget);
    // Note that both resulting renamed back-links are of type document. (i.e. the space link was converted to a doc
    // link)
    assertEquals("X.Y", documentLinkBlock.getReference().getReference());
    assertEquals(ResourceType.DOCUMENT, documentLinkBlock.getReference().getType());
    assertEquals("X.Y", spaceLinkBlock.getReference().getReference());
    assertEquals(ResourceType.DOCUMENT, spaceLinkBlock.getReference().getType());
    verifyDocumentSave(document, "Renamed back-links.", false);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XDOM(org.xwiki.rendering.block.XDOM) LinkBlock(org.xwiki.rendering.block.LinkBlock) SpaceReference(org.xwiki.model.reference.SpaceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

LinkBlock (org.xwiki.rendering.block.LinkBlock)20 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)18 XDOM (org.xwiki.rendering.block.XDOM)10 Test (org.junit.Test)9 DocumentReference (org.xwiki.model.reference.DocumentReference)9 Block (org.xwiki.rendering.block.Block)8 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)6 MacroBlock (org.xwiki.rendering.block.MacroBlock)6 ParagraphBlock (org.xwiki.rendering.block.ParagraphBlock)6 SpaceReference (org.xwiki.model.reference.SpaceReference)4 WordBlock (org.xwiki.rendering.block.WordBlock)4 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)4 GroupBlock (org.xwiki.rendering.block.GroupBlock)3 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)3 IdBlock (org.xwiki.rendering.block.IdBlock)3 ImageBlock (org.xwiki.rendering.block.ImageBlock)3 SectionBlock (org.xwiki.rendering.block.SectionBlock)3 ResourceType (org.xwiki.rendering.listener.reference.ResourceType)3 WikiDocument (org.xwiki.refactoring.WikiDocument)2 SplittingCriterion (org.xwiki.refactoring.splitter.criterion.SplittingCriterion)2