Search in sources :

Example 46 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class SheetScriptService method getModifiableDocument.

/**
 * Note: This method gets the low level XWiki document through reflection by calling a protected method.
 *
 * @param document an instance of {@link Document} received from a script
 * @return an instance of {@link DocumentModelBridge} that wraps the low level document object exposed by the given
 *         document API and that can be <em>safely</em> modified
 */
private DocumentModelBridge getModifiableDocument(Document document) {
    try {
        // HACK: We try to get the modifiable XWikiDocument instance wrapped by the document API using reflection
        // because the corresponding method that clones the wrapped XWikiDocument instance is protected.
        Method getDocMethod = Document.class.getDeclaredMethod("getDoc", new Class[] {});
        getDocMethod.setAccessible(true);
        return (DocumentModelBridge) getDocMethod.invoke(document);
    } catch (Exception e) {
        throw new RuntimeException("Failed to get the modifiable XWikiDocument instance wrapped by the document API.", e);
    }
}
Also used : DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Method(java.lang.reflect.Method)

Example 47 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class XWikiLinkLabelGeneratorTest method generateWhenNestedPage.

@Test
public void generateWhenNestedPage() throws Exception {
    ResourceReference resourceReference = new DocumentResourceReference("WebHome");
    DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("space1", "NestedPage"), "WebHome");
    EntityReferenceResolver<ResourceReference> resourceReferenceResolver = this.mocker.getInstance(new DefaultParameterizedType(null, EntityReferenceResolver.class, ResourceReference.class));
    when(resourceReferenceResolver.resolve(resourceReference, EntityType.DOCUMENT)).thenReturn(documentReference);
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    DocumentModelBridge dmb = mock(DocumentModelBridge.class);
    when(dab.getTranslatedDocumentInstance(documentReference)).thenReturn(dmb);
    when(dmb.getTitle()).thenReturn("My title");
    EntityReferenceSerializer<String> localSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
    when(localSerializer.serialize(new SpaceReference("wiki", "space1", "NestedPage"))).thenReturn("space1.NestedPage");
    assertEquals("%l%la%n%na%N%NA " + "[wiki:space1.NestedPage.WebHome] NestedPage NestedPage Web Home Nested Page (My title) " + "[wiki:space1.NestedPage.WebHome] NestedPage NestedPage Web Home Nested Page (My title)", this.mocker.getComponentUnderTest().generate(resourceReference));
}
Also used : DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) EntityReferenceResolver(org.xwiki.model.reference.EntityReferenceResolver) SpaceReference(org.xwiki.model.reference.SpaceReference) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 48 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class WikiMacroEventListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    if (event instanceof AbstractDocumentEvent) {
        DocumentModelBridge document = (DocumentModelBridge) source;
        DocumentReference documentReference = document.getDocumentReference();
        if (event instanceof DocumentCreatedEvent || event instanceof DocumentUpdatedEvent) {
            registerMacro(documentReference);
        } else if (event instanceof DocumentDeletedEvent) {
            unregisterMacro(documentReference);
        }
    }
}
Also used : DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) AbstractDocumentEvent(org.xwiki.bridge.event.AbstractDocumentEvent) DocumentCreatedEvent(org.xwiki.bridge.event.DocumentCreatedEvent) DocumentUpdatedEvent(org.xwiki.bridge.event.DocumentUpdatedEvent) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 49 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class SheetDocumentDisplayer method applySheet.

/**
 * Applies a sheet to a document.
 *
 * @param document the document to apply the sheet to
 * @param sheetReference the sheet to apply
 * @param parameters the display parameters
 * @return the result of rendering the sheet in the context of the given document
 * @throws Exception if applying the sheet fails
 */
private XDOM applySheet(DocumentModelBridge document, DocumentReference sheetReference, DocumentDisplayerParameters parameters) throws Exception {
    DocumentModelBridge sheet = documentAccessBridge.getTranslatedDocumentInstance(sheetReference);
    if (parameters.isTitleDisplayed() && StringUtils.isEmpty(sheet.getTitle())) {
        // The sheet doesn't control the title. Fall back on the default document displayer.
        return null;
    }
    DocumentModelBridge originalSecurityDoc = this.modelBridge.setSecurityDocument(sheet);
    try {
        return display(document, sheet, parameters);
    } finally {
        this.modelBridge.setSecurityDocument(originalSecurityDoc);
    }
}
Also used : DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge)

Example 50 with DocumentModelBridge

use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.

the class DefaultSheetManagerTest method testSheetResolutionSequence.

/**
 * Tests the order in which sheets are determined: execution context, document sheets and finally class sheets.
 *
 * @throws Exception shouldn't happen, but some methods include "throws" in their signature
 */
@Test
public void testSheetResolutionSequence() throws Exception {
    final String contextSheetName = "ContextSheet";
    context.setProperty(SHEET_PROPERTY, contextSheetName);
    final DocumentReference documentSheetReference = new DocumentReference(WIKI_NAME, "ABC", "DocumentSheet");
    final DocumentReference classSheetReference = new DocumentReference(WIKI_NAME, "BlogCode", "BlogPostSheet");
    final DocumentReference classReference = new DocumentReference(WIKI_NAME, "Blog", "BlogPostClass");
    final DocumentModelBridge classDocument = getMockery().mock(DocumentModelBridge.class, "xclass");
    final String currentAction = "foo";
    final Sequence sheetResolutionSequence = getMockery().sequence("sheetResolutionSequence");
    getMockery().checking(new Expectations() {

        {
            // (1) Look for the sheet specified in the execution context.
            oneOf(documentAccessBridge).getCurrentDocumentReference();
            inSequence(sheetResolutionSequence);
            will(returnValue(document.getDocumentReference()));
            // The sheet is resolved relative to the target document.
            oneOf(documentAccessBridge).exists(new DocumentReference(document.getDocumentReference().getWikiReference().getName(), document.getDocumentReference().getLastSpaceReference().getName(), contextSheetName));
            inSequence(sheetResolutionSequence);
            will(returnValue(false));
            // (2) Look for the custom document sheets.
            oneOf(documentSheetBinder).getSheets(document);
            inSequence(sheetResolutionSequence);
            will(returnValue(Collections.singletonList(documentSheetReference)));
            oneOf(documentAccessBridge).exists(documentSheetReference);
            inSequence(sheetResolutionSequence);
            will(returnValue(true));
            oneOf(documentAccessBridge).getProperty(documentSheetReference, SHEET_CLASS_REFERENCE, ACTION_PROPERTY);
            inSequence(sheetResolutionSequence);
            will(returnValue("bar"));
            // (3) Look for the class sheets.
            oneOf(modelBridge).getXObjectClassReferences(document);
            inSequence(sheetResolutionSequence);
            will(returnValue(Collections.singleton(classReference)));
            oneOf(documentAccessBridge).getTranslatedDocumentInstance(classReference);
            inSequence(sheetResolutionSequence);
            will(returnValue(classDocument));
            oneOf(classSheetBinder).getSheets(classDocument);
            inSequence(sheetResolutionSequence);
            will(returnValue(Collections.singletonList(classSheetReference)));
            oneOf(documentAccessBridge).exists(classSheetReference);
            inSequence(sheetResolutionSequence);
            will(returnValue(true));
            oneOf(documentAccessBridge).getProperty(classSheetReference, SHEET_CLASS_REFERENCE, ACTION_PROPERTY);
            inSequence(sheetResolutionSequence);
            will(returnValue(currentAction));
        }
    });
    getMockedComponent().getSheets(document, currentAction);
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Sequence(org.jmock.Sequence) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

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