Search in sources :

Example 31 with DefaultWikiPrinter

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

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

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

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

Example 35 with DefaultWikiPrinter

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

the class RenderingScriptService method render.

/**
 * Render a list of Blocks into the passed syntax.
 *
 * @param block the block to render
 * @param outputSyntaxId the syntax in which to render the blocks
 * @return the string representing the passed blocks in the passed syntax or null if an error occurred
 * @since 3.2M3
 */
public String render(Block block, String outputSyntaxId) {
    String result;
    WikiPrinter printer = new DefaultWikiPrinter();
    try {
        BlockRenderer renderer = this.componentManagerProvider.get().getInstance(BlockRenderer.class, outputSyntaxId);
        renderer.render(block, printer);
        result = printer.toString();
    } catch (Exception e) {
        result = null;
    }
    return result;
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) ParseException(org.xwiki.rendering.parser.ParseException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MacroLookupException(org.xwiki.rendering.macro.MacroLookupException) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Aggregations

DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)41 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)28 StringReader (java.io.StringReader)18 Test (org.junit.Test)12 Converter (org.xwiki.rendering.converter.Converter)12 XDOM (org.xwiki.rendering.block.XDOM)10 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)8 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)7 Block (org.xwiki.rendering.block.Block)6 ParseException (org.xwiki.rendering.parser.ParseException)5 Parser (org.xwiki.rendering.parser.Parser)5 TransformationException (org.xwiki.rendering.transformation.TransformationException)4 XWikiException (com.xpn.xwiki.XWikiException)3 HashMap (java.util.HashMap)3 Expectations (org.jmock.Expectations)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 Reader (java.io.Reader)2