Search in sources :

Example 1 with MetadataBlockMatcher

use of org.xwiki.rendering.block.match.MetadataBlockMatcher 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 2 with MetadataBlockMatcher

use of org.xwiki.rendering.block.match.MetadataBlockMatcher 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 3 with MetadataBlockMatcher

use of org.xwiki.rendering.block.match.MetadataBlockMatcher 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)

Example 4 with MetadataBlockMatcher

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

the class CurrentMacroEntityReferenceResolver method resolve.

@Override
public EntityReference resolve(String representation, EntityType entityType, Object... parameters) {
    // There must one parameter and it must be of type Block
    if (parameters.length != 1 || !(parameters[0] instanceof Block)) {
        throw new IllegalArgumentException(String.format("You must pass one parameter of type [%s]", Block.class.getName()));
    }
    Block currentBlock = (Block) parameters[0];
    EntityReference result;
    MetaDataBlock metaDataBlock = currentBlock.getFirstBlock(new MetadataBlockMatcher(MetaData.BASE), Block.Axes.ANCESTOR);
    // If no Source MetaData was found resolve against the current entity as a failsafe solution.
    if (metaDataBlock == null) {
        result = this.currentEntityReferenceResolver.resolve(representation, entityType);
    } else {
        String sourceMetaData = (String) metaDataBlock.getMetaData().getMetaData(MetaData.BASE);
        result = this.currentEntityReferenceResolver.resolve(representation, entityType, this.currentEntityReferenceResolver.resolve(sourceMetaData, EntityType.DOCUMENT));
    }
    return result;
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) MetadataBlockMatcher(org.xwiki.rendering.block.match.MetadataBlockMatcher) Block(org.xwiki.rendering.block.Block) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 5 with MetadataBlockMatcher

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

the class CurrentSignedMacroBlockReferenceResolver method getSourceReference.

private EntityReference getSourceReference(Block block) {
    EntityReference sourceRef = null;
    MetaDataBlock metaDataBlock = block.getFirstBlock(new MetadataBlockMatcher(MetaData.SOURCE), Block.Axes.ANCESTOR);
    if (metaDataBlock != null) {
        sourceRef = sourceResolver.resolve((String) metaDataBlock.getMetaData().getMetaData(MetaData.SOURCE));
    }
    return sourceRef;
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) MetadataBlockMatcher(org.xwiki.rendering.block.match.MetadataBlockMatcher) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Aggregations

MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)5 MetadataBlockMatcher (org.xwiki.rendering.block.match.MetadataBlockMatcher)5 EntityReference (org.xwiki.model.reference.EntityReference)2 Block (org.xwiki.rendering.block.Block)2 Reader (java.io.Reader)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 HashMap (java.util.HashMap)1 ScriptContext (javax.script.ScriptContext)1