Search in sources :

Example 6 with LinkBlock

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

the class DefaultLinkRefactoringTest method renameLinks.

@Test
public void renameLinks() 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 terminal document to another terminal document.
    DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "B");
    DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y");
    XDOM xdom = mock(XDOM.class);
    when(document.getXDOM()).thenReturn(xdom);
    ResourceReference linkReference = new ResourceReference("A.B", ResourceType.DOCUMENT);
    LinkBlock linkBlock = new LinkBlock(Collections.<Block>emptyList(), linkReference, false);
    when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(linkBlock));
    when(this.resourceReferenceResolver.resolve(linkReference, null, documentReference)).thenReturn(oldLinkTarget);
    when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget);
    when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y");
    this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget);
    assertEquals("X.Y", linkBlock.getReference().getReference());
    assertEquals(ResourceType.DOCUMENT, linkBlock.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) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 7 with LinkBlock

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

the class DefaultLinkRefactoringTest method renameLinksFromLinksAndMacros.

@Test
public void renameLinksFromLinksAndMacros() 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 terminal document to another terminal document.
    DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "B");
    DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y");
    XDOM xdom = mock(XDOM.class);
    when(document.getXDOM()).thenReturn(xdom);
    Map<String, String> includeParameters = new HashMap<String, String>();
    includeParameters.put("reference", "A.B");
    MacroBlock includeMacroBlock = new MacroBlock("include", includeParameters, false);
    ResourceReference resourceReference = new ResourceReference("A.B", ResourceType.DOCUMENT);
    LinkBlock documentLinkBlock = new LinkBlock(Collections.<Block>emptyList(), resourceReference, false);
    when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(includeMacroBlock, documentLinkBlock));
    when(this.resourceReferenceResolver.resolve(resourceReference, null, documentReference)).thenReturn(oldLinkTarget);
    when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget);
    when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y");
    this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget);
    assertEquals("X.Y", includeMacroBlock.getParameter("reference"));
    assertEquals("X.Y", documentLinkBlock.getReference().getReference());
    assertEquals(ResourceType.DOCUMENT, documentLinkBlock.getReference().getType());
    verifyDocumentSave(document, "Renamed back-links.", false);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) LinkBlock(org.xwiki.rendering.block.LinkBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 8 with LinkBlock

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

the class RssMacro method generateEntries.

/**
 * Renders the given RSS's entries.
 *
 * @param parentBlock the parent Block to which the output is going to be added
 * @param feed the RSS Channel we retrieved via the Feed URL
 * @param parameters our parameter helper object
 * @throws MacroExecutionException if the content cannot be rendered
 */
private void generateEntries(Block parentBlock, SyndFeed feed, RssMacroParameters parameters) throws MacroExecutionException {
    int maxElements = parameters.getCount();
    int count = 0;
    for (Object item : feed.getEntries()) {
        ++count;
        if (count > maxElements) {
            break;
        }
        SyndEntry entry = (SyndEntry) item;
        ResourceReference titleResourceReference = new ResourceReference(entry.getLink(), ResourceType.URL);
        Block titleBlock = new LinkBlock(parsePlainText(entry.getTitle()), titleResourceReference, true);
        ParagraphBlock paragraphTitleBlock = new ParagraphBlock(Collections.singletonList(titleBlock));
        paragraphTitleBlock.setParameter(CLASS_ATTRIBUTE, "rssitemtitle");
        parentBlock.addChild(paragraphTitleBlock);
        if (parameters.isContent() && entry.getDescription() != null) {
            // We are wrapping the feed entry content in a HTML macro, not considering what the declared content
            // is, because some feed will declare text while they actually contain HTML.
            // See http://stuffthathappens.com/blog/2007/10/29/i-hate-rss/
            // A case where doing this might hurt is if a feed declares "text" and has any XML inside it does
            // not want to be interpreted as such, but displayed as is instead. But this certainly is too rare
            // compared to mis-formed feeds that say text while they want to say HTML.
            Block html = new RawBlock(entry.getDescription().getValue(), Syntax.XHTML_1_0);
            parentBlock.addChild(new GroupBlock(Arrays.asList(html), Collections.singletonMap(CLASS_ATTRIBUTE, "rssitemdescription")));
        }
    }
}
Also used : SyndEntry(com.sun.syndication.feed.synd.SyndEntry) LinkBlock(org.xwiki.rendering.block.LinkBlock) RawBlock(org.xwiki.rendering.block.RawBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) ImageBlock(org.xwiki.rendering.block.ImageBlock) RawBlock(org.xwiki.rendering.block.RawBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference)

Example 9 with LinkBlock

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

the class DefaultLinkedResourceHelper method setResourceType.

@Override
public void setResourceType(Block block, ResourceType newResourceType) {
    if (block instanceof LinkBlock) {
        LinkBlock linkBlock = (LinkBlock) block;
        ResourceReference linkReference = linkBlock.getReference();
        linkReference.setType(newResourceType);
    } else if (block instanceof MacroBlock) {
    // N/A yet.
    }
}
Also used : LinkBlock(org.xwiki.rendering.block.LinkBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 10 with LinkBlock

use of org.xwiki.rendering.block.LinkBlock 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)

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