use of org.xwiki.rendering.block.MetaDataBlock in project xwiki-platform by xwiki.
the class DefaultOfficeResourceViewer method maybeFixExpandedMacroAncestor.
private void maybeFixExpandedMacroAncestor(Block block) {
ExpandedMacroBlock expandedMacro = block.getFirstBlock(new ClassBlockMatcher(ExpandedMacroBlock.class), Block.Axes.ANCESTOR_OR_SELF);
if (expandedMacro != null) {
Block parent = expandedMacro.getParent();
if (!(parent instanceof MetaDataBlock) || !((MetaDataBlock) parent).getMetaData().contains(MODULE_NAME)) {
MetaDataBlock metaData = new MetaDataBlock(Collections.<Block>emptyList());
// Use a syntax that supports relative path resource references (we use relative paths to include the
// temporary files).
metaData.getMetaData().addMetaData(MetaData.SYNTAX, Syntax.XWIKI_2_1);
metaData.getMetaData().addMetaData(MODULE_NAME, true);
parent.replaceChild(metaData, expandedMacro);
metaData.addChild(expandedMacro);
}
}
}
use of org.xwiki.rendering.block.MetaDataBlock in project xwiki-platform by xwiki.
the class DefaultOfficeResourceViewerTest method testViewPresentation.
@Test
public void testViewPresentation() throws Exception {
AttachmentResourceReference attachResourceRef = new AttachmentResourceReference("xwiki:Some.Page@presentation.odp");
DocumentReference documentReference = new DocumentReference("wiki", "Some", "Page");
AttachmentReference attachmentReference = new AttachmentReference("presentation.odp", documentReference);
AttachmentReferenceResolver<String> attachmentReferenceResolver = mocker.getInstance(AttachmentReferenceResolver.TYPE_STRING, "current");
when(attachmentReferenceResolver.resolve(attachResourceRef.getReference())).thenReturn(attachmentReference);
when(documentAccessBridge.getAttachmentReferences(attachmentReference.getDocumentReference())).thenReturn(Arrays.asList(attachmentReference));
when(documentAccessBridge.getAttachmentVersion(attachmentReference)).thenReturn("3.2");
ByteArrayInputStream attachmentContent = new ByteArrayInputStream(new byte[256]);
when(documentAccessBridge.getAttachmentContent(attachmentReference)).thenReturn(attachmentContent);
ResourceReference imageReference = new ResourceReference("slide0.png", ResourceType.URL);
ExpandedMacroBlock galleryMacro = new ExpandedMacroBlock("gallery", Collections.singletonMap("width", "300px"), null, false);
galleryMacro.addChild(new ImageBlock(imageReference, true));
XDOM xdom = new XDOM(Collections.<Block>singletonList(galleryMacro));
Map<String, byte[]> artifacts = Collections.singletonMap("slide0.png", new byte[8]);
XDOMOfficeDocument xdomOfficeDocument = new XDOMOfficeDocument(xdom, artifacts, mocker);
PresentationBuilder presentationBuilder = mocker.getInstance(PresentationBuilder.class);
when(presentationBuilder.build(attachmentContent, attachmentReference.getName(), documentReference)).thenReturn(xdomOfficeDocument);
Map<String, ?> viewParameters = Collections.singletonMap("ownerDocument", documentReference);
TemporaryResourceReference temporaryResourceReference = new TemporaryResourceReference("officeviewer", Arrays.asList(String.valueOf(viewParameters.hashCode()), "slide0.png"), documentReference);
Type type = new DefaultParameterizedType(null, ResourceReferenceSerializer.class, TemporaryResourceReference.class, ExtendedURL.class);
ResourceReferenceSerializer<TemporaryResourceReference, ExtendedURL> urlTemporaryResourceReferenceSerializer = mocker.getInstance(type, "standard/tmp");
ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("url", "to", "slide0.png"));
when(urlTemporaryResourceReferenceSerializer.serialize(temporaryResourceReference)).thenReturn(extendedURL);
XDOM output = this.mocker.getComponentUnderTest().createView(attachResourceRef, viewParameters);
ImageBlock imageBlock = (ImageBlock) output.getBlocks(new ClassBlockMatcher(ImageBlock.class), Block.Axes.DESCENDANT).get(0);
assertEquals("/url/to/slide0.png", imageBlock.getReference().getReference());
galleryMacro = (ExpandedMacroBlock) output.getBlocks(new ClassBlockMatcher(ExpandedMacroBlock.class), Block.Axes.DESCENDANT).get(0);
assertFalse(galleryMacro.getParent() instanceof XDOM);
assertEquals(Syntax.XWIKI_2_1, ((MetaDataBlock) galleryMacro.getParent()).getMetaData().getMetaData(MetaData.SYNTAX));
TemporaryResourceStore store = mocker.getInstance(TemporaryResourceStore.class);
verify(store).createTemporaryFile(eq(temporaryResourceReference), any(InputStream.class));
}
use of org.xwiki.rendering.block.MetaDataBlock in project xwiki-platform by xwiki.
the class DisplayMacroTest method testDisplayMacroWhenDisplayingDocumentWithRelativeReferences.
/**
* Verify that relative links returned by the display macro as wrapped with a MetaDataBlock.
*/
@Test
public void testDisplayMacroWhenDisplayingDocumentWithRelativeReferences() throws Exception {
String expected = "beginDocument\n" + "beginMetaData [[base]=[displayedWiki:displayedSpace.displayedPage]" + "[source]=[displayedWiki:displayedSpace.displayedPage][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]=[displayedWiki:displayedSpace.displayedPage]" + "[source]=[displayedWiki:displayedSpace.displayedPage][syntax]=[XWiki 2.0]]\n" + "endDocument";
final DocumentReference displayedDocumentReference = new DocumentReference("displayedWiki", "displayedSpace", "displayedPage");
setUpDocumentMock("displayedWiki:displayedSpace.displayedPage", displayedDocumentReference, "[[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(displayedDocumentReference));
oneOf(mockSetup.bridge).popDocumentFromContext(with(any(Map.class)));
}
});
DisplayMacroParameters parameters = new DisplayMacroParameters();
parameters.setReference("displayedWiki:displayedSpace.displayedPage");
List<Block> blocks = this.displayMacro.execute(parameters, null, createMacroTransformationContext("whatever", false));
assertBlocks(expected, blocks, this.rendererFactory);
}
use of org.xwiki.rendering.block.MetaDataBlock 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.block.MetaDataBlock in project xwiki-platform by xwiki.
the class IncludeMacro method execute.
@Override
public List<Block> execute(IncludeMacroParameters 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 include.");
}
DocumentReference includedReference = resolve(context.getCurrentMacroBlock(), parameters);
checkRecursiveInclusion(context.getCurrentMacroBlock(), includedReference);
if (!this.documentAccessBridge.isDocumentViewable(includedReference)) {
throw new MacroExecutionException(String.format("Current user [%s] doesn't have view rights on document [%s]", this.documentAccessBridge.getCurrentUserReference(), this.defaultEntityReferenceSerializer.serialize(includedReference)));
}
Context parametersContext = parameters.getContext();
// 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.
// Check the value of the "context" parameter.
//
// If CONTEXT_NEW then display the content in an isolated execution and transformation context.
//
// if CONTEXT_CURRENT then display the content without performing any transformations (we don't want any Macro
// to be executed at this stage since they should be executed by the currently running Macro Transformation.
DocumentDisplayerParameters displayParameters = new DocumentDisplayerParameters();
displayParameters.setContentTransformed(parametersContext == Context.NEW);
displayParameters.setExecutionContextIsolated(displayParameters.isContentTransformed());
displayParameters.setSectionId(parameters.getSection());
displayParameters.setTransformationContextIsolated(displayParameters.isContentTransformed());
displayParameters.setTransformationContextRestricted(context.getTransformationContext().isRestricted());
displayParameters.setTargetSyntax(context.getTransformationContext().getTargetSyntax());
displayParameters.setContentTranslated(true);
Stack<Object> references = this.inclusionsBeingExecuted.get();
if (parametersContext == Context.NEW) {
if (references == null) {
references = new Stack<Object>();
this.inclusionsBeingExecuted.set(references);
}
references.push(includedReference);
}
XDOM result;
try {
result = this.documentDisplayer.display(documentBridge, displayParameters);
} catch (Exception e) {
throw new MacroExecutionException(e.getMessage(), e);
} finally {
if (parametersContext == Context.NEW) {
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);
if (parametersContext == Context.NEW) {
metadata.getMetaData().addMetaData(MetaData.BASE, source);
}
return Arrays.<Block>asList(metadata);
}
Aggregations