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