Search in sources :

Example 11 with TransformationContext

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;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) TransformationContext(org.xwiki.rendering.transformation.TransformationContext)

Example 12 with TransformationContext

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;
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) TransformationException(org.xwiki.rendering.transformation.TransformationException) XDOM(org.xwiki.rendering.block.XDOM) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) Block(org.xwiki.rendering.block.Block) TransformationContext(org.xwiki.rendering.transformation.TransformationContext)

Example 13 with TransformationContext

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);
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) PropertyException(org.xwiki.properties.PropertyException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 14 with TransformationContext

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();
}
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 15 with TransformationContext

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

Aggregations

TransformationContext (org.xwiki.rendering.transformation.TransformationContext)19 XDOM (org.xwiki.rendering.block.XDOM)16 MutableRenderingContext (org.xwiki.rendering.internal.transformation.MutableRenderingContext)8 Test (org.junit.Test)7 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)6 Transformation (org.xwiki.rendering.transformation.Transformation)6 StringReader (java.io.StringReader)5 Parser (org.xwiki.rendering.parser.Parser)5 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)5 HashMap (java.util.HashMap)4 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)4 TransformationException (org.xwiki.rendering.transformation.TransformationException)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)3 ContentParser (org.xwiki.rendering.parser.ContentParser)3 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)3 RenderingContext (org.xwiki.rendering.transformation.RenderingContext)3 TransformationManager (org.xwiki.rendering.transformation.TransformationManager)3 Block (org.xwiki.rendering.block.Block)2 CompositeBlock (org.xwiki.rendering.block.CompositeBlock)2 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)2