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