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);
}
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);
}
}
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());
}
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));
}
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);
}
Aggregations