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