use of org.xwiki.rendering.block.MetaDataBlock in project xwiki-platform by xwiki.
the class CurrentMacroEntityReferenceResolver method resolve.
@Override
public EntityReference resolve(String representation, EntityType entityType, Object... parameters) {
// There must one parameter and it must be of type Block
if (parameters.length != 1 || !(parameters[0] instanceof Block)) {
throw new IllegalArgumentException(String.format("You must pass one parameter of type [%s]", Block.class.getName()));
}
Block currentBlock = (Block) parameters[0];
EntityReference result;
MetaDataBlock metaDataBlock = currentBlock.getFirstBlock(new MetadataBlockMatcher(MetaData.BASE), Block.Axes.ANCESTOR);
// If no Source MetaData was found resolve against the current entity as a failsafe solution.
if (metaDataBlock == null) {
result = this.currentEntityReferenceResolver.resolve(representation, entityType);
} else {
String sourceMetaData = (String) metaDataBlock.getMetaData().getMetaData(MetaData.BASE);
result = this.currentEntityReferenceResolver.resolve(representation, entityType, this.currentEntityReferenceResolver.resolve(sourceMetaData, EntityType.DOCUMENT));
}
return result;
}
use of org.xwiki.rendering.block.MetaDataBlock 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.block.MetaDataBlock in project xwiki-platform by xwiki.
the class DisplayMacro method execute.
@Override
public List<Block> execute(DisplayMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
// Step 1: Perform checks.
if (parameters.getReference() == null) {
throw new MacroExecutionException("You must specify a 'reference' parameter pointing to the entity to display.");
}
DocumentReference includedReference = resolve(context.getCurrentMacroBlock(), parameters);
checkRecursiveDisplay(context.getCurrentMacroBlock(), includedReference);
if (!this.documentAccessBridge.isDocumentViewable(includedReference)) {
throw new MacroExecutionException("Current user [" + this.documentAccessBridge.getCurrentUserReference() + "] doesn't have view rights on document [" + this.defaultEntityReferenceSerializer.serialize(includedReference) + "]");
}
// Step 2: Retrieve the included document.
DocumentModelBridge documentBridge;
try {
documentBridge = this.documentAccessBridge.getDocumentInstance(includedReference);
} catch (Exception e) {
throw new MacroExecutionException("Failed to load Document [" + this.defaultEntityReferenceSerializer.serialize(includedReference) + "]", e);
}
// Step 3: Display the content of the included document.
// Display the content in an isolated execution and transformation context.
DocumentDisplayerParameters displayParameters = new DocumentDisplayerParameters();
displayParameters.setContentTransformed(true);
displayParameters.setExecutionContextIsolated(displayParameters.isContentTransformed());
displayParameters.setSectionId(parameters.getSection());
displayParameters.setTransformationContextIsolated(displayParameters.isContentTransformed());
displayParameters.setTargetSyntax(context.getTransformationContext().getTargetSyntax());
displayParameters.setContentTranslated(true);
Stack<Object> references = this.displaysBeingExecuted.get();
if (references == null) {
references = new Stack<Object>();
this.displaysBeingExecuted.set(references);
}
references.push(includedReference);
XDOM result;
try {
result = this.documentDisplayer.display(documentBridge, displayParameters);
} catch (Exception e) {
throw new MacroExecutionException(e.getMessage(), e);
} finally {
references.pop();
}
// Step 4: Wrap Blocks in a MetaDataBlock with the "source" meta data specified so that we know from where the
// content comes and "base" meta data so that reference are properly resolved
MetaDataBlock metadata = new MetaDataBlock(result.getChildren(), result.getMetaData());
String source = this.defaultEntityReferenceSerializer.serialize(includedReference);
metadata.getMetaData().addMetaData(MetaData.SOURCE, source);
metadata.getMetaData().addMetaData(MetaData.BASE, source);
return Arrays.<Block>asList(metadata);
}
use of org.xwiki.rendering.block.MetaDataBlock 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);
}
use of org.xwiki.rendering.block.MetaDataBlock in project xwiki-platform by xwiki.
the class IncludeMacroTest method testIncludeMacroWhenIncludingDocumentWithRelativeReferences.
/**
* Verify that relative links returned by the Include macro as wrapped with a MetaDataBlock.
*/
@Test
public void testIncludeMacroWhenIncludingDocumentWithRelativeReferences() throws Exception {
String expected = "beginDocument\n" + "beginMetaData [[base]=[includedWiki:includedSpace.includedPage]" + "[source]=[includedWiki:includedSpace.includedPage][syntax]=[XWiki 2.0]]\n" + "beginParagraph\n" + "beginLink [Typed = [false] Type = [doc] Reference = [page]] [false]\n" + "endLink [Typed = [false] Type = [doc] Reference = [page]] [false]\n" + "onSpace\n" + "beginLink [Typed = [true] Type = [attach] Reference = [test.png]] [false]\n" + "endLink [Typed = [true] Type = [attach] Reference = [test.png]] [false]\n" + "onSpace\n" + "onImage [Typed = [false] Type = [attach] Reference = [test.png]] [true]\n" + "endParagraph\n" + "endMetaData [[base]=[includedWiki:includedSpace.includedPage]" + "[source]=[includedWiki:includedSpace.includedPage][syntax]=[XWiki 2.0]]\n" + "endDocument";
final DocumentReference includedDocumentReference = new DocumentReference("includedWiki", "includedSpace", "includedPage");
setUpDocumentMock("includedWiki:includedSpace.includedPage", includedDocumentReference, "[[page]] [[attach:test.png]] image:test.png");
getMockery().checking(new Expectations() {
{
oneOf(mockSetup.bridge).isDocumentViewable(with(any(DocumentReference.class)));
will(returnValue(true));
oneOf(mockSetup.bridge).pushDocumentInContext(with(any(Map.class)), with(any(DocumentModelBridge.class)));
oneOf(mockSetup.bridge).getCurrentDocumentReference();
will(returnValue(includedDocumentReference));
oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
}
});
IncludeMacroParameters parameters = new IncludeMacroParameters();
parameters.setReference("includedWiki:includedSpace.includedPage");
parameters.setContext(Context.NEW);
List<Block> blocks = this.includeMacro.execute(parameters, null, createMacroTransformationContext("whatever", false));
assertBlocks(expected, blocks, this.rendererFactory);
}
Aggregations