use of org.xwiki.rendering.renderer.BlockRenderer in project xwiki-platform by xwiki.
the class XDOMOfficeDocument method getContentAsString.
/**
* Renders the XDOM encapsulated by this document into the given syntax.
*
* @param syntaxId string identifier of the syntax.
* @return content of this document in the given syntax or null if the syntax is invalid.
*/
public String getContentAsString(String syntaxId) {
try {
WikiPrinter printer = new DefaultWikiPrinter();
BlockRenderer renderer = this.componentManager.getInstance(BlockRenderer.class, syntaxId);
renderer.render(this.xdom, printer);
return printer.toString();
} catch (ComponentLookupException ex) {
// Nothing to do here.
}
return null;
}
use of org.xwiki.rendering.renderer.BlockRenderer in project xwiki-platform by xwiki.
the class DefaultPresentationBuilder method buildPresentationXDOM.
/**
* Parses the given HTML text into an XDOM tree.
*
* @param html the HTML text to parse
* @param targetDocumentReference specifies the document where the presentation will be imported; we use the target
* document reference to get the syntax of the target document and to set the {@code BASE} meta data on
* the created XDOM
* @return a XDOM tree
* @throws OfficeImporterException if parsing the given HTML fails
*/
protected XDOM buildPresentationXDOM(String html, DocumentReference targetDocumentReference) throws OfficeImporterException {
try {
ComponentManager contextComponentManager = this.contextComponentManagerProvider.get();
String syntaxId = this.documentAccessBridge.getTranslatedDocumentInstance(targetDocumentReference).getSyntax().toIdString();
BlockRenderer renderer = contextComponentManager.getInstance(BlockRenderer.class, syntaxId);
Map<String, String> galleryParameters = Collections.emptyMap();
ExpandedMacroBlock gallery = new ExpandedMacroBlock("gallery", galleryParameters, renderer, false, contextComponentManager);
gallery.addChild(this.xhtmlParser.parse(new StringReader(html)));
XDOM xdom = new XDOM(Collections.singletonList((Block) gallery));
// Make sure (image) references are resolved relative to the target document reference.
xdom.getMetaData().addMetaData(MetaData.BASE, entityReferenceSerializer.serialize(targetDocumentReference));
return xdom;
} catch (Exception e) {
throw new OfficeImporterException("Failed to build presentation XDOM.", e);
}
}
use of org.xwiki.rendering.renderer.BlockRenderer in project xwiki-platform by xwiki.
the class RenderingScriptServiceTest method parseAndRender.
@Test
public void parseAndRender() throws Exception {
Parser parser = this.mocker.registerMockComponent(Parser.class, "plain/1.0");
when(parser.parse(argThat(new StringReaderMatcher("some [[TODO]] stuff")))).thenReturn(new XDOM(Collections.<Block>emptyList()));
BlockRenderer blockRenderer = this.mocker.registerMockComponent(BlockRenderer.class, "xwiki/2.0");
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
WikiPrinter printer = (WikiPrinter) invocationOnMock.getArguments()[1];
printer.print("some ~[~[TODO]] stuff");
return null;
}
}).when(blockRenderer).render(any(XDOM.class), any());
XDOM xdom = this.mocker.getComponentUnderTest().parse("some [[TODO]] stuff", "plain/1.0");
assertEquals("some ~[~[TODO]] stuff", this.mocker.getComponentUnderTest().render(xdom, "xwiki/2.0"));
}
use of org.xwiki.rendering.renderer.BlockRenderer in project xwiki-platform by xwiki.
the class DefaultOfficeViewerScriptService method render.
/**
* Renders the given XDOM into specified syntax.
*
* @param xdom the {@link XDOM} to be rendered
* @param fromSyntax the syntax for which to perform the transformations
* @param toSyntax expected output syntax
* @return string holding the result of rendering
* @throws Exception if an error occurs during rendering
*/
private String render(XDOM xdom, Syntax fromSyntax, Syntax toSyntax) throws Exception {
// Perform the transformations. This is required for office presentations which use the gallery macro to display
// the slide images.
TransformationContext context = new TransformationContext(xdom, fromSyntax);
context.setTargetSyntax(toSyntax);
this.transformationManager.performTransformations(xdom, context);
WikiPrinter printer = new DefaultWikiPrinter();
BlockRenderer renderer = this.componentManager.getInstance(BlockRenderer.class, toSyntax.toIdString());
renderer.render(xdom, printer);
return printer.toString();
}
use of org.xwiki.rendering.renderer.BlockRenderer in project xwiki-platform by xwiki.
the class DefaultHTMLConverterTest method parseAndRender.
/**
* Unit test for {@link DefaultHTMLConverter#parseAndRender(String, String)}.
*/
@Test
public void parseAndRender() throws Exception {
String html = "some HTML";
String syntaxId = "syntax/x.y";
// Verify the HTML is cleaned.
HTMLCleaner cleaner = mocker.getInstance(HTMLCleaner.class);
when(cleaner.clean(html)).thenReturn(html);
// Verify the HTML is parsed into XDOM.
XDOM xdom = new XDOM(Collections.<Block>emptyList());
Parser xhtmlParser = mocker.getInstance(Parser.class, "xhtml/1.0");
when(xhtmlParser.parse(any(StringReader.class))).thenReturn(xdom);
Assert.assertEquals("", mocker.getComponentUnderTest().parseAndRender(html, syntaxId));
// Verify that the macro transformations have been executed.
Transformation macroTransformation = mocker.getInstance(Transformation.class, "macro");
RenderingContext renderingContext = mocker.getInstance(RenderingContext.class);
// It's very important to verify that a transformation context id is set as otherwise if the content being
// edited has different velocity macros executing, they'll be executed in isolation and thus what's defined in
// one won't be visible from the other ones (For example see https://jira.xwiki.org/browse/XWIKI-11695).
ArgumentCaptor<TransformationContext> txContextArgument = ArgumentCaptor.forClass(TransformationContext.class);
verify((MutableRenderingContext) renderingContext).transformInContext(same(macroTransformation), txContextArgument.capture(), same(xdom));
assertEquals("wysiwygtxid", txContextArgument.getValue().getId());
// Verify the XDOM is rendered to Annotated XHTML.
BlockRenderer xhtmlRenderer = mocker.getInstance(BlockRenderer.class, "annotatedxhtml/1.0");
verify(xhtmlRenderer).render(same(xdom), any(WikiPrinter.class));
// Verify that the syntax meta data has been set.
Assert.assertEquals(Syntax.valueOf("syntax/x.y"), xdom.getMetaData().getMetaData(MetaData.SYNTAX));
}
Aggregations