Search in sources :

Example 6 with MetaDataBlock

use of org.xwiki.rendering.block.MetaDataBlock in project xwiki-platform by xwiki.

the class NestedScriptMacroValidatorListener method extractSourceContentReference.

/**
 * @param source the blocks from where to try to extract the source content
 * @return the source content reference or null if none is found
 */
private String extractSourceContentReference(Block source) {
    String contentSource = null;
    MetaDataBlock metaDataBlock = source.getFirstBlock(new MetadataBlockMatcher(MetaData.SOURCE), Block.Axes.ANCESTOR);
    if (metaDataBlock != null) {
        contentSource = (String) metaDataBlock.getMetaData().getMetaData(MetaData.SOURCE);
    }
    return contentSource;
}
Also used : MetadataBlockMatcher(org.xwiki.rendering.block.match.MetadataBlockMatcher) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 7 with MetaDataBlock

use of org.xwiki.rendering.block.MetaDataBlock in project xwiki-platform by xwiki.

the class AbstractJSR223ScriptMacro method evaluateBlock.

/**
 * Execute provided script and return {@link Block} based result.
 *
 * @param engine the script engine to use to evaluate the script.
 * @param parameters the macro parameters.
 * @param content the script to execute.
 * @param context the context of the macro transformation.
 * @return the result of script execution.
 * @throws ScriptException failed to evaluate script
 * @throws MacroExecutionException failed to evaluate provided content.
 */
protected List<Block> evaluateBlock(ScriptEngine engine, P parameters, String content, MacroTransformationContext context) throws ScriptException, MacroExecutionException {
    List<Block> result;
    ScriptContext scriptContext = getScriptContext();
    Writer currentWriter = scriptContext.getWriter();
    Reader currentReader = scriptContext.getReader();
    Map<String, Object> currentEngineBindings = new HashMap<>(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE));
    // Set standard javax.script.filename property
    MetaDataBlock metaDataBlock = context.getCurrentMacroBlock().getFirstBlock(new MetadataBlockMatcher(MetaData.SOURCE), Axes.ANCESTOR_OR_SELF);
    if (metaDataBlock != null) {
        scriptContext.setAttribute(ScriptEngine.FILENAME, metaDataBlock.getMetaData().getMetaData(MetaData.SOURCE), ScriptContext.ENGINE_SCOPE);
    }
    try {
        StringWriter stringWriter = new StringWriter();
        // set writer in script context
        scriptContext.setWriter(stringWriter);
        Object scriptResult = eval(content, engine, scriptContext);
        result = convertScriptExecution(scriptResult, stringWriter, parameters, context);
    } finally {
        // restore current writer
        scriptContext.setWriter(currentWriter);
        // restore current reader
        scriptContext.setReader(currentReader);
        // restore "context" binding
        restoreBinding(currentEngineBindings, scriptContext, BINDING_CONTEXT);
        // restore "javax.script.filename" binding
        restoreBinding(currentEngineBindings, scriptContext, ScriptEngine.FILENAME);
    }
    return result;
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) MetadataBlockMatcher(org.xwiki.rendering.block.match.MetadataBlockMatcher) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) ScriptContext(javax.script.ScriptContext) Reader(java.io.Reader) StringWriter(java.io.StringWriter) Writer(java.io.Writer) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 8 with MetaDataBlock

use of org.xwiki.rendering.block.MetaDataBlock in project xwiki-platform by xwiki.

the class ContextMacro method execute.

@Override
public List<Block> execute(ContextMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    if (parameters.getDocument() == null) {
        throw new MacroExecutionException("You must specify a 'document' parameter pointing to the document to " + "set in the context as the current document.");
    }
    DocumentReference referencedDocReference = this.macroDocumentReferenceResolver.resolve(parameters.getDocument(), context.getCurrentMacroBlock());
    boolean currentContextHasProgrammingRights = this.documentAccessBridge.hasProgrammingRights();
    List<Block> result;
    try {
        Map<String, Object> backupObjects = new HashMap<>();
        try {
            this.documentAccessBridge.pushDocumentInContext(backupObjects, referencedDocReference);
            // error since it would be a security breach otherwise.
            if (this.documentAccessBridge.hasProgrammingRights() && !currentContextHasProgrammingRights) {
                throw new MacroExecutionException("Current document must have programming rights since the " + "context document provided [" + parameters.getDocument() + "] has programming rights.");
            }
            MetaData metadata = new MetaData();
            metadata.addMetaData(MetaData.SOURCE, parameters.getDocument());
            metadata.addMetaData(MetaData.BASE, parameters.getDocument());
            XDOM xdom = this.contentParser.parse(content, context, false, metadata, false);
            // Configure the  Transformation Context depending on the mode asked.
            if (parameters.getTransformationContext() == TransformationContextMode.DOCUMENT || parameters.getTransformationContext() == TransformationContextMode.TRANSFORMATIONS) {
                // Apply the transformations but with a Transformation Context having the XDOM of the passed
                // document so that macros execute on the passed document's XDOM (e.g. the TOC macro will generate
                // the toc for the passed document instead of the current document).
                DocumentModelBridge referencedDoc = this.documentAccessBridge.getTranslatedDocumentInstance(referencedDocReference);
                XDOM referencedXDOM = referencedDoc.getXDOM();
                if (parameters.getTransformationContext() == TransformationContextMode.TRANSFORMATIONS) {
                    // Get the XDOM from the referenced doc but with Transformations applied so that all macro are
                    // executed and contribute XDOM elements.
                    // IMPORTANT: This can be dangerous since it means executing macros, and thus also script macros
                    // defined in the referenced document. To be used with caution.
                    TransformationContext referencedTxContext = new TransformationContext(referencedXDOM, referencedDoc.getSyntax());
                    this.transformationManager.performTransformations(referencedXDOM, referencedTxContext);
                }
                // Now execute transformation on the context macro content but with the referenced XDOM in the
                // Transformation context!
                TransformationContext txContext = new TransformationContext(referencedXDOM, referencedDoc.getSyntax());
                this.transformationManager.performTransformations(xdom, txContext);
            }
            // Keep metadata so that the result stay associated to context properties when inserted in the parent
            // XDOM
            result = Arrays.asList((Block) new MetaDataBlock(xdom.getChildren(), xdom.getMetaData()));
        } finally {
            this.documentAccessBridge.popDocumentFromContext(backupObjects);
        }
    } catch (Exception e) {
        if (e instanceof MacroExecutionException) {
            throw (MacroExecutionException) e;
        } else {
            throw new MacroExecutionException(String.format("Failed to render page in the context of [%s]", referencedDocReference), e);
        }
    }
    return result;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MetaData(org.xwiki.rendering.listener.MetaData) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) DocumentReference(org.xwiki.model.reference.DocumentReference) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 9 with MetaDataBlock

use of org.xwiki.rendering.block.MetaDataBlock in project xwiki-platform by xwiki.

the class DocumentTableBlockDataSourceTest method isDefinedChartSourceTheCurrentDocumentWhenReferenceNotNullAndMatching.

@Test
public void isDefinedChartSourceTheCurrentDocumentWhenReferenceNotNullAndMatching() throws Exception {
    DocumentAccessBridge dab = this.componentManager.getInstance(DocumentAccessBridge.class);
    DocumentReference currentReference = new DocumentReference("currentwiki", "currentspace", "currentpage");
    when(dab.getCurrentDocumentReference()).thenReturn(currentReference);
    DocumentReferenceResolver<String> resolver = this.componentManager.getInstance(DocumentReferenceResolver.TYPE_STRING);
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    when(resolver.resolve("wiki:space.page", currentReference)).thenReturn(documentReference);
    MacroBlock currentMacroBlock = mock(MacroBlock.class);
    MetaDataBlock metaDataBlock = new MetaDataBlock(Collections.EMPTY_LIST, new MetaData(Collections.singletonMap(MetaData.SOURCE, (Object) "wiki:space.page")));
    when(currentMacroBlock.getFirstBlock(any(BlockMatcher.class), any(Block.Axes.class))).thenReturn(metaDataBlock);
    DocumentTableBlockDataSource source = this.componentManager.getComponentUnderTest();
    source.setParameter("document", "wiki:space.page");
    Assert.assertTrue(source.isDefinedChartSourceTheCurrentDocument(currentMacroBlock));
}
Also used : MetaData(org.xwiki.rendering.listener.MetaData) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) BlockMatcher(org.xwiki.rendering.block.match.BlockMatcher) MacroBlock(org.xwiki.rendering.block.MacroBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

Example 10 with MetaDataBlock

use of org.xwiki.rendering.block.MetaDataBlock in project xwiki-platform by xwiki.

the class DocumentTableBlockDataSource method extractSourceContentReference.

/**
 * @param source the blocks from where to try to extract the source content
 * @return the source content reference or null if none is found
 */
private String extractSourceContentReference(Block source) {
    String contentSource = null;
    MetaDataBlock metaDataBlock = source.getFirstBlock(new MetadataBlockMatcher(MetaData.SOURCE), Block.Axes.ANCESTOR);
    if (metaDataBlock != null) {
        contentSource = (String) metaDataBlock.getMetaData().getMetaData(MetaData.SOURCE);
    }
    return contentSource;
}
Also used : MetadataBlockMatcher(org.xwiki.rendering.block.match.MetadataBlockMatcher) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Aggregations

MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)17 Block (org.xwiki.rendering.block.Block)11 DocumentReference (org.xwiki.model.reference.DocumentReference)10 MacroBlock (org.xwiki.rendering.block.MacroBlock)8 Test (org.junit.Test)7 HashMap (java.util.HashMap)6 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)6 XDOM (org.xwiki.rendering.block.XDOM)6 MetadataBlockMatcher (org.xwiki.rendering.block.match.MetadataBlockMatcher)5 MetaData (org.xwiki.rendering.listener.MetaData)5 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)5 Expectations (org.jmock.Expectations)4 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)4 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)4 Map (java.util.Map)3 EntityReference (org.xwiki.model.reference.EntityReference)3 DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)2 AttachmentReference (org.xwiki.model.reference.AttachmentReference)2 ExpandedMacroBlock (org.xwiki.rendering.block.ExpandedMacroBlock)2 ImageBlock (org.xwiki.rendering.block.ImageBlock)2