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())));
}
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);
}
}
Aggregations