Search in sources :

Example 11 with BlockRenderer

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

the class DefaultHTMLConverterTest method toHTML.

/**
 * Unit test for {@link DefaultHTMLConverter#toHTML(String, String)}.
 */
@Test
public void toHTML() throws Exception {
    String source = "wiki syntax";
    String syntaxId = "syntax/x.y";
    // The source should be parsed.
    Parser parser = this.mocker.registerMockComponent(Parser.class, syntaxId);
    XDOM xdom = new XDOM(Collections.<Block>emptyList());
    when(parser.parse(any(StringReader.class))).thenReturn(xdom);
    Assert.assertEquals("", mocker.getComponentUnderTest().toHTML(source, syntaxId));
    // Verify that the macro transformations have been executed.
    Transformation macroTransformation = mocker.getInstance(Transformation.class, "macro");
    RenderingContext renderingContext = mocker.getInstance(RenderingContext.class);
    // It's very important to verify that a transformation context id is set as otherwise if the content being
    // edited has different velocity macros executing, they'll be executed in isolation and thus what's defined in
    // one won't be visible from the other ones (For example see https://jira.xwiki.org/browse/XWIKI-11695).
    ArgumentCaptor<TransformationContext> txContextArgument = ArgumentCaptor.forClass(TransformationContext.class);
    verify((MutableRenderingContext) renderingContext).transformInContext(same(macroTransformation), txContextArgument.capture(), same(xdom));
    assertEquals("wysiwygtxid", txContextArgument.getValue().getId());
    // Verify the XDOM is rendered to Annotated XHTML.
    BlockRenderer xhtmlRenderer = mocker.getInstance(BlockRenderer.class, "annotatedxhtml/1.0");
    verify(xhtmlRenderer).render(same(xdom), any(WikiPrinter.class));
}
Also used : RenderingContext(org.xwiki.rendering.transformation.RenderingContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) Transformation(org.xwiki.rendering.transformation.Transformation) XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) Parser(org.xwiki.rendering.parser.Parser) StreamParser(org.xwiki.rendering.parser.StreamParser) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) Test(org.junit.Test)

Example 12 with BlockRenderer

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

the class XDOMOfficeDocument method renderTitle.

/**
 * Utility method for rendering a title.
 *
 * @param header header block which contains the title.
 * @return header block content rendered as a string.
 */
private String renderTitle(HeaderBlock header) {
    try {
        WikiPrinter printer = new DefaultWikiPrinter();
        BlockRenderer renderer = this.componentManager.getInstance(BlockRenderer.class, "plain/1.0");
        renderer.render(header, printer);
        return printer.toString();
    } catch (ComponentLookupException ex) {
    // Ignore.
    }
    return null;
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 13 with BlockRenderer

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

the class DefaultOfficeViewerScriptServiceTest method view.

@Test
public void view() throws Exception {
    Execution execution = mocker.getInstance(Execution.class);
    when(execution.getContext()).thenReturn(new ExecutionContext());
    AttachmentReference attachmentReference = new AttachmentReference("file.odt", new DocumentReference("wiki", "Space", "Page"));
    DocumentModelBridge document = mock(DocumentModelBridge.class);
    DocumentAccessBridge documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
    when(documentAccessBridge.isDocumentViewable(attachmentReference.getDocumentReference())).thenReturn(true);
    when(documentAccessBridge.getTranslatedDocumentInstance(attachmentReference.getDocumentReference())).thenReturn(document);
    when(document.getSyntax()).thenReturn(Syntax.TEX_1_0);
    XDOM xdom = new XDOM(Collections.<Block>emptyList());
    OfficeViewer viewer = mocker.getInstance(OfficeViewer.class);
    when(viewer.createView(attachmentReference, Collections.<String, String>emptyMap())).thenReturn(xdom);
    BlockRenderer xhtmlRenderer = mocker.registerMockComponent(BlockRenderer.class, "xhtml/1.0");
    Assert.assertEquals("", mocker.getComponentUnderTest().view(attachmentReference));
    TransformationManager transformationManager = mocker.getInstance(TransformationManager.class);
    verify(transformationManager).performTransformations(eq(xdom), notNull(TransformationContext.class));
    verify(xhtmlRenderer).render(eq(xdom), notNull(WikiPrinter.class));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) OfficeViewer(org.xwiki.office.viewer.OfficeViewer) TransformationManager(org.xwiki.rendering.transformation.TransformationManager) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) DocumentReference(org.xwiki.model.reference.DocumentReference) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) Test(org.junit.Test)

Example 14 with BlockRenderer

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

the class DisplayScriptService method renderXDOM.

/**
 * Renders the provided XDOM.
 *
 * @param content the XDOM content to render
 * @param targetSyntax the syntax of the rendering result
 * @return the result of rendering the given XDOM
 * @throws XWikiException if an exception occurred during the rendering process
 */
private String renderXDOM(XDOM content, Syntax targetSyntax) throws XWikiException {
    try {
        BlockRenderer renderer = this.componentManager.getInstance(BlockRenderer.class, targetSyntax.toIdString());
        WikiPrinter printer = new DefaultWikiPrinter();
        renderer.render(content, printer);
        return printer.toString();
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_RENDERING, XWikiException.ERROR_XWIKI_UNKNOWN, "Failed to render XDOM to syntax [" + targetSyntax + "]", e);
    }
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) XWikiException(com.xpn.xwiki.XWikiException) XWikiException(com.xpn.xwiki.XWikiException) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 15 with BlockRenderer

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

the class RenderingScriptService method render.

/**
 * Render a list of Blocks into the passed syntax.
 *
 * @param block the block to render
 * @param outputSyntaxId the syntax in which to render the blocks
 * @return the string representing the passed blocks in the passed syntax or null if an error occurred
 * @since 3.2M3
 */
public String render(Block block, String outputSyntaxId) {
    String result;
    WikiPrinter printer = new DefaultWikiPrinter();
    try {
        BlockRenderer renderer = this.componentManagerProvider.get().getInstance(BlockRenderer.class, outputSyntaxId);
        renderer.render(block, printer);
        result = printer.toString();
    } catch (Exception e) {
        result = null;
    }
    return result;
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) ParseException(org.xwiki.rendering.parser.ParseException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MacroLookupException(org.xwiki.rendering.macro.MacroLookupException) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Aggregations

BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)17 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)14 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)8 Test (org.junit.Test)7 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)6 XDOM (org.xwiki.rendering.block.XDOM)6 StringReader (java.io.StringReader)4 Block (org.xwiki.rendering.block.Block)4 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)4 Parser (org.xwiki.rendering.parser.Parser)3 XWikiException (com.xpn.xwiki.XWikiException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 MutableRenderingContext (org.xwiki.rendering.internal.transformation.MutableRenderingContext)2 ParseException (org.xwiki.rendering.parser.ParseException)2 StreamParser (org.xwiki.rendering.parser.StreamParser)2 RenderingContext (org.xwiki.rendering.transformation.RenderingContext)2 Transformation (org.xwiki.rendering.transformation.Transformation)2 BaseObject (com.xpn.xwiki.objects.BaseObject)1 IOException (java.io.IOException)1