Search in sources :

Example 31 with ResourceReference

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);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) EntityReference(org.xwiki.model.reference.EntityReference) Block(org.xwiki.rendering.block.Block) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 32 with ResourceReference

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;
}
Also used : SyndFeed(com.sun.syndication.feed.synd.SyndFeed) 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) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) BoxMacroParameters(org.xwiki.rendering.macro.box.BoxMacroParameters)

Example 33 with ResourceReference

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);
}
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 34 with ResourceReference

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);
}
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 35 with ResourceReference

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

Aggregations

ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)51 DocumentReference (org.xwiki.model.reference.DocumentReference)27 Test (org.junit.Test)26 LinkBlock (org.xwiki.rendering.block.LinkBlock)20 XDOM (org.xwiki.rendering.block.XDOM)16 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)15 AttachmentResourceReference (org.xwiki.rendering.listener.reference.AttachmentResourceReference)13 Block (org.xwiki.rendering.block.Block)12 SpaceReference (org.xwiki.model.reference.SpaceReference)11 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)9 AttachmentReference (org.xwiki.model.reference.AttachmentReference)9 ImageBlock (org.xwiki.rendering.block.ImageBlock)8 MacroBlock (org.xwiki.rendering.block.MacroBlock)8 ResourceType (org.xwiki.rendering.listener.reference.ResourceType)8 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)7 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)7 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)7 EntityReference (org.xwiki.model.reference.EntityReference)6 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)6 GroupBlock (org.xwiki.rendering.block.GroupBlock)5