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);
}
}
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));
}
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);
}
}
}
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);
}
}
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);
}
Aggregations