use of org.xwiki.rendering.transformation.TransformationContext in project xwiki-platform by xwiki.
the class DocumentContentDisplayer method display.
/**
* Displays the given document in the current execution context.
*
* @param document the document to display
* @param nameSpace the name-space to be used when performing the display transformations
* @param parameters the display parameters
* @return the result of displaying the given document
*/
protected XDOM display(DocumentModelBridge document, String nameSpace, DocumentDisplayerParameters parameters) {
// This is a clone of the cached content that can be safely modified.
XDOM content = getContent(document, parameters);
if (!parameters.isContentTransformed()) {
return content;
}
// Before executing the XDOM transformations make sure the references used by them (e.g. the 'reference'
// parameter of the Include macro) are resolved relative to the current document on the execution context.
content.getMetaData().addMetaData(MetaData.BASE, defaultEntityReferenceSerializer.serialize(documentAccessBridge.getCurrentDocumentReference()));
TransformationContext txContext = new TransformationContext(content, document.getSyntax(), parameters.isTransformationContextRestricted());
txContext.setId(nameSpace);
try {
transformationManager.performTransformations(content, txContext);
} catch (Exception e) {
throw new RuntimeException(e);
}
return content;
}
use of org.xwiki.rendering.transformation.TransformationContext in project xwiki-platform by xwiki.
the class DocumentTitleDisplayer method extractTitleFromContent.
@Override
protected XDOM extractTitleFromContent(DocumentModelBridge document, DocumentDisplayerParameters parameters) {
// Note: Ideally we should apply transformations on the document's returned XDOM here since macros could
// generate headings for example or some other transformations could modify headings. However we don't do this
// at the moment since it would be too costly to do so. In the future we will even probably remove the feature
// of generating the title from the content.
List<HeaderBlock> blocks = document.getXDOM().getBlocks(new ClassBlockMatcher(HeaderBlock.class), Block.Axes.DESCENDANT);
if (!blocks.isEmpty()) {
HeaderBlock heading = blocks.get(0);
// Check the heading depth after which we should return null if no heading was found.
if (heading.getLevel().getAsInt() <= displayConfiguration.getTitleHeadingDepth()) {
XDOM headingXDOM = new XDOM(Collections.<Block>singletonList(heading));
try {
TransformationContext txContext = new TransformationContext(headingXDOM, document.getSyntax(), parameters.isTransformationContextRestricted());
txContext.setTargetSyntax(parameters.getTargetSyntax());
transformationManager.performTransformations(headingXDOM, txContext);
Block headingBlock = headingXDOM.getChildren().size() > 0 ? headingXDOM.getChildren().get(0) : null;
if (headingBlock instanceof HeaderBlock) {
return new XDOM(headingBlock.getChildren());
}
} catch (TransformationException e) {
getLogger().warn("Failed to extract title from document content.");
}
}
}
return null;
}
use of org.xwiki.rendering.transformation.TransformationContext in project xwiki-platform by xwiki.
the class InternalTemplateManager method transform.
private void transform(Block block) {
TransformationContext txContext = new TransformationContext(block instanceof XDOM ? (XDOM) block : new XDOM(Arrays.asList(block)), this.renderingContext.getDefaultSyntax(), this.renderingContext.isRestricted());
txContext.setId(this.renderingContext.getTransformationId());
txContext.setTargetSyntax(getTargetSyntax());
try {
this.transformationManager.performTransformations(block, txContext);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.xwiki.rendering.transformation.TransformationContext 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();
}
use of org.xwiki.rendering.transformation.TransformationContext 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));
}
Aggregations