Search in sources :

Example 1 with DocumentDisplayerParameters

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

the class DisplayScriptService method title.

/**
 * Displays the document title. If a title has not been provided through the title field, it looks for a section
 * title in the document's content and if not found return the page name. The returned title is also interpreted
 * which means it's allowed to use Velocity, Groovy, etc. syntax within a title.
 *
 * @param document the document whose title is displayed
 * @param parameters the display parameters
 * @return the result of displaying the title of the given document
 */
public String title(Document document, Map<String, Object> parameters) {
    Map<String, Object> actualParameters = new HashMap<String, Object>(parameters);
    DocumentDisplayerParameters displayerParameters = (DocumentDisplayerParameters) parameters.get(DISPLAYER_PARAMETERS_KEY);
    if (displayerParameters == null) {
        displayerParameters = new DocumentDisplayerParameters();
        // Default title display parameters.
        displayerParameters.setExecutionContextIsolated(true);
    } else if (!displayerParameters.isTitleDisplayed()) {
        // Clone because we have to enforce title display.
        displayerParameters = displayerParameters.clone();
    }
    // Ensure the title is displayed.
    displayerParameters.setTitleDisplayed(true);
    Syntax outputSyntax = getOutputSyntax(parameters);
    displayerParameters.setTargetSyntax(outputSyntax);
    actualParameters.put(DISPLAYER_PARAMETERS_KEY, displayerParameters);
    return document(document, actualParameters, outputSyntax);
}
Also used : DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) HashMap(java.util.HashMap) Syntax(org.xwiki.rendering.syntax.Syntax)

Example 2 with DocumentDisplayerParameters

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

the class DisplayScriptService method content.

/**
 * Displays the content of the given document.
 *
 * @param document the document whose content is displayed
 * @param parameters the display parameters
 * @return the result of rendering the content of the given document using the provided parameters
 */
public String content(Document document, Map<String, Object> parameters) {
    XWikiContext context = getXWikiContext();
    String content = null;
    try {
        content = document.getTranslatedContent();
    } catch (XWikiException e) {
        this.logger.warn("Failed to get the translated content of document [{}].", document.getPrefixedFullName(), e);
        return null;
    }
    String renderedContent = this.renderingCache.getRenderedContent(document.getDocumentReference(), content, context);
    if (renderedContent == null) {
        Map<String, Object> actualParameters = new HashMap<String, Object>(parameters);
        DocumentDisplayerParameters displayerParameters = (DocumentDisplayerParameters) parameters.get(DISPLAYER_PARAMETERS_KEY);
        if (displayerParameters == null) {
            displayerParameters = new DocumentDisplayerParameters();
            // Default content display parameters.
            displayerParameters.setExecutionContextIsolated(true);
            displayerParameters.setContentTranslated(true);
        } else if (displayerParameters.isTitleDisplayed()) {
            // Clone because we have to enforce content display.
            displayerParameters = displayerParameters.clone();
        }
        // Ensure the content is displayed.
        displayerParameters.setTitleDisplayed(false);
        Syntax outputSyntax = getOutputSyntax(parameters);
        displayerParameters.setTargetSyntax(outputSyntax);
        actualParameters.put(DISPLAYER_PARAMETERS_KEY, displayerParameters);
        renderedContent = document(document, actualParameters, outputSyntax);
        if (renderedContent != null) {
            this.renderingCache.setRenderedContent(document.getDocumentReference(), content, renderedContent, context);
        }
    }
    return renderedContent;
}
Also used : DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) HashMap(java.util.HashMap) XWikiContext(com.xpn.xwiki.XWikiContext) Syntax(org.xwiki.rendering.syntax.Syntax) XWikiException(com.xpn.xwiki.XWikiException)

Example 3 with DocumentDisplayerParameters

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

the class IncludeMacro method execute.

@Override
public List<Block> execute(IncludeMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    // Step 1: Perform checks.
    if (parameters.getReference() == null) {
        throw new MacroExecutionException("You must specify a 'reference' parameter pointing to the entity to include.");
    }
    DocumentReference includedReference = resolve(context.getCurrentMacroBlock(), parameters);
    checkRecursiveInclusion(context.getCurrentMacroBlock(), includedReference);
    if (!this.documentAccessBridge.isDocumentViewable(includedReference)) {
        throw new MacroExecutionException(String.format("Current user [%s] doesn't have view rights on document [%s]", this.documentAccessBridge.getCurrentUserReference(), this.defaultEntityReferenceSerializer.serialize(includedReference)));
    }
    Context parametersContext = parameters.getContext();
    // Step 2: Retrieve the included document.
    DocumentModelBridge documentBridge;
    try {
        documentBridge = this.documentAccessBridge.getDocumentInstance(includedReference);
    } catch (Exception e) {
        throw new MacroExecutionException("Failed to load Document [" + this.defaultEntityReferenceSerializer.serialize(includedReference) + "]", e);
    }
    // Step 3: Display the content of the included document.
    // Check the value of the "context" parameter.
    // 
    // If CONTEXT_NEW then display the content in an isolated execution and transformation context.
    // 
    // if CONTEXT_CURRENT then display the content without performing any transformations (we don't want any Macro
    // to be executed at this stage since they should be executed by the currently running Macro Transformation.
    DocumentDisplayerParameters displayParameters = new DocumentDisplayerParameters();
    displayParameters.setContentTransformed(parametersContext == Context.NEW);
    displayParameters.setExecutionContextIsolated(displayParameters.isContentTransformed());
    displayParameters.setSectionId(parameters.getSection());
    displayParameters.setTransformationContextIsolated(displayParameters.isContentTransformed());
    displayParameters.setTransformationContextRestricted(context.getTransformationContext().isRestricted());
    displayParameters.setTargetSyntax(context.getTransformationContext().getTargetSyntax());
    displayParameters.setContentTranslated(true);
    Stack<Object> references = this.inclusionsBeingExecuted.get();
    if (parametersContext == Context.NEW) {
        if (references == null) {
            references = new Stack<Object>();
            this.inclusionsBeingExecuted.set(references);
        }
        references.push(includedReference);
    }
    XDOM result;
    try {
        result = this.documentDisplayer.display(documentBridge, displayParameters);
    } catch (Exception e) {
        throw new MacroExecutionException(e.getMessage(), e);
    } finally {
        if (parametersContext == Context.NEW) {
            references.pop();
        }
    }
    // Step 4: Wrap Blocks in a MetaDataBlock with the "source" meta data specified so that we know from where the
    // content comes and "base" meta data so that reference are properly resolved
    MetaDataBlock metadata = new MetaDataBlock(result.getChildren(), result.getMetaData());
    String source = this.defaultEntityReferenceSerializer.serialize(includedReference);
    metadata.getMetaData().addMetaData(MetaData.SOURCE, source);
    if (parametersContext == Context.NEW) {
        metadata.getMetaData().addMetaData(MetaData.BASE, source);
    }
    return Arrays.<Block>asList(metadata);
}
Also used : MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) Context(org.xwiki.rendering.macro.include.IncludeMacroParameters.Context) DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Block(org.xwiki.rendering.block.Block) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MacroBlock(org.xwiki.rendering.block.MacroBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 4 with DocumentDisplayerParameters

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

the class DisplayMacro method execute.

@Override
public List<Block> execute(DisplayMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    // Step 1: Perform checks.
    if (parameters.getReference() == null) {
        throw new MacroExecutionException("You must specify a 'reference' parameter pointing to the entity to display.");
    }
    DocumentReference includedReference = resolve(context.getCurrentMacroBlock(), parameters);
    checkRecursiveDisplay(context.getCurrentMacroBlock(), includedReference);
    if (!this.documentAccessBridge.isDocumentViewable(includedReference)) {
        throw new MacroExecutionException("Current user [" + this.documentAccessBridge.getCurrentUserReference() + "] doesn't have view rights on document [" + this.defaultEntityReferenceSerializer.serialize(includedReference) + "]");
    }
    // Step 2: Retrieve the included document.
    DocumentModelBridge documentBridge;
    try {
        documentBridge = this.documentAccessBridge.getDocumentInstance(includedReference);
    } catch (Exception e) {
        throw new MacroExecutionException("Failed to load Document [" + this.defaultEntityReferenceSerializer.serialize(includedReference) + "]", e);
    }
    // Step 3: Display the content of the included document.
    // Display the content in an isolated execution and transformation context.
    DocumentDisplayerParameters displayParameters = new DocumentDisplayerParameters();
    displayParameters.setContentTransformed(true);
    displayParameters.setExecutionContextIsolated(displayParameters.isContentTransformed());
    displayParameters.setSectionId(parameters.getSection());
    displayParameters.setTransformationContextIsolated(displayParameters.isContentTransformed());
    displayParameters.setTargetSyntax(context.getTransformationContext().getTargetSyntax());
    displayParameters.setContentTranslated(true);
    Stack<Object> references = this.displaysBeingExecuted.get();
    if (references == null) {
        references = new Stack<Object>();
        this.displaysBeingExecuted.set(references);
    }
    references.push(includedReference);
    XDOM result;
    try {
        result = this.documentDisplayer.display(documentBridge, displayParameters);
    } catch (Exception e) {
        throw new MacroExecutionException(e.getMessage(), e);
    } finally {
        references.pop();
    }
    // Step 4: Wrap Blocks in a MetaDataBlock with the "source" meta data specified so that we know from where the
    // content comes and "base" meta data so that reference are properly resolved
    MetaDataBlock metadata = new MetaDataBlock(result.getChildren(), result.getMetaData());
    String source = this.defaultEntityReferenceSerializer.serialize(includedReference);
    metadata.getMetaData().addMetaData(MetaData.SOURCE, source);
    metadata.getMetaData().addMetaData(MetaData.BASE, source);
    return Arrays.<Block>asList(metadata);
}
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) MacroBlock(org.xwiki.rendering.block.MacroBlock) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 5 with DocumentDisplayerParameters

use of org.xwiki.display.internal.DocumentDisplayerParameters 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)

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