Search in sources :

Example 11 with MutableRenderingContext

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

the class OfficeMacroImporter method render.

/**
 * Renders the given XDOM to the annotated XHTML syntax.
 *
 * @param xdom the XDOM to be rendered
 * @return the result of rendering the given XDOM to annotated XHTML syntax
 * @throws Exception if the rendering process fails
 */
public String render(XDOM xdom) throws Exception {
    TransformationContext txContext = new TransformationContext();
    txContext.setXDOM(xdom);
    ((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, txContext, xdom);
    WikiPrinter printer = new DefaultWikiPrinter();
    xhtmlRenderer.render(xdom, printer);
    return printer.toString();
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)

Example 12 with MutableRenderingContext

use of org.xwiki.rendering.internal.transformation.MutableRenderingContext 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 13 with MutableRenderingContext

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

the class DefaultWikiMacro method execute.

@Override
public List<Block> execute(WikiMacroParameters parameters, String macroContent, MacroTransformationContext context) throws MacroExecutionException {
    validate(parameters, macroContent);
    // Parse the wiki macro content.
    XDOM xdom = prepareWikiMacroContent(context);
    // Prepare macro context.
    Map<String, Object> macroBinding = new HashMap<>();
    macroBinding.put(MACRO_PARAMS_KEY, parameters);
    macroBinding.put(MACRO_CONTENT_KEY, macroContent);
    macroBinding.put(MACRO_DESCRIPTOR_KEY, getDescriptor());
    macroBinding.put(MACRO_CONTEXT_KEY, context);
    macroBinding.put(MACRO_RESULT_KEY, null);
    // Extension point to add more wiki macro bindings
    try {
        List<WikiMacroBindingInitializer> bindingInitializers = this.componentManager.getInstanceList(WikiMacroBindingInitializer.class);
        for (WikiMacroBindingInitializer bindingInitializer : bindingInitializers) {
            bindingInitializer.initialize(this.macroDocumentReference, parameters, macroContent, context, macroBinding);
        }
    } catch (ComponentLookupException e) {
    // TODO: we should probably log something but that should never happen
    }
    // Execute the macro
    ObservationManager observation = null;
    try {
        observation = this.componentManager.getInstance(ObservationManager.class);
    } catch (ComponentLookupException e) {
    // TODO: maybe log something
    }
    // Get XWiki context
    Map<String, Object> xwikiContext = null;
    try {
        Execution execution = this.componentManager.getInstance(Execution.class);
        ExecutionContext econtext = execution.getContext();
        if (econtext != null) {
            xwikiContext = (Map<String, Object>) execution.getContext().getProperty("xwikicontext");
        }
    } catch (ComponentLookupException e) {
    // TODO: maybe log something
    }
    try {
        Transformation macroTransformation = this.componentManager.getInstance(Transformation.class, MACRO_HINT);
        if (xwikiContext != null) {
            // Place macro context inside xwiki context ($xcontext.macro).
            xwikiContext.put(MACRO_KEY, macroBinding);
        }
        MacroBlock wikiMacroBlock = context.getCurrentMacroBlock();
        MacroMarkerBlock wikiMacroMarker = new MacroMarkerBlock(wikiMacroBlock.getId(), wikiMacroBlock.getParameters(), wikiMacroBlock.getContent(), xdom.getChildren(), wikiMacroBlock.isInline());
        // Make sure to use provided metadatas
        MetaDataBlock metaDataBlock = new MetaDataBlock(Collections.<Block>singletonList(wikiMacroMarker), xdom.getMetaData());
        // Make sure the context XDOM contains the wiki macro content
        wikiMacroBlock.getParent().replaceChild(metaDataBlock, wikiMacroBlock);
        // "Emulate" the fact that wiki macro block is still part of the XDOM (what is in the XDOM is a
        // MacroMarkerBlock and MacroTransformationContext current macro block only support MacroBlock so we can't
        // switch it without breaking some APIs)
        wikiMacroBlock.setParent(metaDataBlock.getParent());
        wikiMacroBlock.setNextSiblingBlock(metaDataBlock.getNextSibling());
        wikiMacroBlock.setPreviousSiblingBlock(metaDataBlock.getPreviousSibling());
        try {
            if (observation != null) {
                observation.notify(STARTEXECUTION_EVENT, this, macroBinding);
            }
            // Perform internal macro transformations.
            TransformationContext txContext = new TransformationContext(context.getXDOM(), this.syntax);
            txContext.setId(context.getId());
            RenderingContext renderingContext = componentManager.getInstance(RenderingContext.class);
            ((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, txContext, wikiMacroMarker);
        } finally {
            // Restore context XDOM to its previous state
            metaDataBlock.getParent().replaceChild(wikiMacroBlock, metaDataBlock);
        }
        return extractResult(wikiMacroMarker.getChildren(), macroBinding, context);
    } catch (Exception ex) {
        throw new MacroExecutionException("Error while performing internal macro transformations", ex);
    } finally {
        if (xwikiContext != null) {
            xwikiContext.remove(MACRO_KEY);
        }
        if (observation != null) {
            observation.notify(ENDEXECUTION_EVENT, this);
        }
    }
}
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) HashMap(java.util.HashMap) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ObservationManager(org.xwiki.observation.ObservationManager) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MacroParameterException(org.xwiki.rendering.macro.parameter.MacroParameterException) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) MacroBlock(org.xwiki.rendering.block.MacroBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 14 with MutableRenderingContext

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

the class PanelWikiUIExtension method execute.

@Override
public Block execute() {
    // We need to clone the xdom to avoid transforming the original and make it useless after the first
    // transformation
    final XDOM transformedXDOM = this.xdom.clone();
    this.progress.startStep(getDocumentReference(), "panel.progress.execute", "Execute panel [{}]", getDocumentReference());
    // Perform panel transformations with the right of the panel author
    try {
        this.authorExecutor.call(() -> {
            TransformationContext transformationContext = new TransformationContext(transformedXDOM, syntax);
            transformationContext.setId(getRoleHint());
            ((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, transformationContext, transformedXDOM);
            return null;
        }, getAuthorReference());
    } catch (Exception e) {
        LOGGER.error("Error while executing transformation for panel [{}]", this.panelReference);
    } finally {
        this.progress.endStep(getDocumentReference());
    }
    return new CompositeBlock(transformedXDOM.getChildren());
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 15 with MutableRenderingContext

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

the class PageTest method tearDown.

/**
 * Clean up after the test.
 *
 * @throws Exception in case of errors
 */
@After
public void tearDown() throws Exception {
    MutableRenderingContext renderingContext = mocker.getInstance(RenderingContext.class);
    renderingContext.pop();
}
Also used : MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) After(org.junit.After)

Aggregations

MutableRenderingContext (org.xwiki.rendering.internal.transformation.MutableRenderingContext)15 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)8 XDOM (org.xwiki.rendering.block.XDOM)6 RenderingContext (org.xwiki.rendering.transformation.RenderingContext)4 StringReader (java.io.StringReader)3 HashMap (java.util.HashMap)3 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)3 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)3 Transformation (org.xwiki.rendering.transformation.Transformation)3 XWikiException (com.xpn.xwiki.XWikiException)2 Test (org.junit.Test)2 CompositeBlock (org.xwiki.rendering.block.CompositeBlock)2 Parser (org.xwiki.rendering.parser.Parser)2 StreamParser (org.xwiki.rendering.parser.StreamParser)2 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)2 TransformationException (org.xwiki.rendering.transformation.TransformationException)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 WikiSkin (com.xpn.xwiki.internal.skin.WikiSkin)1 ExternalServletURLFactory (com.xpn.xwiki.web.ExternalServletURLFactory)1