Search in sources :

Example 1 with TransformationContext

use of org.xwiki.rendering.transformation.TransformationContext in project xwiki-platform by xwiki.

the class DefaultIOTargetService method getTransformedXDOM.

private XDOM getTransformedXDOM(String content, String sourceSyntaxId) throws ParseException, org.xwiki.component.manager.ComponentLookupException, TransformationException {
    Parser parser = componentManager.getInstance(Parser.class, sourceSyntaxId);
    XDOM xdom = parser.parse(new StringReader(content));
    // run transformations
    TransformationContext txContext = new TransformationContext(xdom, Syntax.valueOf(sourceSyntaxId));
    TransformationManager transformationManager = componentManager.getInstance(TransformationManager.class);
    transformationManager.performTransformations(xdom, txContext);
    return xdom;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) TransformationManager(org.xwiki.rendering.transformation.TransformationManager) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) Parser(org.xwiki.rendering.parser.Parser)

Example 2 with TransformationContext

use of org.xwiki.rendering.transformation.TransformationContext in project xwiki-platform by xwiki.

the class DefaultHTMLConverter method executeMacroTransformation.

private void executeMacroTransformation(XDOM xdom, Syntax syntax) throws TransformationException, ParseException {
    TransformationContext txContext = new TransformationContext();
    txContext.setXDOM(xdom);
    txContext.setSyntax(syntax);
    // It's very important to set a Transformation id as otherwise if any Velocity Macro is executed it'll be
    // executed in isolation (and if you have, say, 2 velocity macros, the second one will not 'see' what's defined
    // in the first one...
    txContext.setId(TRANSFORMATION_ID);
    ((MutableRenderingContext) this.renderingContext).transformInContext(this.macroTransformation, txContext, xdom);
}
Also used : TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext)

Example 3 with TransformationContext

use of org.xwiki.rendering.transformation.TransformationContext 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 4 with TransformationContext

use of org.xwiki.rendering.transformation.TransformationContext in project xwiki-platform by xwiki.

the class WikiUIExtensionRenderer method execute.

/**
 * @return the rendered content of the extension
 */
public CompositeBlock execute() {
    // We need to clone the xdom to avoid transforming the original and make it useless after the first
    // transformation
    XDOM transformedXDOM = xdom.clone();
    // Perform macro transformations.
    try {
        // Get the document holding the UIX and put it in the UIX context
        XWikiDocument xdoc = getXWikiContext().getWiki().getDocument(documentReference, getXWikiContext());
        Map<String, Object> uixContext = new HashMap<>();
        uixContext.put(WikiUIExtension.CONTEXT_UIX_DOC_KEY, xdoc.newDocument(getXWikiContext()));
        // Put the UIX context in the XWiki context
        getXWikiContext().put(WikiUIExtension.CONTEXT_UIX_KEY, uixContext);
        // Transform the macros
        TransformationContext transformationContext = new TransformationContext(xdom, xdoc.getSyntax());
        transformationContext.setId(roleHint);
        ((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, transformationContext, transformedXDOM);
    } catch (TransformationException e) {
        LOGGER.warn("Error while executing wiki component macro transformation for extension [{}]", roleHint);
    } catch (XWikiException ex) {
        LOGGER.warn("Failed to retrieve document [{}]", documentReference);
    }
    return new CompositeBlock(transformedXDOM.getChildren());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) TransformationException(org.xwiki.rendering.transformation.TransformationException) XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) XWikiException(com.xpn.xwiki.XWikiException)

Example 5 with TransformationContext

use of org.xwiki.rendering.transformation.TransformationContext 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

TransformationContext (org.xwiki.rendering.transformation.TransformationContext)19 XDOM (org.xwiki.rendering.block.XDOM)16 MutableRenderingContext (org.xwiki.rendering.internal.transformation.MutableRenderingContext)8 Test (org.junit.Test)7 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)6 Transformation (org.xwiki.rendering.transformation.Transformation)6 StringReader (java.io.StringReader)5 Parser (org.xwiki.rendering.parser.Parser)5 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)5 HashMap (java.util.HashMap)4 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)4 TransformationException (org.xwiki.rendering.transformation.TransformationException)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)3 ContentParser (org.xwiki.rendering.parser.ContentParser)3 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)3 RenderingContext (org.xwiki.rendering.transformation.RenderingContext)3 TransformationManager (org.xwiki.rendering.transformation.TransformationManager)3 Block (org.xwiki.rendering.block.Block)2 CompositeBlock (org.xwiki.rendering.block.CompositeBlock)2 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)2