use of org.xwiki.rendering.parser.Parser in project xwiki-platform by xwiki.
the class DocumentTitleDisplayerTest method whenSettingTheContextDocumentTheContextWikiIsAlsoSet.
@Test
public void whenSettingTheContextDocumentTheContextWikiIsAlsoSet() throws Exception {
EntityReferenceProvider defaultEntityReferenceProvider = this.mocker.getInstance(EntityReferenceProvider.class);
when(defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(new EntityReference("Page", EntityType.DOCUMENT));
DocumentModelBridge document = mock(DocumentModelBridge.class);
DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Space"), "Page");
when(document.getDocumentReference()).thenReturn(documentReference);
when(document.getTitle()).thenReturn("title");
XDOM titleXDOM = new XDOM(Arrays.asList(new WordBlock("title")));
Parser plainTextParser = this.mocker.getInstance(Parser.class, "plain/1.0");
when(plainTextParser.parse(any(StringReader.class))).thenReturn(titleXDOM);
ModelContext modelContext = this.mocker.getInstance(ModelContext.class);
WikiReference currentWikiReference = new WikiReference("currentWiki");
when(modelContext.getCurrentEntityReference()).thenReturn(currentWikiReference);
AuthorizationManager authorizationManager = this.mocker.getInstance(AuthorizationManager.class);
when(authorizationManager.hasAccess(eq(Right.SCRIPT), any(), any())).thenReturn(true);
DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
DocumentDisplayerParameters params = new DocumentDisplayerParameters();
params.setTitleDisplayed(true);
params.setExecutionContextIsolated(true);
this.mocker.getComponentUnderTest().display(document, params);
// Check that the context is set.
verify(dab).pushDocumentInContext(any(), eq(documentReference));
verify(modelContext).setCurrentEntityReference(documentReference.getWikiReference());
// Check that the context is restored.
verify(dab).popDocumentFromContext(any());
verify(modelContext).setCurrentEntityReference(currentWikiReference);
}
use of org.xwiki.rendering.parser.Parser in project xwiki-platform by xwiki.
the class DefaultExtendedRenderingConfigurationTest method getConfiguredAndDisabledSyntaxesWhenNoConfigXObjectAndNoXWikiCfgProperty.
@Test
public void getConfiguredAndDisabledSyntaxesWhenNoConfigXObjectAndNoXWikiCfgProperty() throws Exception {
ConfigurationSource renderingSource = this.mocker.getInstance(ConfigurationSource.class, "rendering");
when(renderingSource.getProperty("disabledSyntaxes")).thenReturn(null);
ConfigurationSource xwikiCfgSource = this.mocker.getInstance(ConfigurationSource.class, "xwikicfg");
when(xwikiCfgSource.getProperty("xwiki.rendering.syntaxes", List.class)).thenReturn(null);
CoreConfiguration coreConfiguration = this.mocker.getInstance(CoreConfiguration.class);
Syntax defaultSyntax = new Syntax(new SyntaxType("xwiki", "XWiki"), "2.1");
when(coreConfiguration.getDefaultDocumentSyntax()).thenReturn(defaultSyntax);
// Register some Syntaxes for the test
Parser defaultSyntaxParser = this.mocker.registerMockComponent(Parser.class, "xwiki/2.1");
when(defaultSyntaxParser.getSyntax()).thenReturn(defaultSyntax);
Parser syntax1Parser = this.mocker.registerMockComponent(Parser.class, "syntax1/1.0");
Syntax syntax1 = new Syntax(new SyntaxType("syntax1", "Syntax 1"), "1.0");
when(syntax1Parser.getSyntax()).thenReturn(syntax1);
Parser syntax2Parser = this.mocker.registerMockComponent(Parser.class, "syntax2/1.0");
Syntax syntax2 = new Syntax(new SyntaxType("syntax2", "Syntax 2"), "1.0");
when(syntax2Parser.getSyntax()).thenReturn(syntax2);
List<Syntax> disabledSyntaxes = this.mocker.getComponentUnderTest().getDisabledSyntaxes();
assertEquals(2, disabledSyntaxes.size());
assertTrue(disabledSyntaxes.contains(syntax1));
assertTrue(disabledSyntaxes.contains(syntax2));
List<Syntax> configuredSyntaxes = this.mocker.getComponentUnderTest().getConfiguredSyntaxes();
assertEquals(1, configuredSyntaxes.size());
assertTrue(configuredSyntaxes.contains(defaultSyntax));
}
use of org.xwiki.rendering.parser.Parser 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);
}
}
use of org.xwiki.rendering.parser.Parser in project xwiki-platform by xwiki.
the class DefaultHTMLConverterTest method toHTML.
/**
* Unit test for {@link DefaultHTMLConverter#toHTML(String, String)}.
*/
@Test
public void toHTML() throws Exception {
String source = "wiki syntax";
String syntaxId = "syntax/x.y";
// The source should be parsed.
Parser parser = this.mocker.registerMockComponent(Parser.class, syntaxId);
XDOM xdom = new XDOM(Collections.<Block>emptyList());
when(parser.parse(any(StringReader.class))).thenReturn(xdom);
Assert.assertEquals("", mocker.getComponentUnderTest().toHTML(source, 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));
}
use of org.xwiki.rendering.parser.Parser 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());
}
Aggregations