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);
}
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;
}
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));
}
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));
}
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);
}
Aggregations