Search in sources :

Example 16 with Parser

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);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) WordBlock(org.xwiki.rendering.block.WordBlock) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Parser(org.xwiki.rendering.parser.Parser) ModelContext(org.xwiki.model.ModelContext) EntityReferenceProvider(org.xwiki.model.reference.EntityReferenceProvider) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) EntityReference(org.xwiki.model.reference.EntityReference) StringReader(java.io.StringReader) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 17 with Parser

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));
}
Also used : ConfigurationSource(org.xwiki.configuration.ConfigurationSource) SyntaxType(org.xwiki.rendering.syntax.SyntaxType) CoreConfiguration(com.xpn.xwiki.CoreConfiguration) Syntax(org.xwiki.rendering.syntax.Syntax) Parser(org.xwiki.rendering.parser.Parser) Test(org.junit.Test)

Example 18 with Parser

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);
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) ParseException(org.xwiki.rendering.parser.ParseException) TransformationException(org.xwiki.rendering.transformation.TransformationException) Parser(org.xwiki.rendering.parser.Parser) StreamParser(org.xwiki.rendering.parser.StreamParser)

Example 19 with Parser

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));
}
Also used : RenderingContext(org.xwiki.rendering.transformation.RenderingContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) Transformation(org.xwiki.rendering.transformation.Transformation) XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) Parser(org.xwiki.rendering.parser.Parser) StreamParser(org.xwiki.rendering.parser.StreamParser) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) Test(org.junit.Test)

Example 20 with Parser

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());
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) TransformationManager(org.xwiki.rendering.transformation.TransformationManager) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) Parser(org.xwiki.rendering.parser.Parser) Test(org.junit.Test)

Aggregations

Parser (org.xwiki.rendering.parser.Parser)24 XDOM (org.xwiki.rendering.block.XDOM)19 StringReader (java.io.StringReader)17 Test (org.junit.Test)13 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)8 Block (org.xwiki.rendering.block.Block)5 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)5 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)5 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)4 DocumentReference (org.xwiki.model.reference.DocumentReference)4 Syntax (org.xwiki.rendering.syntax.Syntax)4 TransformationManager (org.xwiki.rendering.transformation.TransformationManager)4 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)3 ConfigurationSource (org.xwiki.configuration.ConfigurationSource)3 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)3 SyntaxType (org.xwiki.rendering.syntax.SyntaxType)3 Expectations (org.jmock.Expectations)2 ModelContext (org.xwiki.model.ModelContext)2 EntityReference (org.xwiki.model.reference.EntityReference)2 EntityReferenceProvider (org.xwiki.model.reference.EntityReferenceProvider)2