Search in sources :

Example 11 with LinkBlock

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

the class DefaultLinkedResourceHelper method getResourceReferenceString.

@Override
public String getResourceReferenceString(Block block) {
    // Determine the reference string and reference type for each block type.
    String referenceString = null;
    if (block instanceof LinkBlock) {
        LinkBlock linkBlock = (LinkBlock) block;
        ResourceReference linkReference = linkBlock.getReference();
        referenceString = linkReference.getReference();
    } else if (block instanceof MacroBlock) {
        referenceString = block.getParameter(REFERENCE_MACRO_PARAMETER);
        if (StringUtils.isBlank(referenceString)) {
            referenceString = block.getParameter(DOCUMENT_MACRO_PARAMETER);
        }
        if (StringUtils.isBlank(referenceString)) {
            // If the reference is not set or is empty, we have a recursive include which is not valid anyway.
            // Skip it.
            referenceString = null;
        }
    }
    return referenceString;
}
Also used : LinkBlock(org.xwiki.rendering.block.LinkBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 12 with LinkBlock

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

the class RssMacro method generateBoxTitle.

/**
 * Renders the RSS's title.
 *
 * @param cssClass the CSS sheet
 * @param feed the RSS feed data
 * @return the list of blocks making the RSS Box title
 */
private List<? extends Block> generateBoxTitle(String cssClass, SyndFeed feed) {
    List<Block> titleBlocks;
    if (feed.getLink() == null) {
        titleBlocks = parsePlainText(feed.getTitle());
    } else {
        // Title link.
        ResourceReference titleResourceReference = new ResourceReference(feed.getLink(), ResourceType.URL);
        // Title text link.
        Block titleTextLinkBlock = new LinkBlock(parsePlainText(feed.getTitle()), titleResourceReference, true);
        // Rss icon.
        String imagePath = this.skinAccessBridge.getSkinFile(FEED_ICON_RESOURCE_PATH);
        ImageBlock imageBlock = new ImageBlock(new ResourceReference(imagePath, ResourceType.URL), false);
        // Title rss icon link.
        Block titleImageLinkBlock = new LinkBlock(Arrays.<Block>asList(imageBlock), titleResourceReference, true);
        titleBlocks = Arrays.<Block>asList(titleTextLinkBlock, titleImageLinkBlock);
    }
    ParagraphBlock titleBlock = new ParagraphBlock(titleBlocks);
    titleBlock.setParameter(CLASS_ATTRIBUTE, cssClass);
    return Collections.singletonList(titleBlock);
}
Also used : ImageBlock(org.xwiki.rendering.block.ImageBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) 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 13 with LinkBlock

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

the class ContextMacroTest method executeOk.

@Test
public void executeOk() throws Exception {
    MacroBlock macroBlock = new MacroBlock("context", Collections.<String, String>emptyMap(), false);
    MacroTransformationContext macroContext = new MacroTransformationContext();
    macroContext.setSyntax(Syntax.XWIKI_2_0);
    macroContext.setCurrentMacroBlock(macroBlock);
    DocumentReferenceResolver<String> resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "macro");
    DocumentReference referencedDocumentReference = new DocumentReference("wiki", "space", "page");
    when(resolver.resolve("wiki:space.page", macroBlock)).thenReturn(referencedDocumentReference);
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    DocumentModelBridge dmb = mock(DocumentModelBridge.class);
    when(dab.getTranslatedDocumentInstance(referencedDocumentReference)).thenReturn(dmb);
    MacroContentParser parser = this.mocker.getInstance(MacroContentParser.class);
    XDOM xdom = new XDOM(Arrays.asList((Block) new ParagraphBlock(Arrays.asList((Block) new LinkBlock(Collections.emptyList(), new ResourceReference("", ResourceType.DOCUMENT), false)))));
    when(parser.parse(eq(""), same(macroContext), eq(false), any(MetaData.class), eq(false))).thenReturn(xdom);
    ContextMacroParameters parameters = new ContextMacroParameters();
    parameters.setDocument("wiki:space.page");
    // Note: we're not testing the returned value here since this is done in integation tests.
    this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) MacroContentParser(org.xwiki.rendering.macro.MacroContentParser) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) LinkBlock(org.xwiki.rendering.block.LinkBlock) MetaData(org.xwiki.rendering.listener.MetaData) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) MacroBlock(org.xwiki.rendering.block.MacroBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) ContextMacroParameters(org.xwiki.rendering.macro.context.ContextMacroParameters) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 14 with LinkBlock

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

the class ChartMacro method execute.

@Override
public List<Block> execute(ChartMacroParameters macroParams, String content, MacroTransformationContext context) throws MacroExecutionException {
    // Generate the chart image in a temporary location.
    generateChart(macroParams, content, context);
    String imageLocation = this.imageWriter.getURL(new ImageId(macroParams));
    String title = macroParams.getTitle();
    ResourceReference reference = new ResourceReference(imageLocation, ResourceType.URL);
    ImageBlock imageBlock = new ImageBlock(new ResourceReference(imageLocation, ResourceType.URL), true);
    imageBlock.setParameter("alt", title);
    LinkBlock linkBlock = new LinkBlock(Collections.singletonList((Block) imageBlock), reference, true);
    linkBlock.setParameter("title", title);
    // If the macro is used standalone then we need to wrap it in a paragraph block.
    Block resultBlock;
    if (context.isInline()) {
        resultBlock = linkBlock;
    } else {
        resultBlock = new ParagraphBlock(Collections.singletonList((Block) linkBlock));
    }
    return Collections.singletonList(resultBlock);
}
Also used : ImageBlock(org.xwiki.rendering.block.ImageBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) ImageBlock(org.xwiki.rendering.block.ImageBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference)

Example 15 with LinkBlock

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

the class DefaultDocumentSplitterTest method updateAnchors.

@Test
public void updateAnchors() throws Exception {
    SplittingCriterion splittingCriterion = mock(SplittingCriterion.class);
    when(splittingCriterion.shouldSplit(any(Block.class), anyInt())).thenReturn(false, true, true);
    when(splittingCriterion.shouldIterate(any(SectionBlock.class), anyInt())).thenReturn(false, false, false);
    NamingCriterion namingCriterion = mock(NamingCriterion.class);
    when(namingCriterion.getDocumentName(any(XDOM.class))).thenReturn("Child1", "Child2");
    // [[link>>||anchor="chapter1"]]
    // = {{id name="chapter1"}}Chapter 1 =
    // = Chapter 2 ==
    // [[link>>path:#chapter1]]
    ResourceReference reference = new ResourceReference("", ResourceType.DOCUMENT);
    reference.setParameter("anchor", "chapter1");
    LinkBlock link = new LinkBlock(Arrays.<Block>asList(new WordBlock("link")), reference, false);
    ParagraphBlock paragraph = new ParagraphBlock(Arrays.<Block>asList(link));
    HeaderBlock header = new HeaderBlock(Arrays.<Block>asList(new IdBlock("chapter1"), new WordBlock("Chapter 1")), HeaderLevel.LEVEL1);
    SectionBlock section1 = new SectionBlock(Arrays.<Block>asList(header));
    header = new HeaderBlock(Arrays.<Block>asList(new WordBlock("Chapter 2")), HeaderLevel.LEVEL1);
    reference = new ResourceReference("#chapter1", ResourceType.PATH);
    link = new LinkBlock(Arrays.<Block>asList(new WordBlock("link")), reference, false);
    SectionBlock section2 = new SectionBlock(Arrays.<Block>asList(header, new ParagraphBlock(Arrays.<Block>asList(link))));
    XDOM xdom = new XDOM(Arrays.<Block>asList(paragraph, section1, section2));
    WikiDocument document = new WikiDocument("Space.Page", xdom, null);
    List<WikiDocument> result = mocker.getComponentUnderTest().split(document, splittingCriterion, namingCriterion);
    ClassBlockMatcher linkMatcher = new ClassBlockMatcher(LinkBlock.class);
    ResourceReference updatedReference = document.getXdom().<LinkBlock>getFirstBlock(linkMatcher, Axes.DESCENDANT).getReference();
    assertEquals("chapter1", updatedReference.getParameter("anchor"));
    assertEquals(result.get(1).getFullName(), updatedReference.getReference());
    updatedReference = result.get(2).getXdom().<LinkBlock>getFirstBlock(linkMatcher, Axes.DESCENDANT).getReference();
    assertEquals(ResourceType.DOCUMENT, updatedReference.getType());
    assertEquals("chapter1", updatedReference.getParameter("anchor"));
    assertEquals(result.get(1).getFullName(), updatedReference.getReference());
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) XDOM(org.xwiki.rendering.block.XDOM) WordBlock(org.xwiki.rendering.block.WordBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) WikiDocument(org.xwiki.refactoring.WikiDocument) IdBlock(org.xwiki.rendering.block.IdBlock) NamingCriterion(org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion) LinkBlock(org.xwiki.rendering.block.LinkBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) 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) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) SectionBlock(org.xwiki.rendering.block.SectionBlock) SplittingCriterion(org.xwiki.refactoring.splitter.criterion.SplittingCriterion) 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