Search in sources :

Example 1 with MutableRenderingContext

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

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

the class XWiki method parseTemplate.

/**
 * @param template the name of the template
 * @param skinId the id of the skin from which to load the template
 * @param context see {@link XWikiContext}
 * @deprecated since 7.0M1, use {@link TemplateManager#renderFromSkin(String, Skin)} instead
 */
@Deprecated
public String parseTemplate(String template, String skinId, XWikiContext context) {
    MutableRenderingContext mutableRenderingContext = getMutableRenderingContext();
    Syntax currentTargetSyntax = mutableRenderingContext.getTargetSyntax();
    try {
        // Force rendering with XHTML 1.0 syntax for retro-compatibility
        mutableRenderingContext.setTargetSyntax(Syntax.XHTML_1_0);
        Skin skin = getInternalSkinManager().getSkin(skinId);
        return getTemplateManager().renderFromSkin(template, skin);
    } catch (Exception e) {
        LOGGER.error("Error while evaluating velocity template [{}] skin [{}]", template, skinId, e);
        Object[] args = { template, skinId };
        XWikiException xe = new XWikiException(XWikiException.MODULE_XWIKI_RENDERING, XWikiException.ERROR_XWIKI_RENDERING_VELOCITY_EXCEPTION, "Error while evaluating velocity template [{0}] from skin [{1}]", e, args);
        return Util.getHTMLExceptionMessage(xe, context);
    } finally {
        mutableRenderingContext.setTargetSyntax(currentTargetSyntax);
    }
}
Also used : Skin(org.xwiki.skin.Skin) WikiSkin(com.xpn.xwiki.internal.skin.WikiSkin) Syntax(org.xwiki.rendering.syntax.Syntax) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) IOException(java.io.IOException) JobException(org.xwiki.job.JobException) ParseException(org.xwiki.rendering.parser.ParseException) QueryException(org.xwiki.query.QueryException) URIException(org.apache.commons.httpclient.URIException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NamingException(javax.naming.NamingException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException)

Example 3 with MutableRenderingContext

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

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

Example 5 with MutableRenderingContext

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

the class PageTest method setOutputSyntax.

/**
 * Sets the Syntax with which the Document to test will be rendered into. If not called, the Document will be
 * rendered as XHTML.
 *
 * @param syntax the Syntax to render the Document into
 * @throws Exception in case of errors
 */
protected void setOutputSyntax(Syntax syntax) throws Exception {
    MutableRenderingContext renderingContext = mocker.getInstance(RenderingContext.class);
    renderingContext.push(renderingContext.getTransformation(), renderingContext.getXDOM(), renderingContext.getDefaultSyntax(), "test", renderingContext.isRestricted(), syntax);
}
Also used : MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext)

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