Search in sources :

Example 26 with WikiPrinter

use of org.xwiki.rendering.renderer.printer.WikiPrinter 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 27 with WikiPrinter

use of org.xwiki.rendering.renderer.printer.WikiPrinter 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)

Example 28 with WikiPrinter

use of org.xwiki.rendering.renderer.printer.WikiPrinter in project xwiki-platform by xwiki.

the class DefaultAnnotationService method getAnnotatedRenderedContent.

@Override
public String getAnnotatedRenderedContent(String sourceReference, String sourceSyntax, String outputSyntax, Collection<Annotation> annotations) throws AnnotationServiceException {
    try {
        XDOM xdom = targetIoService.getXDOM(sourceReference, sourceSyntax);
        // build the annotations renderer hint for the specified output syntax
        String outputSyntaxId = "annotations-" + outputSyntax;
        AnnotationPrintRenderer annotationsRenderer = componentManager.getInstance(AnnotationPrintRenderer.class, outputSyntaxId);
        WikiPrinter printer = new DefaultWikiPrinter();
        annotationsRenderer.setPrinter(printer);
        // set the annotations for this renderer
        annotationsRenderer.setAnnotations(annotations);
        xdom.traverse(annotationsRenderer);
        return printer.toString();
    } catch (Exception exc) {
        throw new AnnotationServiceException(exc);
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) AnnotationPrintRenderer(org.xwiki.annotation.renderer.AnnotationPrintRenderer) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) AnnotationServiceException(org.xwiki.annotation.AnnotationServiceException) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) AnnotationServiceException(org.xwiki.annotation.AnnotationServiceException) IOServiceException(org.xwiki.annotation.io.IOServiceException)

Example 29 with WikiPrinter

use of org.xwiki.rendering.renderer.printer.WikiPrinter in project xwiki-platform by xwiki.

the class XDOMOfficeDocument method renderTitle.

/**
 * Utility method for rendering a title.
 *
 * @param header header block which contains the title.
 * @return header block content rendered as a string.
 */
private String renderTitle(HeaderBlock header) {
    try {
        WikiPrinter printer = new DefaultWikiPrinter();
        BlockRenderer renderer = this.componentManager.getInstance(BlockRenderer.class, "plain/1.0");
        renderer.render(header, printer);
        return printer.toString();
    } catch (ComponentLookupException ex) {
    // Ignore.
    }
    return null;
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 30 with WikiPrinter

use of org.xwiki.rendering.renderer.printer.WikiPrinter in project xwiki-platform by xwiki.

the class DisplayScriptService method renderXDOM.

/**
 * Renders the provided XDOM.
 *
 * @param content the XDOM content to render
 * @param targetSyntax the syntax of the rendering result
 * @return the result of rendering the given XDOM
 * @throws XWikiException if an exception occurred during the rendering process
 */
private String renderXDOM(XDOM content, Syntax targetSyntax) throws XWikiException {
    try {
        BlockRenderer renderer = this.componentManager.getInstance(BlockRenderer.class, targetSyntax.toIdString());
        WikiPrinter printer = new DefaultWikiPrinter();
        renderer.render(content, printer);
        return printer.toString();
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_RENDERING, XWikiException.ERROR_XWIKI_UNKNOWN, "Failed to render XDOM to syntax [" + targetSyntax + "]", e);
    }
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) XWikiException(com.xpn.xwiki.XWikiException) XWikiException(com.xpn.xwiki.XWikiException) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Aggregations

WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)33 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)28 XDOM (org.xwiki.rendering.block.XDOM)12 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)11 StringReader (java.io.StringReader)8 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)6 Parser (org.xwiki.rendering.parser.Parser)6 Test (org.junit.Test)5 Block (org.xwiki.rendering.block.Block)5 ParseException (org.xwiki.rendering.parser.ParseException)5 TransformationException (org.xwiki.rendering.transformation.TransformationException)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)3 XWikiException (com.xpn.xwiki.XWikiException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 WordBlock (org.xwiki.rendering.block.WordBlock)2 Converter (org.xwiki.rendering.converter.Converter)2 TransformationManager (org.xwiki.rendering.transformation.TransformationManager)2