Search in sources :

Example 21 with WikiPrinter

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

the class InternalTemplateManager method render.

private void render(XDOM xdom, Writer writer) {
    WikiPrinter printer = new WriterWikiPrinter(writer);
    BlockRenderer blockRenderer;
    try {
        blockRenderer = this.componentManagerProvider.get().getInstance(BlockRenderer.class, getTargetSyntax().toIdString());
    } catch (ComponentLookupException e) {
        blockRenderer = this.plainRenderer;
    }
    blockRenderer.render(xdom, printer);
}
Also used : WriterWikiPrinter(org.xwiki.rendering.renderer.printer.WriterWikiPrinter) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) WriterWikiPrinter(org.xwiki.rendering.renderer.printer.WriterWikiPrinter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 22 with WikiPrinter

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

the class DefaultHTMLConverter method parseAndRender.

@Override
public String parseAndRender(String dirtyHTML, String syntaxId) {
    try {
        // Clean
        String html = this.htmlCleaner.clean(dirtyHTML);
        // Parse
        XDOM xdom = this.xhtmlParser.parse(new StringReader(html));
        // The XHTML parser sets the "syntax" meta data property of the created XDOM to "xhtml/1.0". The syntax meta
        // data is used as the default syntax for macro content. We have to change this to the specified syntax
        // because HTML is used only to be able to edit the source syntax in the WYSIWYG editor.
        Syntax syntax = Syntax.valueOf(syntaxId);
        xdom.getMetaData().addMetaData(MetaData.SYNTAX, syntax);
        // Execute the macro transformation
        executeMacroTransformation(xdom, Syntax.valueOf(syntaxId));
        // Render
        WikiPrinter printer = new DefaultWikiPrinter();
        this.xhtmlRenderer.render(xdom, printer);
        return printer.toString();
    } catch (Exception e) {
        this.logger.error(e.getLocalizedMessage(), e);
        throw new RuntimeException("Exception while refreshing HTML", e);
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Syntax(org.xwiki.rendering.syntax.Syntax) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) ParseException(org.xwiki.rendering.parser.ParseException) TransformationException(org.xwiki.rendering.transformation.TransformationException)

Example 23 with WikiPrinter

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

the class DefaultHTMLConverter method fromHTML.

@Override
public String fromHTML(String dirtyHTML, String syntaxId) {
    try {
        // Clean
        String html = this.htmlCleaner.clean(dirtyHTML);
        // Parse & Render
        // Note that transformations are not executed when converting XHTML to source syntax.
        WikiPrinter printer = new DefaultWikiPrinter();
        PrintRendererFactory printRendererFactory = this.contextComponentManager.getInstance(PrintRendererFactory.class, syntaxId);
        this.xhtmlStreamParser.parse(new StringReader(html), printRendererFactory.createRenderer(printer));
        return printer.toString();
    } catch (Exception e) {
        this.logger.error(e.getLocalizedMessage(), e);
        throw new RuntimeException("Exception while parsing HTML", e);
    }
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) PrintRendererFactory(org.xwiki.rendering.renderer.PrintRendererFactory) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) ParseException(org.xwiki.rendering.parser.ParseException) TransformationException(org.xwiki.rendering.transformation.TransformationException)

Example 24 with WikiPrinter

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

the class DefaultHTMLConverter method toHTML.

@Override
public String toHTML(String source, String syntaxId) {
    try {
        // Parse
        Parser parser = this.contextComponentManager.getInstance(Parser.class, syntaxId);
        XDOM xdom = parser.parse(new StringReader(source));
        // Execute the macro transformation
        executeMacroTransformation(xdom, Syntax.valueOf(syntaxId));
        // Render
        WikiPrinter printer = new DefaultWikiPrinter();
        this.xhtmlRenderer.render(xdom, printer);
        return printer.toString();
    } catch (Exception e) {
        this.logger.error(e.getLocalizedMessage(), e);
        throw new RuntimeException("Exception while rendering HTML", e);
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) ParseException(org.xwiki.rendering.parser.ParseException) TransformationException(org.xwiki.rendering.transformation.TransformationException) Parser(org.xwiki.rendering.parser.Parser) StreamParser(org.xwiki.rendering.parser.StreamParser)

Example 25 with WikiPrinter

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

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