use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.
the class ContextMacroTest method executeOk.
@Test
public void executeOk() throws Exception {
MacroBlock macroBlock = new MacroBlock("context", Collections.<String, String>emptyMap(), false);
MacroTransformationContext macroContext = new MacroTransformationContext();
macroContext.setSyntax(Syntax.XWIKI_2_0);
macroContext.setCurrentMacroBlock(macroBlock);
DocumentReferenceResolver<String> resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "macro");
DocumentReference referencedDocumentReference = new DocumentReference("wiki", "space", "page");
when(resolver.resolve("wiki:space.page", macroBlock)).thenReturn(referencedDocumentReference);
DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
DocumentModelBridge dmb = mock(DocumentModelBridge.class);
when(dab.getTranslatedDocumentInstance(referencedDocumentReference)).thenReturn(dmb);
MacroContentParser parser = this.mocker.getInstance(MacroContentParser.class);
XDOM xdom = new XDOM(Arrays.asList((Block) new ParagraphBlock(Arrays.asList((Block) new LinkBlock(Collections.emptyList(), new ResourceReference("", ResourceType.DOCUMENT), false)))));
when(parser.parse(eq(""), same(macroContext), eq(false), any(MetaData.class), eq(false))).thenReturn(xdom);
ContextMacroParameters parameters = new ContextMacroParameters();
parameters.setDocument("wiki:space.page");
// Note: we're not testing the returned value here since this is done in integation tests.
this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
}
use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.
the class ContextMacroTest method executeWithReferencedDocumentHavingProgrammingRightsAndCallingDocumentToo.
@Test
public void executeWithReferencedDocumentHavingProgrammingRightsAndCallingDocumentToo() throws Exception {
MacroBlock macroBlock = new MacroBlock("context", Collections.<String, String>emptyMap(), false);
MacroTransformationContext macroContext = new MacroTransformationContext();
macroContext.setSyntax(Syntax.XWIKI_2_0);
macroContext.setCurrentMacroBlock(macroBlock);
DocumentReferenceResolver<String> resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "macro");
DocumentReference referencedDocumentReference = new DocumentReference("wiki", "space", "page");
when(resolver.resolve("wiki:space.page", macroBlock)).thenReturn(referencedDocumentReference);
DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
when(dab.hasProgrammingRights()).thenReturn(true).thenReturn(true);
DocumentModelBridge dmb = mock(DocumentModelBridge.class);
when(dab.getTranslatedDocumentInstance(referencedDocumentReference)).thenReturn(dmb);
MacroContentParser parser = this.mocker.getInstance(MacroContentParser.class);
when(parser.parse(eq(""), same(macroContext), eq(false), any(MetaData.class), eq(false))).thenReturn(new XDOM(Collections.emptyList()));
ContextMacroParameters parameters = new ContextMacroParameters();
parameters.setDocument("wiki:space.page");
this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
}
use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.
the class DocumentTableBlockDataSource method computeXDOM.
/**
* Get the XDOM for the data source.
*
* @param context the Macro context from which we can get the XDOM if the source is in the current content
* @return the XDOM in which the data source is located
* @throws MacroExecutionException in case of an error getting the XDOM
*/
private XDOM computeXDOM(MacroTransformationContext context) throws MacroExecutionException {
XDOM xdom;
// Context and 2) it's going to cause a cycle...
if (isDefinedChartSourceTheCurrentDocument(context.getCurrentMacroBlock())) {
xdom = context.getXDOM();
} else {
try {
DocumentModelBridge document = this.docBridge.getDocumentInstance(this.documentReference);
DocumentDisplayerParameters parameters = new DocumentDisplayerParameters();
parameters.setContentTranslated(true);
parameters.setTargetSyntax(context.getTransformationContext().getTargetSyntax());
parameters.setContentTranslated(true);
xdom = this.documentDisplayer.display(document, parameters);
} catch (Exception e) {
throw new MacroExecutionException(String.format("Error getting Chart table from document [%s]", this.documentReference, e));
}
}
return xdom;
}
use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.
the class DocumentContentAnnotationUpdateListener method onEvent.
@Override
public void onEvent(Event event, Object source, Object data) {
DocumentModelBridge currentDocument = (DocumentModelBridge) source;
DocumentModelBridge previousDocument = currentDocument.getOriginalDocument();
// maintainer, and the difference is in the content of the document
if (!isUpdating && !previousDocument.getContent().equals(currentDocument.getContent())) {
isUpdating = true;
String content = currentDocument.getContent();
String previousContent = previousDocument.getContent();
// maintain the document annotations
try {
maintainer.updateAnnotations(this.serializer.serialize(currentDocument.getDocumentReference()), previousContent, content);
} catch (MaintainerServiceException e) {
this.logger.warn(e.getMessage(), e);
// nothing else, just go further
}
isUpdating = false;
}
}
use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.
the class OfficeMacroImporter method buildXDOM.
/**
* Builds an XDOM with a single block, the office macro.
*
* @param attachmentReference the office file to be viewed
* @param filterStyles whether to filter text styles such as font, color, alignment, margins, etc.
* @return the XDOM that can be rendered to view the office file
*/
public XDOM buildXDOM(AttachmentReference attachmentReference, boolean filterStyles) {
Map<String, String> macroParams = new HashMap<String, String>();
macroParams.put("attachment", attachmentReference.getName());
if (!filterStyles) {
macroParams.put("filterStyles", "false");
}
MacroBlock officeMacro = new MacroBlock("office", macroParams, false);
XDOM xdom = new XDOM(Collections.<Block>singletonList(officeMacro));
// Since we're generating an XDOM block we need to set up the required MetaData information
// Set the BASE MetaData
xdom.getMetaData().addMetaData(MetaData.BASE, entityReferenceSerializer.serialize(attachmentReference.getDocumentReference()));
// Set the SYNTAX MetaData
try {
DocumentModelBridge document = documentAccessBridge.getTranslatedDocumentInstance(attachmentReference.getDocumentReference());
xdom.getMetaData().addMetaData(MetaData.SYNTAX, document.getSyntax());
} catch (Exception e) {
throw new RuntimeException(String.format("Failed to compute Syntax for the document containing attachment [%s]", attachmentReference), e);
}
return xdom;
}
Aggregations