Search in sources :

Example 1 with TransformationManager

use of org.xwiki.rendering.transformation.TransformationManager 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;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) TransformationManager(org.xwiki.rendering.transformation.TransformationManager) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) Parser(org.xwiki.rendering.parser.Parser)

Example 2 with TransformationManager

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

the class AbstractAnnotationMaintainer method renderPlainText.

/**
 * Helper method to render the plain text version of the passed content.
 *
 * @param content the content to render in plain text
 * @param syntaxId the source syntax of the content to render
 * @throws Exception if anything goes wrong while rendering the content
 * @return the normalized plain text rendered content
 */
private String renderPlainText(String content, String syntaxId) throws Exception {
    PrintRenderer renderer = componentManager.getInstance(PrintRenderer.class, "normalizer-plain/1.0");
    // parse
    Parser parser = componentManager.getInstance(Parser.class, syntaxId);
    XDOM xdom = parser.parse(new StringReader(content));
    // run transformations -> although it's going to be at least strange to handle rendered content since there
    // is no context
    Syntax sourceSyntax = Syntax.valueOf(syntaxId);
    TransformationManager transformationManager = componentManager.getInstance(TransformationManager.class);
    transformationManager.performTransformations(xdom, sourceSyntax);
    // render
    WikiPrinter printer = new DefaultWikiPrinter();
    renderer.setPrinter(printer);
    xdom.traverse(renderer);
    return printer.toString();
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) PrintRenderer(org.xwiki.rendering.renderer.PrintRenderer) StringReader(java.io.StringReader) TransformationManager(org.xwiki.rendering.transformation.TransformationManager) Syntax(org.xwiki.rendering.syntax.Syntax) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) Parser(org.xwiki.rendering.parser.Parser)

Example 3 with TransformationManager

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

the class DocumentContentDisplayerTest method testBaseMetaDataIsSetBeforeExecutingTransformations.

@Test
public void testBaseMetaDataIsSetBeforeExecutingTransformations() throws Exception {
    // The execution context is expected to have the "xwikicontext" property set.
    Execution mockExecution = mocker.getInstance(Execution.class);
    ExecutionContext executionContext = new ExecutionContext();
    executionContext.setProperty("xwikicontext", new HashMap<String, Object>());
    Mockito.when(mockExecution.getContext()).thenReturn(executionContext);
    // The document being displayed.
    DocumentModelBridge mockDocument = Mockito.mock(DocumentModelBridge.class);
    XDOM content = new XDOM(Collections.<Block>emptyList());
    Mockito.when(mockDocument.getXDOM()).thenReturn(content);
    // The reference of the current document musts be set as the value of the BASE meta data.
    DocumentReference currentDocRef = new DocumentReference("wiki", "Space", "Page");
    DocumentAccessBridge mockDocumentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
    Mockito.when(mockDocumentAccessBridge.getCurrentDocumentReference()).thenReturn(currentDocRef);
    EntityReferenceSerializer<String> serializer = mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
    Mockito.when(serializer.serialize(currentDocRef)).thenReturn("foo");
    // We can't verify the meta data after the display method is called because we want to make sure the BASE meta
    // data is correctly set before XDOM transformations are executed, not after.
    TransformationManager mockTransformationManager = mocker.getInstance(TransformationManager.class);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            XDOM xdom = (XDOM) invocation.getArguments()[0];
            // We have to assert the meta data before the transformations are executed not at the end!
            Assert.assertEquals("foo", xdom.getMetaData().getMetaData(MetaData.BASE));
            return null;
        }
    }).when(mockTransformationManager).performTransformations(Mockito.any(XDOM.class), Mockito.any(TransformationContext.class));
    // Execute the display.
    Assert.assertSame(content, mocker.getComponentUnderTest().display(mockDocument, new DocumentDisplayerParameters()));
    // Make sure the transformations are executed exactly once, and on the right content.
    Mockito.verify(mockTransformationManager, Mockito.times(1)).performTransformations(Mockito.same(content), Mockito.any(TransformationContext.class));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TransformationManager(org.xwiki.rendering.transformation.TransformationManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 4 with TransformationManager

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

the class AnnotationXHTMLRendererTest method getAnnotatedHTML.

/**
 * Test rendering the annotations in the document description file results in the annotated html.
 *
 * @throws Exception in case something goes wrong looking up components and rendering
 */
@Test
public void getAnnotatedHTML() throws Exception {
    Parser parser = getComponentManager().getInstance(Parser.class, docFactory.getDocument(docName).getSyntax());
    XDOM xdom = parser.parse(new StringReader(docFactory.getDocument(docName).getSource()));
    // run transformations
    TransformationManager transformationManager = getComponentManager().getInstance(TransformationManager.class);
    TransformationContext context = new TransformationContext(xdom, Syntax.valueOf(docFactory.getDocument(docName).getSyntax()));
    context.setTargetSyntax(Syntax.ANNOTATED_XHTML_1_0);
    transformationManager.performTransformations(xdom, context);
    AnnotationPrintRenderer renderer = getComponentManager().getInstance(AnnotationPrintRenderer.class, ANNOTATIONS_RENDERER_HINT);
    WikiPrinter printer = new DefaultWikiPrinter();
    renderer.setPrinter(printer);
    // set the annotations for this renderer
    renderer.setAnnotations(docFactory.getDocument(docName).getAnnotations());
    xdom.traverse(renderer);
    assertEquals("[" + docName + "] test failed", docFactory.getDocument(docName).getAnnotatedContent(), printer.toString());
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) TransformationManager(org.xwiki.rendering.transformation.TransformationManager) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) Parser(org.xwiki.rendering.parser.Parser) Test(org.junit.Test)

Example 5 with TransformationManager

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

the class AnnotationXHTMLRendererTest method getAnnotatedHTMLWithoutAnnotations.

/**
 * Test rendering with the annotations renderer but without annotations doesn't alter the content.
 *
 * @throws Exception in case something goes wrong looking up components and rendering
 */
@Test
public void getAnnotatedHTMLWithoutAnnotations() throws Exception {
    Parser parser = getComponentManager().getInstance(Parser.class, docFactory.getDocument(docName).getSyntax());
    XDOM xdom = parser.parse(new StringReader(docFactory.getDocument(docName).getSource()));
    // run transformations
    TransformationManager transformationManager = getComponentManager().getInstance(TransformationManager.class);
    TransformationContext context = new TransformationContext(xdom, Syntax.valueOf(docFactory.getDocument(docName).getSyntax()));
    context.setTargetSyntax(Syntax.ANNOTATED_XHTML_1_0);
    transformationManager.performTransformations(xdom, context);
    AnnotationPrintRenderer renderer = getComponentManager().getInstance(AnnotationPrintRenderer.class, ANNOTATIONS_RENDERER_HINT);
    WikiPrinter printer = new DefaultWikiPrinter();
    renderer.setPrinter(printer);
    xdom.traverse(renderer);
    assertEquals("[" + docName + "] test failed", docFactory.getDocument(docName).getRenderedContent(), printer.toString());
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) TransformationManager(org.xwiki.rendering.transformation.TransformationManager) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) Parser(org.xwiki.rendering.parser.Parser) Test(org.junit.Test)

Aggregations

XDOM (org.xwiki.rendering.block.XDOM)6 TransformationManager (org.xwiki.rendering.transformation.TransformationManager)6 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)5 StringReader (java.io.StringReader)4 Test (org.junit.Test)4 Parser (org.xwiki.rendering.parser.Parser)4 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)4 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)3 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)2 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)2 Execution (org.xwiki.context.Execution)2 ExecutionContext (org.xwiki.context.ExecutionContext)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 AttachmentReference (org.xwiki.model.reference.AttachmentReference)1 OfficeViewer (org.xwiki.office.viewer.OfficeViewer)1 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)1 PrintRenderer (org.xwiki.rendering.renderer.PrintRenderer)1 Syntax (org.xwiki.rendering.syntax.Syntax)1