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