Search in sources :

Example 21 with ResourceReference

use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.

the class DefaultLinkRefactoringTest method renameNonTerminalToTerminalDocumentLinks.

@Test
public void renameNonTerminalToTerminalDocumentLinks() 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 non-terminal document to a terminal document.
    DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "WebHome");
    DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y");
    XDOM xdom = mock(XDOM.class);
    when(document.getXDOM()).thenReturn(xdom);
    ResourceReference docLinkReference = new ResourceReference("A.WebHome", ResourceType.DOCUMENT);
    LinkBlock documentLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false);
    ResourceReference spaceLinkReference = new ResourceReference("A", ResourceType.SPACE);
    LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false);
    when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(documentLinkBlock, spaceLinkBlock));
    // Doc link
    when(this.resourceReferenceResolver.resolve(docLinkReference, null, documentReference)).thenReturn(oldLinkTarget);
    when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget);
    when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y");
    // Space link
    SpaceReference spaceReference = oldLinkTarget.getLastSpaceReference();
    when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, documentReference)).thenReturn(spaceReference);
    when(this.defaultReferenceDocumentReferenceResolver.resolve(spaceReference)).thenReturn(oldLinkTarget);
    when(this.compactEntityReferenceSerializer.serialize(newLinkTarget.getLastSpaceReference(), documentReference)).thenReturn("X");
    this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget);
    // Note that both resulting renamed back-links are of type document. (i.e. the space link was converted to a doc
    // link)
    assertEquals("X.Y", documentLinkBlock.getReference().getReference());
    assertEquals(ResourceType.DOCUMENT, documentLinkBlock.getReference().getType());
    assertEquals("X.Y", spaceLinkBlock.getReference().getReference());
    assertEquals(ResourceType.DOCUMENT, spaceLinkBlock.getReference().getType());
    verifyDocumentSave(document, "Renamed back-links.", false);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XDOM(org.xwiki.rendering.block.XDOM) LinkBlock(org.xwiki.rendering.block.LinkBlock) SpaceReference(org.xwiki.model.reference.SpaceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 22 with ResourceReference

use of org.xwiki.rendering.listener.reference.ResourceReference 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);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XDOM(org.xwiki.rendering.block.XDOM) LinkBlock(org.xwiki.rendering.block.LinkBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 23 with ResourceReference

use of org.xwiki.rendering.listener.reference.ResourceReference 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);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) LinkBlock(org.xwiki.rendering.block.LinkBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 24 with ResourceReference

use of org.xwiki.rendering.listener.reference.ResourceReference 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")));
        }
    }
}
Also used : SyndEntry(com.sun.syndication.feed.synd.SyndEntry) LinkBlock(org.xwiki.rendering.block.LinkBlock) RawBlock(org.xwiki.rendering.block.RawBlock) 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) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference)

Example 25 with ResourceReference

use of org.xwiki.rendering.listener.reference.ResourceReference in project xwiki-platform by xwiki.

the class UserAvatarMacro method execute.

@Override
public List<Block> execute(UserAvatarMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    DocumentReference userReference = this.currentDocumentReferenceResolver.resolve(parameters.getUsername(), new EntityReference(USER_SPACE, EntityType.SPACE));
    // Find the avatar attachment name or null if not defined or an error happened when locating it
    String fileName = null;
    if (this.documentAccessBridge.exists(userReference)) {
        Object avatarProperty = this.documentAccessBridge.getProperty(userReference, new DocumentReference(userReference.getWikiReference().getName(), USER_SPACE, "XWikiUsers"), "avatar");
        if (avatarProperty != null) {
            fileName = avatarProperty.toString();
        }
    } else {
        throw new MacroExecutionException("User [" + this.compactWikiEntityReferenceSerializer.serialize(userReference) + "] is not registered in this wiki");
    }
    // Initialize with the default avatar.
    ResourceReference imageReference = new ResourceReference(this.skinAccessBridge.getSkinFile("icons/xwiki/noavatar.png"), ResourceType.URL);
    // Try to use the configured avatar.
    if (!StringUtils.isBlank(fileName)) {
        AttachmentReference attachmentReference = new AttachmentReference(fileName, userReference);
        // Check if the configured avatar file actually exists.
        try {
            if (documentAccessBridge.getAttachmentVersion(attachmentReference) != null) {
                // Use it.
                imageReference = new ResourceReference(this.compactWikiEntityReferenceSerializer.serialize(attachmentReference), ResourceType.ATTACHMENT);
            }
        } catch (Exception e) {
            // Log and fallback on default.
            logger.warn("Failed to get the avatar for user [{}]: [{}]. Using default.", this.compactWikiEntityReferenceSerializer.serialize(userReference), ExceptionUtils.getRootCauseMessage(e));
        }
    }
    ImageBlock imageBlock = new ImageBlock(imageReference, false);
    imageBlock.setParameter("alt", "Picture of " + userReference.getName());
    imageBlock.setParameter("title", userReference.getName());
    if (parameters.getWidth() != null) {
        imageBlock.setParameter("width", String.valueOf(parameters.getWidth()));
    }
    if (parameters.getHeight() != null) {
        imageBlock.setParameter("height", String.valueOf(parameters.getHeight()));
    }
    List<Block> result = Collections.singletonList(imageBlock);
    return context.isInline() ? result : Collections.singletonList(new GroupBlock(result));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ImageBlock(org.xwiki.rendering.block.ImageBlock) EntityReference(org.xwiki.model.reference.EntityReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) GroupBlock(org.xwiki.rendering.block.GroupBlock) Block(org.xwiki.rendering.block.Block) ImageBlock(org.xwiki.rendering.block.ImageBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

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