Search in sources :

Example 26 with DefaultWikiPrinter

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

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

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

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

Example 30 with DefaultWikiPrinter

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

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