use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class DefaultOldRendering method refactorDocumentLinks.
/**
* @since 2.2M1
*/
private void refactorDocumentLinks(XWikiDocument document, DocumentReference oldDocumentReference, DocumentReference newDocumentReference, XWikiContext context) throws XWikiException {
// FIXME: Duplicate code. See org.xwiki.refactoring.internal.DefaultLinkRefactoring#renameLinks in
// xwiki-platform-refactoring-default
DocumentReference currentDocumentReference = document.getDocumentReference();
XDOM xdom = document.getXDOM();
List<Block> blocks = linkedResourceHelper.getBlocks(xdom);
for (Block block : blocks) {
ResourceReference resourceReference = linkedResourceHelper.getResourceReference(block);
if (resourceReference == null) {
// Skip invalid blocks.
continue;
}
ResourceType resourceType = resourceReference.getType();
// TODO: support ATTACHMENT as well.
if (!ResourceType.DOCUMENT.equals(resourceType) && !ResourceType.SPACE.equals(resourceType)) {
// We are currently only interested in Document or Space references.
continue;
}
// Resolve the resource reference.
EntityReference linkEntityReference = resourceReferenceResolver.resolve(resourceReference, null, currentDocumentReference);
// Resolve the document of the reference.
DocumentReference linkTargetDocumentReference = defaultReferenceDocumentReferenceResolver.resolve(linkEntityReference);
EntityReference newTargetReference = newDocumentReference;
ResourceType newResourceType = resourceType;
// If the link was resolved to a space...
if (EntityType.SPACE.equals(linkEntityReference.getType())) {
if (XWiki.DEFAULT_SPACE_HOMEPAGE.equals(newDocumentReference.getName())) {
// If the new document reference is also a space (non-terminal doc), be careful to keep it
// serialized as a space still (i.e. without ".WebHome") and not serialize it as a doc by mistake
// (i.e. with ".WebHome").
newTargetReference = newDocumentReference.getLastSpaceReference();
} else {
// If the new target is a non-terminal document, we can not use a "space:" resource type to access
// it anymore. To fix it, we need to change the resource type of the link reference "doc:".
newResourceType = ResourceType.DOCUMENT;
}
}
// If the link targets the old (renamed) document reference, we must update it.
if (linkTargetDocumentReference.equals(oldDocumentReference)) {
String newReferenceString = this.compactEntityReferenceSerializer.serialize(newTargetReference, currentDocumentReference);
// Update the reference in the XDOM.
linkedResourceHelper.setResourceReferenceString(block, newReferenceString);
linkedResourceHelper.setResourceType(block, newResourceType);
}
}
document.setContent(xdom);
}
use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.
the class RssMacro method execute.
@Override
public List<Block> execute(RssMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
List<Block> result;
SyndFeed feed = this.romeFeedFactory.createFeed(parameters);
if (parameters.isDecoration()) {
BoxMacroParameters boxParameters = new BoxMacroParameters();
boolean hasImage = parameters.isImage() && (feed.getImage() != null);
boxParameters.setCssClass(FEED_CLASS_VALUE);
if (StringUtils.isNotEmpty(parameters.getWidth())) {
boxParameters.setWidth(parameters.getWidth());
}
boxParameters.setBlockTitle(generateBoxTitle("rsschanneltitle", feed));
if (hasImage) {
boxParameters.setImage(new ResourceReference(feed.getImage().getUrl(), ResourceType.URL));
}
result = this.boxMacro.execute(boxParameters, content == null ? StringUtils.EMPTY : content, context);
} else {
result = Arrays.<Block>asList(new GroupBlock(Collections.singletonMap(CLASS_ATTRIBUTE, FEED_CLASS_VALUE)));
}
generateEntries(result.get(0), feed, parameters);
return result;
}
use of org.xwiki.rendering.listener.reference.ResourceReference 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.listener.reference.ResourceReference 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.listener.reference.ResourceReference 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);
}
Aggregations