Search in sources :

Example 1 with MetaData

use of org.xwiki.rendering.listener.MetaData in project xwiki-platform by xwiki.

the class DisplayMacroTest method testDisplayMacroInsideBaseMetaDataBlockAndWithRelativeDocumentReferencePassed.

@Test
public void testDisplayMacroInsideBaseMetaDataBlockAndWithRelativeDocumentReferencePassed() throws Exception {
    String expected = "beginDocument\n" + "beginMetaData [[base]=[wiki:space.relativePage][source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "beginParagraph\n" + "onWord [content]\n" + "endParagraph\n" + "endMetaData [[base]=[wiki:space.relativePage][source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "endDocument";
    DisplayMacroParameters parameters = new DisplayMacroParameters();
    parameters.setReference("relativePage");
    MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
    // Add a Source MetaData Block as a parent of the display Macro block.
    new MetaDataBlock(Collections.<Block>singletonList(macroContext.getCurrentMacroBlock()), new MetaData(Collections.<String, Object>singletonMap(MetaData.BASE, "wiki:space.page")));
    final DocumentReference sourceReference = new DocumentReference("wiki", "space", "page");
    final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "relativePage");
    setUpDocumentMock("relativePage", resolvedReference, "content");
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentReferenceResolver).resolve(with("wiki:space.page"), with(IsArray.array(any(MacroBlock.class))));
            will(returnValue(sourceReference));
            allowing(mockDocumentReferenceResolver).resolve(with("relativePage"), with(IsArray.array(any(MacroBlock.class))));
            will(returnValue(resolvedReference));
            oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
            will(returnValue(true));
            oneOf(mockSetup.bridge).pushDocumentInContext(with(any(Map.class)), with(same(mockDocument)));
            oneOf(mockSetup.bridge).getCurrentDocumentReference();
            will(returnValue(resolvedReference));
            oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
        }
    });
    List<Block> blocks = this.displayMacro.execute(parameters, null, macroContext);
    assertBlocks(expected, blocks, this.rendererFactory);
}
Also used : Expectations(org.jmock.Expectations) MetaData(org.xwiki.rendering.listener.MetaData) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) DisplayMacroParameters(org.xwiki.rendering.macro.display.DisplayMacroParameters) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

Example 2 with MetaData

use of org.xwiki.rendering.listener.MetaData in project xwiki-platform by xwiki.

the class ContextMacro method execute.

@Override
public List<Block> execute(ContextMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    if (parameters.getDocument() == null) {
        throw new MacroExecutionException("You must specify a 'document' parameter pointing to the document to " + "set in the context as the current document.");
    }
    DocumentReference referencedDocReference = this.macroDocumentReferenceResolver.resolve(parameters.getDocument(), context.getCurrentMacroBlock());
    boolean currentContextHasProgrammingRights = this.documentAccessBridge.hasProgrammingRights();
    List<Block> result;
    try {
        Map<String, Object> backupObjects = new HashMap<>();
        try {
            this.documentAccessBridge.pushDocumentInContext(backupObjects, referencedDocReference);
            // error since it would be a security breach otherwise.
            if (this.documentAccessBridge.hasProgrammingRights() && !currentContextHasProgrammingRights) {
                throw new MacroExecutionException("Current document must have programming rights since the " + "context document provided [" + parameters.getDocument() + "] has programming rights.");
            }
            MetaData metadata = new MetaData();
            metadata.addMetaData(MetaData.SOURCE, parameters.getDocument());
            metadata.addMetaData(MetaData.BASE, parameters.getDocument());
            XDOM xdom = this.contentParser.parse(content, context, false, metadata, false);
            // Configure the  Transformation Context depending on the mode asked.
            if (parameters.getTransformationContext() == TransformationContextMode.DOCUMENT || parameters.getTransformationContext() == TransformationContextMode.TRANSFORMATIONS) {
                // Apply the transformations but with a Transformation Context having the XDOM of the passed
                // document so that macros execute on the passed document's XDOM (e.g. the TOC macro will generate
                // the toc for the passed document instead of the current document).
                DocumentModelBridge referencedDoc = this.documentAccessBridge.getTranslatedDocumentInstance(referencedDocReference);
                XDOM referencedXDOM = referencedDoc.getXDOM();
                if (parameters.getTransformationContext() == TransformationContextMode.TRANSFORMATIONS) {
                    // Get the XDOM from the referenced doc but with Transformations applied so that all macro are
                    // executed and contribute XDOM elements.
                    // IMPORTANT: This can be dangerous since it means executing macros, and thus also script macros
                    // defined in the referenced document. To be used with caution.
                    TransformationContext referencedTxContext = new TransformationContext(referencedXDOM, referencedDoc.getSyntax());
                    this.transformationManager.performTransformations(referencedXDOM, referencedTxContext);
                }
                // Now execute transformation on the context macro content but with the referenced XDOM in the
                // Transformation context!
                TransformationContext txContext = new TransformationContext(referencedXDOM, referencedDoc.getSyntax());
                this.transformationManager.performTransformations(xdom, txContext);
            }
            // Keep metadata so that the result stay associated to context properties when inserted in the parent
            // XDOM
            result = Arrays.asList((Block) new MetaDataBlock(xdom.getChildren(), xdom.getMetaData()));
        } finally {
            this.documentAccessBridge.popDocumentFromContext(backupObjects);
        }
    } catch (Exception e) {
        if (e instanceof MacroExecutionException) {
            throw (MacroExecutionException) e;
        } else {
            throw new MacroExecutionException(String.format("Failed to render page in the context of [%s]", referencedDocReference), e);
        }
    }
    return result;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MetaData(org.xwiki.rendering.listener.MetaData) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 3 with MetaData

use of org.xwiki.rendering.listener.MetaData in project xwiki-platform by xwiki.

the class DocumentTableBlockDataSourceTest method isDefinedChartSourceTheCurrentDocumentWhenReferenceNotNullAndMatching.

@Test
public void isDefinedChartSourceTheCurrentDocumentWhenReferenceNotNullAndMatching() throws Exception {
    DocumentAccessBridge dab = this.componentManager.getInstance(DocumentAccessBridge.class);
    DocumentReference currentReference = new DocumentReference("currentwiki", "currentspace", "currentpage");
    when(dab.getCurrentDocumentReference()).thenReturn(currentReference);
    DocumentReferenceResolver<String> resolver = this.componentManager.getInstance(DocumentReferenceResolver.TYPE_STRING);
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    when(resolver.resolve("wiki:space.page", currentReference)).thenReturn(documentReference);
    MacroBlock currentMacroBlock = mock(MacroBlock.class);
    MetaDataBlock metaDataBlock = new MetaDataBlock(Collections.EMPTY_LIST, new MetaData(Collections.singletonMap(MetaData.SOURCE, (Object) "wiki:space.page")));
    when(currentMacroBlock.getFirstBlock(any(BlockMatcher.class), any(Block.Axes.class))).thenReturn(metaDataBlock);
    DocumentTableBlockDataSource source = this.componentManager.getComponentUnderTest();
    source.setParameter("document", "wiki:space.page");
    Assert.assertTrue(source.isDefinedChartSourceTheCurrentDocument(currentMacroBlock));
}
Also used : MetaData(org.xwiki.rendering.listener.MetaData) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) BlockMatcher(org.xwiki.rendering.block.match.BlockMatcher) MacroBlock(org.xwiki.rendering.block.MacroBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

Example 4 with MetaData

use of org.xwiki.rendering.listener.MetaData in project xwiki-platform by xwiki.

the class CurrentMacroEntityReferenceResolverTest method resolveWhenMetaDataBlock.

@Test
public void resolveWhenMetaDataBlock() throws Exception {
    DocumentReference baseReference = new DocumentReference("basewiki", "basespace", "basepage");
    EntityReference expectedReference = new AttachmentReference("file", baseReference);
    EntityReferenceResolver<String> currentEntityReferenceResolver = mocker.getInstance(EntityReferenceResolver.TYPE_STRING, "current");
    when(currentEntityReferenceResolver.resolve("basewiki:basespace.basepage", EntityType.DOCUMENT)).thenReturn(baseReference);
    when(currentEntityReferenceResolver.resolve("file", EntityType.ATTACHMENT, baseReference)).thenReturn(expectedReference);
    Block wordBlock = new WordBlock("whatever");
    MetaData metaData = new MetaData(Collections.<String, Object>singletonMap(MetaData.BASE, "basewiki:basespace.basepage"));
    new XDOM(Arrays.<Block>asList(new MetaDataBlock(Arrays.<Block>asList(wordBlock), metaData)));
    Assert.assertEquals(expectedReference, mocker.getComponentUnderTest().resolve("file", EntityType.ATTACHMENT, wordBlock));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XDOM(org.xwiki.rendering.block.XDOM) MetaData(org.xwiki.rendering.listener.MetaData) WordBlock(org.xwiki.rendering.block.WordBlock) EntityReference(org.xwiki.model.reference.EntityReference) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

Example 5 with MetaData

use of org.xwiki.rendering.listener.MetaData in project xwiki-platform by xwiki.

the class IncludeMacroTest method testIncludeMacroInsideSourceMetaDataBlockAndWithRelativeDocumentReferencePassed.

@Test
public void testIncludeMacroInsideSourceMetaDataBlockAndWithRelativeDocumentReferencePassed() throws Exception {
    String expected = "beginDocument\n" + "beginMetaData [[source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "beginParagraph\n" + "onWord [content]\n" + "endParagraph\n" + "endMetaData [[source]=[wiki:space.relativePage][syntax]=[XWiki 2.0]]\n" + "endDocument";
    IncludeMacroParameters parameters = new IncludeMacroParameters();
    parameters.setReference("relativePage");
    final MacroTransformationContext macroContext = createMacroTransformationContext("whatever", false);
    // Add a Source MetaData Block as a parent of the include Macro block.
    new MetaDataBlock(Collections.<Block>singletonList(macroContext.getCurrentMacroBlock()), new MetaData(Collections.<String, Object>singletonMap(MetaData.BASE, "wiki:space.page")));
    final DocumentReference sourceReference = new DocumentReference("wiki", "space", "page");
    final DocumentReference resolvedReference = new DocumentReference("wiki", "space", "relativePage");
    final DocumentModelBridge mockDocument = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            oneOf(mockDocumentReferenceResolver).resolve("relativePage", macroContext.getCurrentMacroBlock());
            will(returnValue(resolvedReference));
            oneOf(mockSetup.bridge).isDocumentViewable(resolvedReference);
            will(returnValue(true));
            oneOf(mockSetup.bridge).getDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            oneOf(mockSetup.bridge).getTranslatedDocumentInstance(resolvedReference);
            will(returnValue(mockDocument));
            oneOf(mockSetup.bridge).getCurrentDocumentReference();
            will(returnValue(sourceReference));
            oneOf(mockDocument).getXDOM();
            will(returnValue(getXDOM("content")));
            oneOf(mockDocument).getSyntax();
            will(returnValue(Syntax.XWIKI_2_0));
            oneOf(mockDocument).getDocumentReference();
            will(returnValue(resolvedReference));
            allowing(mockDocument).getRealLanguage();
            will(returnValue(""));
        }
    });
    List<Block> blocks = this.includeMacro.execute(parameters, null, macroContext);
    assertBlocks(expected, blocks, this.rendererFactory);
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MetaData(org.xwiki.rendering.listener.MetaData) IncludeMacroParameters(org.xwiki.rendering.macro.include.IncludeMacroParameters) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

Aggregations

DocumentReference (org.xwiki.model.reference.DocumentReference)5 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)5 MetaData (org.xwiki.rendering.listener.MetaData)5 Test (org.junit.Test)4 Block (org.xwiki.rendering.block.Block)4 MacroBlock (org.xwiki.rendering.block.MacroBlock)3 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)3 HashMap (java.util.HashMap)2 Expectations (org.jmock.Expectations)2 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)2 XDOM (org.xwiki.rendering.block.XDOM)2 Map (java.util.Map)1 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)1 AttachmentReference (org.xwiki.model.reference.AttachmentReference)1 EntityReference (org.xwiki.model.reference.EntityReference)1 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)1 WordBlock (org.xwiki.rendering.block.WordBlock)1 BlockMatcher (org.xwiki.rendering.block.match.BlockMatcher)1 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)1 DisplayMacroParameters (org.xwiki.rendering.macro.display.DisplayMacroParameters)1