Search in sources :

Example 6 with DocumentDisplayerParameters

use of org.xwiki.display.internal.DocumentDisplayerParameters in project xwiki-platform by xwiki.

the class DefaultIOTargetService method getDocumentXDOM.

private XDOM getDocumentXDOM(DocumentReference reference) throws Exception {
    DocumentDisplayerParameters parameters = new DocumentDisplayerParameters();
    parameters.setExecutionContextIsolated(true);
    parameters.setContentTranslated(true);
    parameters.setTargetSyntax(renderingContext.getTargetSyntax());
    return documentDisplayer.display(dab.getDocumentInstance(reference), parameters);
}
Also used : DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters)

Example 7 with DocumentDisplayerParameters

use of org.xwiki.display.internal.DocumentDisplayerParameters in project xwiki-platform by xwiki.

the class DisplayScriptService method document.

/**
 * Displays a document.
 *
 * @param document the document to display
 * @param parameters the display parameters
 * @return the result of displaying the given document
 */
private String document(Document document, Map<String, Object> parameters, Syntax outputSyntax) {
    DocumentDisplayerParameters displayerParameters = (DocumentDisplayerParameters) parameters.get(DISPLAYER_PARAMETERS_KEY);
    if (displayerParameters == null) {
        displayerParameters = new DocumentDisplayerParameters();
        displayerParameters.setTargetSyntax(outputSyntax);
    }
    String displayerHint = (String) parameters.get("displayerHint");
    if (displayerHint == null) {
        displayerHint = "configured";
    }
    try {
        DocumentDisplayer displayer = this.componentManager.getInstance(DocumentDisplayer.class, displayerHint);
        return renderXDOM(displayer.display(getDocument(document), displayerParameters), outputSyntax);
    } catch (Exception e) {
        this.logger.error("Failed to display document [{}].", document.getPrefixedFullName(), e);
        return null;
    }
}
Also used : DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) DocumentDisplayer(org.xwiki.display.internal.DocumentDisplayer) XWikiException(com.xpn.xwiki.XWikiException)

Example 8 with DocumentDisplayerParameters

use of org.xwiki.display.internal.DocumentDisplayerParameters in project xwiki-platform by xwiki.

the class SheetDocumentDisplayerTest method testPreserveSheetPRWhenDocumentIsNotOnContext.

/**
 * Tests if the programming rights of the sheet are preserved when the document is not on the context.
 *
 * @throws Exception if something wrong happens
 */
@Test
public void testPreserveSheetPRWhenDocumentIsNotOnContext() throws Exception {
    DocumentModelBridge document = mockDocument(DOCUMENT_REFERENCE);
    DocumentModelBridge sheet = mockDocument(SHEET_REFERENCE);
    // We test that the displayed document is put on the context even if the current document is just a different
    // instance of the displayed document. This is needed because the displayed document can have unsaved changes.
    setCurrentDocument(mockDocument(DOCUMENT_REFERENCE));
    // The sheet must be determined and displayed in a new execution context that has the target document as
    // the current document.
    Map<String, Object> backupObjects = new HashMap<String, Object>();
    when(this.modelBridge.pushDocumentInContext(document)).thenReturn(backupObjects);
    SheetManager sheetManager = this.mocker.getInstance(SheetManager.class);
    when(sheetManager.getSheets(document, "view")).thenReturn(Collections.singletonList(SHEET_REFERENCE));
    DocumentModelBridge originalSecurityDoc = mock(DocumentModelBridge.class, "sdoc");
    // Required in order to preserve the programming rights of the sheet.
    when(this.modelBridge.setSecurityDocument(sheet)).thenReturn(originalSecurityDoc);
    XDOM output = new XDOM(Collections.<Block>emptyList());
    DocumentDisplayer documentDisplayer = this.mocker.getInstance(DocumentDisplayer.class);
    when(documentDisplayer.display(eq(sheet), any(DocumentDisplayerParameters.class))).thenReturn(output);
    assertSame(output, this.mocker.getComponentUnderTest().display(document, new DocumentDisplayerParameters()));
    // The security document must be reverted.
    verify(this.modelBridge).setSecurityDocument(originalSecurityDoc);
    // The previous execution context must be restored.
    verify(this.documentAccessBridge).popDocumentFromContext(backupObjects);
}
Also used : DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) DocumentDisplayer(org.xwiki.display.internal.DocumentDisplayer) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) HashMap(java.util.HashMap) SheetManager(org.xwiki.sheet.SheetManager) Test(org.junit.Test)

Example 9 with DocumentDisplayerParameters

use of org.xwiki.display.internal.DocumentDisplayerParameters in project xwiki-platform by xwiki.

the class SheetDocumentDisplayerTest method testPreserveSheetPRWhenDocumentIsOnContext.

/**
 * Tests if the programming rights of the sheet are preserved when the document is already on the context.
 *
 * @throws Exception if something wrong happens
 */
@Test
public void testPreserveSheetPRWhenDocumentIsOnContext() throws Exception {
    DocumentModelBridge document = mockDocument(DOCUMENT_REFERENCE);
    DocumentModelBridge sheet = mockDocument(SHEET_REFERENCE);
    setCurrentDocument(document);
    SheetManager sheetManager = this.mocker.getInstance(SheetManager.class);
    when(sheetManager.getSheets(document, "view")).thenReturn(Collections.singletonList(SHEET_REFERENCE));
    DocumentModelBridge originalSecurityDoc = mock(DocumentModelBridge.class, "sdoc");
    // Required in order to preserve the programming rights of the sheet.
    when(this.modelBridge.setSecurityDocument(sheet)).thenReturn(originalSecurityDoc);
    XDOM output = new XDOM(Collections.<Block>emptyList());
    DocumentDisplayer documentDisplayer = this.mocker.getInstance(DocumentDisplayer.class);
    when(documentDisplayer.display(eq(sheet), any(DocumentDisplayerParameters.class))).thenReturn(output);
    assertSame(output, this.mocker.getComponentUnderTest().display(document, new DocumentDisplayerParameters()));
    // The security document must be reverted.
    verify(this.modelBridge).setSecurityDocument(originalSecurityDoc);
}
Also used : DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) DocumentDisplayer(org.xwiki.display.internal.DocumentDisplayer) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) SheetManager(org.xwiki.sheet.SheetManager) Test(org.junit.Test)

Example 10 with DocumentDisplayerParameters

use of org.xwiki.display.internal.DocumentDisplayerParameters in project xwiki-platform by xwiki.

the class XWikiDocument method getRenderedTitle.

/**
 * Get the rendered version of the document title. The title is extracted and then Velocity is applied on it and
 * it's then rendered using the passed Syntax. The following logic is used to extract the title:
 * <ul>
 * <li>If a Sheet is specified for the document and this Sheet document contains a non empty title then it's
 * used</li>
 * <li>If not and the document's title is specified then it's used</li>
 * <li>If not and if the title compatibility mode is turned on ({@code xwiki.title.compatibility=1} in
 * {@code xwiki.cfg}) then an attempt is made to extract the title from the first heading found in the document's
 * content</li>
 * <li>If not, then at last resort the page name is returned</li>
 * </ul>
 *
 * @param outputSyntax the syntax to render to; this is not taken into account for XWiki 1.0 syntax
 * @param context the XWiki context
 * @return the rendered version of the document title
 */
public String getRenderedTitle(Syntax outputSyntax, XWikiContext context) {
    DocumentDisplayerParameters parameters = new DocumentDisplayerParameters();
    parameters.setTitleDisplayed(true);
    parameters.setExecutionContextIsolated(true);
    parameters.setTargetSyntax(outputSyntax);
    try {
        XDOM titleXDOM = getDocumentDisplayer().display(this, parameters);
        return renderXDOM(titleXDOM, outputSyntax);
    } catch (Exception e) {
        // We've failed to extract the Document's title or to render it. We log an error but we use the page name
        // as the returned title in order to not generate errors in lots of places in the wiki (e.g. Activity
        // Stream, menus, etc). The title is used in a lots of places...
        LOGGER.error("Failed to render title for [{}]", getDocumentReference(), e);
        return getDocumentReference().getName();
    }
}
Also used : DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) XDOM(org.xwiki.rendering.block.XDOM) XWikiException(com.xpn.xwiki.XWikiException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DifferentiationFailedException(org.suigeneris.jrcs.diff.DifferentiationFailedException) ExecutionContextException(org.xwiki.context.ExecutionContextException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MissingParserException(org.xwiki.rendering.parser.MissingParserException) IOException(java.io.IOException) ParseException(org.xwiki.rendering.parser.ParseException) TransformationException(org.xwiki.rendering.transformation.TransformationException) QueryException(org.xwiki.query.QueryException)

Aggregations

DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)11 XDOM (org.xwiki.rendering.block.XDOM)7 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)5 XWikiException (com.xpn.xwiki.XWikiException)3 HashMap (java.util.HashMap)3 DocumentDisplayer (org.xwiki.display.internal.DocumentDisplayer)3 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 Test (org.junit.Test)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 Block (org.xwiki.rendering.block.Block)2 MacroBlock (org.xwiki.rendering.block.MacroBlock)2 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)2 Syntax (org.xwiki.rendering.syntax.Syntax)2 SheetManager (org.xwiki.sheet.SheetManager)2 IOException (java.io.IOException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 DifferentiationFailedException (org.suigeneris.jrcs.diff.DifferentiationFailedException)1 ToString (org.suigeneris.jrcs.util.ToString)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1