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