Search in sources :

Example 16 with LinkBlock

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

the class DefaultDocumentSplitterTest method split.

@Test
public void split() throws Exception {
    SplittingCriterion splittingCriterion = mock(SplittingCriterion.class);
    when(splittingCriterion.shouldSplit(any(Block.class), anyInt())).thenReturn(true, false, false, true);
    when(splittingCriterion.shouldIterate(any(SectionBlock.class), anyInt())).thenReturn(true, false, false, false);
    NamingCriterion namingCriterion = mock(NamingCriterion.class);
    when(namingCriterion.getDocumentName(any(XDOM.class))).thenReturn("Child1", "Child2");
    // = Chapter 1 =
    // Once upon a time..
    // == Section 1.1 ==
    // In a kingdom far away..
    HeaderBlock header = new HeaderBlock(Arrays.<Block>asList(new WordBlock("Section 1.1")), HeaderLevel.LEVEL2);
    ParagraphBlock paragraph = new ParagraphBlock(Arrays.<Block>asList(new WordBlock("In a kingdom far away..")));
    SectionBlock subSection = new SectionBlock(Arrays.<Block>asList(header, paragraph));
    header = new HeaderBlock(Arrays.<Block>asList(new WordBlock("Chapter 1")), HeaderLevel.LEVEL1);
    paragraph = new ParagraphBlock(Arrays.<Block>asList(new WordBlock("Once upon a time..")));
    SectionBlock section = new SectionBlock(Arrays.<Block>asList(header, paragraph, subSection));
    XDOM xdom = new XDOM(Arrays.<Block>asList(section));
    WikiDocument document = new WikiDocument("Space.Page", xdom, null);
    List<WikiDocument> result = mocker.getComponentUnderTest().split(document, splittingCriterion, namingCriterion);
    assertEquals(3, result.size());
    assertSame(document, result.get(0));
    assertEquals("Child1", result.get(1).getFullName());
    assertSame(document, result.get(1).getParent());
    assertEquals("Child2", result.get(2).getFullName());
    assertSame(result.get(1), result.get(2).getParent());
    ClassBlockMatcher headerMatcher = new ClassBlockMatcher(HeaderBlock.class);
    assertTrue(document.getXdom().<LinkBlock>getBlocks(headerMatcher, Axes.DESCENDANT).isEmpty());
    assertEquals(1, result.get(1).getXdom().<LinkBlock>getBlocks(headerMatcher, Axes.DESCENDANT).size());
    assertEquals(1, result.get(2).getXdom().<LinkBlock>getBlocks(headerMatcher, Axes.DESCENDANT).size());
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) XDOM(org.xwiki.rendering.block.XDOM) NamingCriterion(org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) LinkBlock(org.xwiki.rendering.block.LinkBlock) WordBlock(org.xwiki.rendering.block.WordBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) Block(org.xwiki.rendering.block.Block) IdBlock(org.xwiki.rendering.block.IdBlock) WordBlock(org.xwiki.rendering.block.WordBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) WikiDocument(org.xwiki.refactoring.WikiDocument) SectionBlock(org.xwiki.rendering.block.SectionBlock) SplittingCriterion(org.xwiki.refactoring.splitter.criterion.SplittingCriterion) Test(org.junit.Test)

Example 17 with LinkBlock

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

the class DefaultLinkRefactoringTest method renameNonTerminalDocumentLinks.

@Test
public void renameNonTerminalDocumentLinks() 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 another non-terminal document.
    DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "WebHome");
    DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "WebHome");
    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.WebHome");
    // 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);
    assertEquals("X.WebHome", documentLinkBlock.getReference().getReference());
    assertEquals(ResourceType.DOCUMENT, documentLinkBlock.getReference().getType());
    assertEquals("X", spaceLinkBlock.getReference().getReference());
    assertEquals(ResourceType.SPACE, 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)

Example 18 with LinkBlock

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

the class DefaultDocumentSplitter method updateAnchors.

/**
 * @param document the document whose anchors to update
 * @param fragments see {@link #collectDocumentFragments(List)}
 */
private void updateAnchors(WikiDocument document, Map<String, String> fragments) {
    for (LinkBlock linkBlock : document.getXdom().<LinkBlock>getBlocks(new ClassBlockMatcher(LinkBlock.class), Axes.DESCENDANT)) {
        ResourceReference reference = linkBlock.getReference();
        ResourceType resoureceType = reference.getType();
        String fragment = null;
        if ((ResourceType.DOCUMENT.equals(resoureceType) || ResourceType.SPACE.equals(resoureceType)) && StringUtils.isEmpty(reference.getReference())) {
            fragment = reference.getParameter(ANCHOR_PARAMETER);
        } else if (StringUtils.startsWith(reference.getReference(), "#") && (ResourceType.PATH.equals(resoureceType) || ResourceType.URL.equals(resoureceType))) {
            fragment = reference.getReference().substring(1);
        }
        String targetDocument = fragments.get(fragment);
        if (targetDocument != null && !targetDocument.equals(document.getFullName())) {
            // The fragment has been moved so we need to update the link.
            reference.setType(ResourceType.DOCUMENT);
            reference.setReference(targetDocument);
            reference.setParameter(ANCHOR_PARAMETER, fragment);
        }
    }
}
Also used : LinkBlock(org.xwiki.rendering.block.LinkBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference)

Example 19 with LinkBlock

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

the class DefaultDocumentSplitter method createLink.

/**
 * Creates a {@link LinkBlock} suitable to be placed in the parent document.
 *
 * @param block the {@link Block} that has just been split into a separate document.
 * @param target name of the target wiki document.
 * @return a {@link LinkBlock} representing the link from the parent document to new document.
 */
private LinkBlock createLink(Block block, String target) {
    Block firstBlock = block.getChildren().get(0);
    if (firstBlock instanceof HeaderBlock) {
        DocumentResourceReference reference = new DocumentResourceReference(target);
        // Clone the header block and remove any unwanted stuff
        Block clonedHeaderBlock = firstBlock.clone(new BlockFilter() {

            @Override
            public List<Block> filter(Block block) {
                List<Block> blocks = new ArrayList<Block>();
                if (block instanceof WordBlock || block instanceof SpaceBlock || block instanceof SpecialSymbolBlock) {
                    blocks.add(block);
                }
                return blocks;
            }
        });
        return new LinkBlock(clonedHeaderBlock.getChildren(), reference, false);
    } else if (firstBlock instanceof SectionBlock) {
        return createLink(firstBlock, target);
    } else {
        throw new IllegalArgumentException("A SectionBlock should either begin with a HeaderBlock or another SectionBlock.");
    }
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) WordBlock(org.xwiki.rendering.block.WordBlock) SpaceBlock(org.xwiki.rendering.block.SpaceBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) Block(org.xwiki.rendering.block.Block) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) NewLineBlock(org.xwiki.rendering.block.NewLineBlock) IdBlock(org.xwiki.rendering.block.IdBlock) WordBlock(org.xwiki.rendering.block.WordBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock) SpaceBlock(org.xwiki.rendering.block.SpaceBlock) ArrayList(java.util.ArrayList) List(java.util.List) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) SectionBlock(org.xwiki.rendering.block.SectionBlock) BlockFilter(org.xwiki.rendering.block.BlockFilter)

Example 20 with LinkBlock

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

the class XWikiDocument method getUniqueLinkedPages.

/**
 * Extract all the unique static (i.e. not generated by macros) wiki links (pointing to wiki page) from this
 * document's content to others documents.
 *
 * @param context the XWiki context.
 * @return the document names for linked pages, if null an error append.
 * @since 1.9M2
 */
public Set<String> getUniqueLinkedPages(XWikiContext context) {
    Set<String> pageNames;
    XWikiDocument contextDoc = context.getDoc();
    String contextWiki = context.getWikiId();
    try {
        // Make sure the right document is used as context document
        context.setDoc(this);
        // Make sure the right wiki is used as context document
        context.setWikiId(getDatabase());
        if (is10Syntax()) {
            pageNames = getUniqueLinkedPages10(context);
        } else {
            XDOM dom = getXDOM();
            // TODO: Add support for macro as well.
            List<LinkBlock> linkBlocks = dom.getBlocks(new ClassBlockMatcher(LinkBlock.class), Block.Axes.DESCENDANT);
            pageNames = new LinkedHashSet<String>(linkBlocks.size());
            DocumentReference currentDocumentReference = getDocumentReference();
            for (LinkBlock linkBlock : linkBlocks) {
                ResourceReference reference = linkBlock.getReference();
                String referenceString = reference.getReference();
                ResourceType resourceType = reference.getType();
                // TODO: Add support for ATTACHMENT as well.
                if (!ResourceType.DOCUMENT.equals(resourceType) && !ResourceType.SPACE.equals(resourceType)) {
                    // We are only interested in Document or Space references.
                    continue;
                }
                // Optimisation: If the reference is empty, the link is an autolink and we don`t include it.
                if (StringUtils.isEmpty(referenceString)) {
                    continue;
                }
                // FIXME?: pass this.getDocumentReference as parameter to the resolve so that relative references
                // get resolved relative to this and not to the context document.
                EntityReference documentReference = getResourceReferenceEntityReferenceResolver().resolve(reference, EntityType.DOCUMENT);
                // Verify after resolving it that the link is not an autolink(i.e. a link to the current document)
                if (!documentReference.equals(currentDocumentReference)) {
                    // Since this method is used for saving backlinks and since backlinks must be
                    // saved with the space and page name but without the wiki part, we remove the wiki
                    // part before serializing.
                    // This is a bit of a hack since the default serializer should theoretically fail
                    // if it's passed an invalid reference.
                    pageNames.add(getCompactWikiEntityReferenceSerializer().serialize(documentReference));
                }
            }
        }
    } finally {
        context.setDoc(contextDoc);
        context.setWikiId(contextWiki);
    }
    return pageNames;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) LinkBlock(org.xwiki.rendering.block.LinkBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) EntityReference(org.xwiki.model.reference.EntityReference) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ToString(org.suigeneris.jrcs.util.ToString) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference)

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