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;
}
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);
}
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);
}
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);
}
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());
}
Aggregations