Search in sources :

Example 11 with Parser

use of org.xwiki.rendering.parser.Parser in project xwiki-platform by xwiki.

the class AbstractMacroContentTableBlockDataSourceTest method setUpContentExpectation.

protected void setUpContentExpectation(final String macroContent) throws Exception {
    final MacroContentParser parser = getComponentManager().getInstance(MacroContentParser.class);
    // In order to make it easy to write unit tests, we allow tests to pass a string written in XWiki/2.0 synyax
    // which we then parser to generate an XDOM that we use in the expectation.
    final XDOM expectedXDOM = getComponentManager().<Parser>getInstance(Parser.class, Syntax.XWIKI_2_0.toIdString()).parse(new StringReader(macroContent));
    getMockery().checking(new Expectations() {

        {
            // Simulate parsing the macro content that returns a XDOM not containing a table
            oneOf(parser).parse(macroContent, null, true, false);
            will(returnValue(expectedXDOM));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) XDOM(org.xwiki.rendering.block.XDOM) MacroContentParser(org.xwiki.rendering.macro.MacroContentParser) StringReader(java.io.StringReader) URLResourceReferenceTypeParser(org.xwiki.rendering.internal.parser.reference.type.URLResourceReferenceTypeParser) DocumentResourceReferenceTypeParser(org.xwiki.rendering.internal.parser.reference.type.DocumentResourceReferenceTypeParser) XWiki20Parser(org.xwiki.rendering.internal.parser.xwiki20.XWiki20Parser) Parser(org.xwiki.rendering.parser.Parser) XWiki20ImageReferenceParser(org.xwiki.rendering.internal.parser.xwiki20.XWiki20ImageReferenceParser) XWiki20LinkReferenceParser(org.xwiki.rendering.internal.parser.xwiki20.XWiki20LinkReferenceParser) SpaceResourceReferenceTypeParser(org.xwiki.rendering.internal.parser.reference.type.SpaceResourceReferenceTypeParser) DefaultUntypedLinkReferenceParser(org.xwiki.rendering.internal.parser.reference.DefaultUntypedLinkReferenceParser) MacroContentParser(org.xwiki.rendering.macro.MacroContentParser) AttachmentResourceReferenceTypeParser(org.xwiki.rendering.internal.parser.reference.type.AttachmentResourceReferenceTypeParser)

Example 12 with Parser

use of org.xwiki.rendering.parser.Parser in project xwiki-platform by xwiki.

the class DefaultWikiComponentBridgeTest method getHandledMethods.

@Test
public void getHandledMethods() throws Exception {
    final ComponentManager componentManager = getComponentManager().getInstance(ComponentManager.class);
    final ContentParser contentParser = getComponentManager().getInstance(ContentParser.class);
    final Parser parser = getMockery().mock(Parser.class);
    final XDOM xdom = new XDOM(new ArrayList<Block>());
    final BaseObject methodObject = getMockery().mock(BaseObject.class, "method");
    final Vector<BaseObject> methodObjects = new Vector<BaseObject>();
    methodObjects.add(methodObject);
    getMockery().checking(new Expectations() {

        {
            oneOf(componentDoc).getObjectNumbers(METHOD_CLASS);
            will(returnValue(1));
            oneOf(componentDoc).getObjects(METHOD_CLASS);
            will(returnValue(methodObjects));
            allowing(methodObject).getStringValue(METHOD_NAME_FIELD);
            will(returnValue("test"));
            allowing(methodObject).getStringValue(METHOD_CODE_FIELD);
            will(returnValue("test"));
            oneOf(componentDoc).getSyntax();
            will(returnValue(Syntax.XWIKI_2_1));
            oneOf(contentParser).parse("test", Syntax.XWIKI_2_1, DOC_REFERENCE);
            will(returnValue(xdom));
        }
    });
    Assert.assertEquals(1, bridge.getHandledMethods(DOC_REFERENCE).size());
}
Also used : Expectations(org.jmock.Expectations) XDOM(org.xwiki.rendering.block.XDOM) ComponentManager(org.xwiki.component.manager.ComponentManager) ContentParser(org.xwiki.component.wiki.internal.bridge.ContentParser) Block(org.xwiki.rendering.block.Block) Vector(java.util.Vector) ContentParser(org.xwiki.component.wiki.internal.bridge.ContentParser) Parser(org.xwiki.rendering.parser.Parser) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 13 with Parser

use of org.xwiki.rendering.parser.Parser in project xwiki-platform by xwiki.

the class RenderingScriptService method parse.

/**
 * Parses a text written in the passed syntax.
 *
 * @param text the text to parse
 * @param syntaxId the id of the syntax in which the text is written in
 * @return the XDOM representing the AST of the parsed text or null if an error occurred
 * @since 3.2M3
 */
public XDOM parse(String text, String syntaxId) {
    XDOM result;
    try {
        Parser parser = this.componentManagerProvider.get().getInstance(Parser.class, syntaxId);
        result = parser.parse(new StringReader(text));
    } catch (Exception e) {
        result = null;
    }
    return result;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) ParseException(org.xwiki.rendering.parser.ParseException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MacroLookupException(org.xwiki.rendering.macro.MacroLookupException) Parser(org.xwiki.rendering.parser.Parser)

Example 14 with Parser

use of org.xwiki.rendering.parser.Parser in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method registerWikiMacro.

private void registerWikiMacro(String macroId, String macroContent, Syntax syntax, List<WikiMacroParameterDescriptor> parameterDescriptors) throws Exception {
    WikiMacroDescriptor descriptor = new WikiMacroDescriptor(new MacroId(macroId), "Wiki Macro", "Description", "Test", WikiMacroVisibility.GLOBAL, new DefaultContentDescriptor(false), parameterDescriptors);
    Parser parser = getComponentManager().getInstance(Parser.class, syntax.toIdString());
    DefaultWikiMacro wikiMacro = new DefaultWikiMacro(wikiMacroDocumentReference, null, true, descriptor, parser.parse(new StringReader(macroContent)), syntax, getComponentManager());
    this.wikiMacroManager.registerWikiMacro(wikiMacroDocumentReference, wikiMacro);
}
Also used : DefaultContentDescriptor(org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor) StringReader(java.io.StringReader) MacroId(org.xwiki.rendering.macro.MacroId) WikiMacroDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor) Parser(org.xwiki.rendering.parser.Parser)

Example 15 with Parser

use of org.xwiki.rendering.parser.Parser in project xwiki-platform by xwiki.

the class IntegrationTests method initialize.

@RenderingTestSuite.Initialized
public void initialize(MockitoComponentManager componentManager) throws Exception {
    ModelContext modelContext = componentManager.registerMockComponent(ModelContext.class);
    when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("currentWiki"));
    // Document Access Bridge mock
    DocumentAccessBridge dab = componentManager.registerMockComponent(DocumentAccessBridge.class);
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    DocumentReference currentDocumentReference = new DocumentReference("currentwiki", "currentspace", "currentpage");
    DocumentModelBridge document = Mockito.mock(DocumentModelBridge.class);
    when(dab.getDocumentURL(new DocumentReference("currentWiki", "space", "page"), "temp", null, null)).thenReturn("temppath");
    when(dab.getCurrentDocumentReference()).thenReturn(currentDocumentReference);
    when(dab.exists(documentReference)).thenReturn(true);
    when(dab.getDocumentInstance(documentReference)).thenReturn(document);
    when(dab.getCurrentUserReference()).thenReturn(null);
    DocumentDisplayer displayer = componentManager.registerMockComponent(DocumentDisplayer.class);
    Parser parser = componentManager.getInstance(Parser.class, Syntax.XWIKI_2_0.toIdString());
    final XDOM xdom = parser.parse(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(WIKI_CONTENT_FILE)));
    when(displayer.display(eq(document), any(DocumentDisplayerParameters.class))).thenReturn(xdom);
    AuthorizationManager authorizationManager = componentManager.registerMockComponent(AuthorizationManager.class);
    when(authorizationManager.hasAccess(Right.VIEW, null, documentReference)).thenReturn(true);
    componentManager.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "current");
    componentManager.registerMockComponent(SpaceReferenceResolver.TYPE_STRING, "current");
}
Also used : ModelContext(org.xwiki.model.ModelContext) DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) DocumentDisplayer(org.xwiki.display.internal.DocumentDisplayer) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) InputStreamReader(java.io.InputStreamReader) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Parser(org.xwiki.rendering.parser.Parser)

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