Search in sources :

Example 31 with WikiPrinter

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

Example 32 with WikiPrinter

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

the class LocalizationScriptServiceTest method setUp.

@Before
public void setUp() throws Exception {
    componentManager = mock(ComponentManager.class);
    Provider<ComponentManager> componentManagerProvider = mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
    when(componentManagerProvider.get()).thenReturn(componentManager);
    renderer = mock(BlockRenderer.class, Syntax.PLAIN_1_0.toIdString());
    when(componentManager.getInstance(BlockRenderer.class, Syntax.PLAIN_1_0.toIdString())).thenReturn(renderer);
    localizationContext = mocker.getInstance(LocalizationContext.class);
    localizationManager = mocker.getInstance(LocalizationManager.class);
    localizationScriptService = (LocalizationScriptService) mocker.getComponentUnderTest();
    translation = mock(Translation.class);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            WikiPrinter printer = (WikiPrinter) invocation.getArguments()[1];
            printer.print("print result");
            return null;
        }
    }).when(renderer).render(eq(new WordBlock("message")), any(WikiPrinter.class));
    when(translation.render(Locale.ROOT, ArrayUtils.EMPTY_OBJECT_ARRAY)).thenReturn(new WordBlock("message"));
    when(localizationManager.getTranslation("key", Locale.ROOT)).thenReturn(translation);
    when(localizationContext.getCurrentLocale()).thenReturn(Locale.ROOT);
    environment = mocker.getInstance(Environment.class);
}
Also used : Translation(org.xwiki.localization.Translation) LocalizationContext(org.xwiki.localization.LocalizationContext) WordBlock(org.xwiki.rendering.block.WordBlock) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) Provider(javax.inject.Provider) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) LocalizationManager(org.xwiki.localization.LocalizationManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ComponentManager(org.xwiki.component.manager.ComponentManager) Environment(org.xwiki.environment.Environment) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Before(org.junit.Before)

Example 33 with WikiPrinter

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

the class XWikiDocument method renderXDOM.

/**
 * Render privided XDOM into content of the provided syntax identifier.
 *
 * @param content the XDOM content to render
 * @param targetSyntax the syntax identifier of the rendered content
 * @return the rendered content
 * @throws XWikiException if an exception occurred during the rendering process
 */
protected static String renderXDOM(XDOM content, Syntax targetSyntax) throws XWikiException {
    try {
        BlockRenderer renderer = Utils.getComponent(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 document to syntax [" + targetSyntax + "]", e);
    }
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) XWikiException(com.xpn.xwiki.XWikiException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DifferentiationFailedException(org.suigeneris.jrcs.diff.DifferentiationFailedException) ExecutionContextException(org.xwiki.context.ExecutionContextException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MissingParserException(org.xwiki.rendering.parser.MissingParserException) IOException(java.io.IOException) ParseException(org.xwiki.rendering.parser.ParseException) TransformationException(org.xwiki.rendering.transformation.TransformationException) QueryException(org.xwiki.query.QueryException) 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