Search in sources :

Example 16 with BlockRenderer

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

the class HeadingNameNamingCriterionTest method testDocumentNamesGeneration.

/**
 * Tests document names generated.
 *
 * @throws Exception
 */
@Test
public void testDocumentNamesGeneration() throws Exception {
    XDOM xdom = xwikiParser.parse(new StringReader("=Heading="));
    BlockRenderer plainSyntaxBlockRenderer = getComponentManager().getInstance(BlockRenderer.class, Syntax.PLAIN_1_0.toIdString());
    NamingCriterion namingCriterion = new HeadingNameNamingCriterion("Test.Test", docBridge, plainSyntaxBlockRenderer, false);
    Block sectionBlock = xdom.getChildren().get(0);
    // Test normal heading-name naming
    Assert.assertEquals("Test.Heading", namingCriterion.getDocumentName(new XDOM(sectionBlock.getChildren())));
    // Test name clash resolution
    Assert.assertEquals("Test.Heading-1", namingCriterion.getDocumentName(new XDOM(sectionBlock.getChildren())));
    // Test heading text cleaning (replacing)
    xdom = xwikiParser.parse(new StringReader("= This-Very.Weird:Heading! ="));
    sectionBlock = xdom.getChildren().get(0);
    Assert.assertEquals("Test.This-Very-Weird-Heading!", namingCriterion.getDocumentName(new XDOM(sectionBlock.getChildren())));
    // Test heading text cleaning (stripping)
    xdom = xwikiParser.parse(new StringReader("= This?Is@A/Very#Weird~Heading ="));
    sectionBlock = xdom.getChildren().get(0);
    Assert.assertEquals("Test.ThisIsAVeryWeirdHeading", namingCriterion.getDocumentName(new XDOM(sectionBlock.getChildren())));
    // Test page name truncation.
    xdom = xwikiParser.parse(new StringReader("=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa="));
    sectionBlock = xdom.getChildren().get(0);
    Assert.assertEquals(255, namingCriterion.getDocumentName(new XDOM(sectionBlock.getChildren())).length());
    // Test fallback operation
    Assert.assertEquals("Test.Test-1", namingCriterion.getDocumentName(xdom));
    // Test fallback operation under empty heading names
    xdom = xwikiParser.parse(new StringReader("=   ="));
    sectionBlock = xdom.getChildren().get(0);
    Assert.assertEquals("Test.Test-2", namingCriterion.getDocumentName(new XDOM(sectionBlock.getChildren())));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) Block(org.xwiki.rendering.block.Block) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) Test(org.junit.Test)

Example 17 with BlockRenderer

use of org.xwiki.rendering.renderer.BlockRenderer 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

BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)17 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)14 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)8 Test (org.junit.Test)7 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)6 XDOM (org.xwiki.rendering.block.XDOM)6 StringReader (java.io.StringReader)4 Block (org.xwiki.rendering.block.Block)4 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)4 Parser (org.xwiki.rendering.parser.Parser)3 XWikiException (com.xpn.xwiki.XWikiException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 MutableRenderingContext (org.xwiki.rendering.internal.transformation.MutableRenderingContext)2 ParseException (org.xwiki.rendering.parser.ParseException)2 StreamParser (org.xwiki.rendering.parser.StreamParser)2 RenderingContext (org.xwiki.rendering.transformation.RenderingContext)2 Transformation (org.xwiki.rendering.transformation.Transformation)2 BaseObject (com.xpn.xwiki.objects.BaseObject)1 IOException (java.io.IOException)1