Search in sources :

Example 36 with DocumentModelBridge

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);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) MacroContentParser(org.xwiki.rendering.macro.MacroContentParser) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) LinkBlock(org.xwiki.rendering.block.LinkBlock) MetaData(org.xwiki.rendering.listener.MetaData) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) MacroBlock(org.xwiki.rendering.block.MacroBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) ContextMacroParameters(org.xwiki.rendering.macro.context.ContextMacroParameters) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 37 with DocumentModelBridge

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);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MacroContentParser(org.xwiki.rendering.macro.MacroContentParser) MetaData(org.xwiki.rendering.listener.MetaData) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ContextMacroParameters(org.xwiki.rendering.macro.context.ContextMacroParameters) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 38 with DocumentModelBridge

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;
}
Also used : DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 39 with DocumentModelBridge

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;
    }
}
Also used : DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MaintainerServiceException(org.xwiki.annotation.maintainer.MaintainerServiceException)

Example 40 with DocumentModelBridge

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;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) HashMap(java.util.HashMap) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Aggregations

DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)58 DocumentReference (org.xwiki.model.reference.DocumentReference)43 Test (org.junit.Test)39 Expectations (org.jmock.Expectations)18 XDOM (org.xwiki.rendering.block.XDOM)18 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)14 Syntax (org.xwiki.rendering.syntax.Syntax)11 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)10 MacroBlock (org.xwiki.rendering.block.MacroBlock)9 DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)8 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)7 Block (org.xwiki.rendering.block.Block)6 HashMap (java.util.HashMap)5 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)5 DocumentDisplayer (org.xwiki.display.internal.DocumentDisplayer)5 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)5 MetaData (org.xwiki.rendering.listener.MetaData)5 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)5 StringReader (java.io.StringReader)4