Search in sources :

Example 11 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter in project xwiki-platform by xwiki.

the class DefaultNotificationRSSRenderer method renderNotification.

@Override
public SyndEntry renderNotification(CompositeEvent eventNotification) throws NotificationException {
    SyndEntry entry = new SyndEntryImpl();
    SyndContent entryDescription = new SyndContentImpl();
    // The users contained in the CompositeEvent are already stored in a Set, they are therefore necessarily unique
    List<SyndPerson> eventAuthors = new ArrayList<SyndPerson>();
    // Convert every author of the CompositeEvent to a SyndPerson and add it to the new entry
    for (DocumentReference author : eventNotification.getUsers()) {
        SyndPerson person = new SyndPersonImpl();
        person.setName(author.getName());
        eventAuthors.add(person);
    }
    entry.setAuthors(eventAuthors);
    // Define the GUID of the event
    entry.setUri(String.join("-", eventNotification.getEventIds()));
    // Set the entry title
    entry.setTitle(this.contextualLocalizationManager.getTranslationPlain(eventNotification.getEvents().get(0).getTitle(), eventNotification.getEvents().get(0).getDocumentTitle()));
    // Render the description (the main part) of the feed entry
    try {
        this.scriptContextManager.getCurrentScriptContext().setAttribute(COMPOSITE_EVENT_BUILDING_NAME, eventNotification, ScriptContext.ENGINE_SCOPE);
        // Try to get a template associated with the composite event
        Template template = this.templateManager.getTemplate(String.format("notification/rss/%s.vm", eventNotification.getType().replaceAll("\\/", ".")));
        // If no template is found, fallback on the default one
        if (template == null) {
            template = this.templateManager.getTemplate("notification/rss/default.vm");
        }
        XDOM descriptionXDOM = this.templateManager.execute(template);
        WikiPrinter printer = new DefaultWikiPrinter();
        blockRenderer.render(descriptionXDOM, printer);
        // Add the description to the entry
        entryDescription.setType("text/html");
        entryDescription.setValue(printer.toString());
        entry.setDescription(entryDescription);
    } catch (Exception e) {
        throw new NotificationException(String.format("Unable to render the description of the event [%s].", eventNotification), e);
    } finally {
        this.scriptContextManager.getCurrentScriptContext().removeAttribute(COMPOSITE_EVENT_BUILDING_NAME, ScriptContext.ENGINE_SCOPE);
    }
    // Dates are sorted in descending order in a CompositeEvent, the first date is then the most recent one
    entry.setUpdatedDate(eventNotification.getDates().get(0));
    return entry;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) SyndContentImpl(com.rometools.rome.feed.synd.SyndContentImpl) ArrayList(java.util.ArrayList) NotificationException(org.xwiki.notifications.NotificationException) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) NotificationException(org.xwiki.notifications.NotificationException) Template(org.xwiki.template.Template) SyndPerson(com.rometools.rome.feed.synd.SyndPerson) SyndPersonImpl(com.rometools.rome.feed.synd.SyndPersonImpl) SyndContent(com.rometools.rome.feed.synd.SyndContent) SyndEntryImpl(com.rometools.rome.feed.synd.SyndEntryImpl) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 12 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testExecuteWhenWikiMacroBinding.

@Test
public void testExecuteWhenWikiMacroBinding() throws Exception {
    registerWikiMacro("wikimacrobindings", "{{groovy}}" + "print xcontext.macro.doc" + "{{/groovy}}");
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    converter.convert(new StringReader("{{wikimacrobindings param1=\"value2\" param2=\"value2\"/}}"), Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
    Assert.assertEquals("<p>" + this.wikiMacroDocument.toString() + "</p>", printer.toString());
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) Test(org.junit.Test)

Example 13 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testExecuteWhenWikiRequiringPRAfterDropPermission.

@Test
public void testExecuteWhenWikiRequiringPRAfterDropPermission() throws Exception {
    registerWikiMacro("wikimacrobindings", "{{groovy}}" + "print xcontext.macro.doc" + "{{/groovy}}");
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    getContext().dropPermissions();
    this.wikiMacroDocument.newDocument(getContext()).dropPermissions();
    converter.convert(new StringReader("{{wikimacrobindings param1=\"value2\" param2=\"value2\"/}}"), Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
    Assert.assertEquals("<p>" + this.wikiMacroDocument.toString() + "</p>", printer.toString());
    Assert.assertTrue("Wiki macro did not properly restord persmission dropping", getContext().hasDroppedPermissions());
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) Test(org.junit.Test)

Example 14 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testExecuteWhenInnerMacro.

/**
 * Check that macro used inside wiki macro are executed as part of the document.
 */
@Test
public void testExecuteWhenInnerMacro() throws Exception {
    registerWikiMacro("wikimacro1", "{{toc/}}", Syntax.XWIKI_2_0);
    getMockery().checking(new Expectations() {

        {
            DocumentResourceReference reference = new DocumentResourceReference(null);
            reference.setAnchor("Hheading");
            allowing(mockWikiModel).getDocumentViewURL(reference);
            will(returnValue("url"));
        }
    });
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    converter.convert(new StringReader("= heading\n\n{{wikimacro1 param1=\"value1\" param2=\"value2\"/}}"), Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
    Assert.assertEquals("<h1 id=\"Hheading\" class=\"wikigeneratedid\"><span>heading</span></h1>" + "<ul><li><span class=\"wikilink\"><a href=\"#Hheading\">heading</a></span></li></ul>", printer.toString());
}
Also used : Expectations(org.jmock.Expectations) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) Test(org.junit.Test)

Example 15 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testExecuteWhenInlineAndWithMacro.

/**
 * When a wiki macro is used in inline mode and its code starts with a macro, that nested macro is made inline. In
 * other words, the nested macro should not generate extra paragraph elements.
 */
@Test
public void testExecuteWhenInlineAndWithMacro() throws Exception {
    registerWikiMacro("wikimacro1", "This is **bold**", Syntax.XWIKI_2_0);
    registerWikiMacro("wikimacro2", "{{wikimacro1 param1=\"v1\" param2=\"v2\"/}}", Syntax.XWIKI_2_0);
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    // Note: We're putting the macro after the "Hello" text to force it as an inline macro.
    converter.convert(new StringReader("Hello {{wikimacro2 param1=\"value1\" param2=\"value2\"/}}"), Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
    Assert.assertEquals("<p>Hello This is <strong>bold</strong></p>", printer.toString());
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) Test(org.junit.Test)

Aggregations

DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)41 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)28 StringReader (java.io.StringReader)18 Test (org.junit.Test)12 Converter (org.xwiki.rendering.converter.Converter)12 XDOM (org.xwiki.rendering.block.XDOM)10 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)8 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)7 Block (org.xwiki.rendering.block.Block)6 ParseException (org.xwiki.rendering.parser.ParseException)5 Parser (org.xwiki.rendering.parser.Parser)5 TransformationException (org.xwiki.rendering.transformation.TransformationException)4 XWikiException (com.xpn.xwiki.XWikiException)3 HashMap (java.util.HashMap)3 Expectations (org.jmock.Expectations)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 Reader (java.io.Reader)2