Search in sources :

Example 1 with BlockRenderer

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

the class XDOMOfficeDocument method getContentAsString.

/**
 * Renders the XDOM encapsulated by this document into the given syntax.
 *
 * @param syntaxId string identifier of the syntax.
 * @return content of this document in the given syntax or null if the syntax is invalid.
 */
public String getContentAsString(String syntaxId) {
    try {
        WikiPrinter printer = new DefaultWikiPrinter();
        BlockRenderer renderer = this.componentManager.getInstance(BlockRenderer.class, syntaxId);
        renderer.render(this.xdom, printer);
        return printer.toString();
    } catch (ComponentLookupException ex) {
    // Nothing to do here.
    }
    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 2 with BlockRenderer

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

the class DefaultPresentationBuilder method buildPresentationXDOM.

/**
 * Parses the given HTML text into an XDOM tree.
 *
 * @param html the HTML text to parse
 * @param targetDocumentReference specifies the document where the presentation will be imported; we use the target
 *            document reference to get the syntax of the target document and to set the {@code BASE} meta data on
 *            the created XDOM
 * @return a XDOM tree
 * @throws OfficeImporterException if parsing the given HTML fails
 */
protected XDOM buildPresentationXDOM(String html, DocumentReference targetDocumentReference) throws OfficeImporterException {
    try {
        ComponentManager contextComponentManager = this.contextComponentManagerProvider.get();
        String syntaxId = this.documentAccessBridge.getTranslatedDocumentInstance(targetDocumentReference).getSyntax().toIdString();
        BlockRenderer renderer = contextComponentManager.getInstance(BlockRenderer.class, syntaxId);
        Map<String, String> galleryParameters = Collections.emptyMap();
        ExpandedMacroBlock gallery = new ExpandedMacroBlock("gallery", galleryParameters, renderer, false, contextComponentManager);
        gallery.addChild(this.xhtmlParser.parse(new StringReader(html)));
        XDOM xdom = new XDOM(Collections.singletonList((Block) gallery));
        // Make sure (image) references are resolved relative to the target document reference.
        xdom.getMetaData().addMetaData(MetaData.BASE, entityReferenceSerializer.serialize(targetDocumentReference));
        return xdom;
    } catch (Exception e) {
        throw new OfficeImporterException("Failed to build presentation XDOM.", e);
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) ExpandedMacroBlock(org.xwiki.rendering.block.ExpandedMacroBlock) ComponentManager(org.xwiki.component.manager.ComponentManager) StringReader(java.io.StringReader) Block(org.xwiki.rendering.block.Block) ExpandedMacroBlock(org.xwiki.rendering.block.ExpandedMacroBlock) OfficeImporterException(org.xwiki.officeimporter.OfficeImporterException) OfficeImporterException(org.xwiki.officeimporter.OfficeImporterException) OfficeConverterException(org.xwiki.officeimporter.converter.OfficeConverterException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 3 with BlockRenderer

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

the class RenderingScriptServiceTest method parseAndRender.

@Test
public void parseAndRender() throws Exception {
    Parser parser = this.mocker.registerMockComponent(Parser.class, "plain/1.0");
    when(parser.parse(argThat(new StringReaderMatcher("some [[TODO]] stuff")))).thenReturn(new XDOM(Collections.<Block>emptyList()));
    BlockRenderer blockRenderer = this.mocker.registerMockComponent(BlockRenderer.class, "xwiki/2.0");
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            WikiPrinter printer = (WikiPrinter) invocationOnMock.getArguments()[1];
            printer.print("some ~[~[TODO]] stuff");
            return null;
        }
    }).when(blockRenderer).render(any(XDOM.class), any());
    XDOM xdom = this.mocker.getComponentUnderTest().parse("some [[TODO]] stuff", "plain/1.0");
    assertEquals("some ~[~[TODO]] stuff", this.mocker.getComponentUnderTest().render(xdom, "xwiki/2.0"));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StringReaderMatcher(org.xwiki.test.mockito.StringReaderMatcher) Block(org.xwiki.rendering.block.Block) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) Parser(org.xwiki.rendering.parser.Parser) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) Test(org.junit.Test)

Example 4 with BlockRenderer

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

the class DefaultOfficeViewerScriptService method render.

/**
 * Renders the given XDOM into specified syntax.
 *
 * @param xdom the {@link XDOM} to be rendered
 * @param fromSyntax the syntax for which to perform the transformations
 * @param toSyntax expected output syntax
 * @return string holding the result of rendering
 * @throws Exception if an error occurs during rendering
 */
private String render(XDOM xdom, Syntax fromSyntax, Syntax toSyntax) throws Exception {
    // Perform the transformations. This is required for office presentations which use the gallery macro to display
    // the slide images.
    TransformationContext context = new TransformationContext(xdom, fromSyntax);
    context.setTargetSyntax(toSyntax);
    this.transformationManager.performTransformations(xdom, context);
    WikiPrinter printer = new DefaultWikiPrinter();
    BlockRenderer renderer = this.componentManager.getInstance(BlockRenderer.class, toSyntax.toIdString());
    renderer.render(xdom, printer);
    return printer.toString();
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 5 with BlockRenderer

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

the class DefaultHTMLConverterTest method parseAndRender.

/**
 * Unit test for {@link DefaultHTMLConverter#parseAndRender(String, String)}.
 */
@Test
public void parseAndRender() throws Exception {
    String html = "some HTML";
    String syntaxId = "syntax/x.y";
    // Verify the HTML is cleaned.
    HTMLCleaner cleaner = mocker.getInstance(HTMLCleaner.class);
    when(cleaner.clean(html)).thenReturn(html);
    // Verify the HTML is parsed into XDOM.
    XDOM xdom = new XDOM(Collections.<Block>emptyList());
    Parser xhtmlParser = mocker.getInstance(Parser.class, "xhtml/1.0");
    when(xhtmlParser.parse(any(StringReader.class))).thenReturn(xdom);
    Assert.assertEquals("", mocker.getComponentUnderTest().parseAndRender(html, 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));
    // Verify that the syntax meta data has been set.
    Assert.assertEquals(Syntax.valueOf("syntax/x.y"), xdom.getMetaData().getMetaData(MetaData.SYNTAX));
}
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) HTMLCleaner(org.xwiki.wysiwyg.cleaner.HTMLCleaner) Parser(org.xwiki.rendering.parser.Parser) StreamParser(org.xwiki.rendering.parser.StreamParser) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) Test(org.junit.Test)

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