use of org.xwiki.rendering.transformation.TransformationException in project xwiki-platform by xwiki.
the class WikiUIExtensionRenderer method execute.
/**
* @return the rendered content of the extension
*/
public CompositeBlock execute() {
// We need to clone the xdom to avoid transforming the original and make it useless after the first
// transformation
XDOM transformedXDOM = xdom.clone();
// Perform macro transformations.
try {
// Get the document holding the UIX and put it in the UIX context
XWikiDocument xdoc = getXWikiContext().getWiki().getDocument(documentReference, getXWikiContext());
Map<String, Object> uixContext = new HashMap<>();
uixContext.put(WikiUIExtension.CONTEXT_UIX_DOC_KEY, xdoc.newDocument(getXWikiContext()));
// Put the UIX context in the XWiki context
getXWikiContext().put(WikiUIExtension.CONTEXT_UIX_KEY, uixContext);
// Transform the macros
TransformationContext transformationContext = new TransformationContext(xdom, xdoc.getSyntax());
transformationContext.setId(roleHint);
((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, transformationContext, transformedXDOM);
} catch (TransformationException e) {
LOGGER.warn("Error while executing wiki component macro transformation for extension [{}]", roleHint);
} catch (XWikiException ex) {
LOGGER.warn("Failed to retrieve document [{}]", documentReference);
}
return new CompositeBlock(transformedXDOM.getChildren());
}
use of org.xwiki.rendering.transformation.TransformationException in project xwiki-platform by xwiki.
the class DefaultWikiComponentMethodExecutor method execute.
@Override
public Object execute(Method method, Object[] args, DocumentReference componentDocumentReference, XDOM xdom, Syntax syntax, Map<String, Object> methodContext) throws WikiComponentRuntimeException {
// Prepare and put the method context in the XWiki Context
Map<Object, Object> xwikiContext = (Map<Object, Object>) execution.getContext().getProperty("xwikicontext");
this.prepareMethodContext(methodContext, args);
xwikiContext.put("method", methodContext);
// Save current context document, to put it back after the execution.
Object contextDoc = xwikiContext.get(XWIKI_CONTEXT_DOC_KEY);
try {
// component document and not the context one.
try {
xwikiContext.put(XWIKI_CONTEXT_DOC_KEY, dab.getDocumentInstance(componentDocumentReference));
} catch (Exception e) {
throw new WikiComponentRuntimeException(String.format("Failed to load wiki component document [%s]", componentDocumentReference), e);
}
// We need to clone the xdom to avoid transforming the original and make it useless after the first
// transformation
XDOM transformedXDOM = xdom.clone();
// Perform internal macro transformations
try {
TransformationContext transformationContext = new TransformationContext(transformedXDOM, syntax);
transformationContext.setId(method.getClass().getName() + "#" + method.getName());
((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, transformationContext, transformedXDOM);
} catch (TransformationException e) {
throw new WikiComponentRuntimeException(String.format("Error while executing wiki component macro transformation for method [%s]", method.getName()), e);
}
if (!method.getReturnType().getName().equals("void")) {
if (methodContext.get(OUTPUT_KEY) != null && ((WikiMethodOutputHandler) methodContext.get(OUTPUT_KEY)).getValue() != null) {
return method.getReturnType().cast(((WikiMethodOutputHandler) methodContext.get(OUTPUT_KEY)).getValue());
} else {
return this.castRenderedContent(transformedXDOM, method);
}
} else {
return null;
}
} finally {
if (contextDoc != null) {
xwikiContext.put(XWIKI_CONTEXT_DOC_KEY, contextDoc);
}
}
}
use of org.xwiki.rendering.transformation.TransformationException in project xwiki-platform by xwiki.
the class MacroContentExecutorTest method executeWhenTransformationException.
@Test
public void executeWhenTransformationException() throws Exception {
XDOM parsedBlocks = new XDOM(Collections.emptyList());
ContentParser contentParser = this.mocker.getInstance(ContentParser.class);
when(contentParser.parse("", Syntax.PLAIN_1_0)).thenReturn(parsedBlocks);
TransformationContext transformationContext = new TransformationContext();
MacroTransformationContext context = new MacroTransformationContext(transformationContext);
Transformation macroTransformation = this.mocker.getInstance(Transformation.class, "macro");
doThrow(new TransformationException("error")).when(macroTransformation).transform(parsedBlocks, transformationContext);
try {
this.mocker.getComponentUnderTest().execute("", Syntax.PLAIN_1_0, context);
fail("Should have raised a ContentExecutorException");
} catch (ContentExecutorException expected) {
assertEquals("Failed to execute content", expected.getMessage());
}
}
use of org.xwiki.rendering.transformation.TransformationException in project xwiki-platform by xwiki.
the class DocumentTitleDisplayer method extractTitleFromContent.
@Override
protected XDOM extractTitleFromContent(DocumentModelBridge document, DocumentDisplayerParameters parameters) {
// Note: Ideally we should apply transformations on the document's returned XDOM here since macros could
// generate headings for example or some other transformations could modify headings. However we don't do this
// at the moment since it would be too costly to do so. In the future we will even probably remove the feature
// of generating the title from the content.
List<HeaderBlock> blocks = document.getXDOM().getBlocks(new ClassBlockMatcher(HeaderBlock.class), Block.Axes.DESCENDANT);
if (!blocks.isEmpty()) {
HeaderBlock heading = blocks.get(0);
// Check the heading depth after which we should return null if no heading was found.
if (heading.getLevel().getAsInt() <= displayConfiguration.getTitleHeadingDepth()) {
XDOM headingXDOM = new XDOM(Collections.<Block>singletonList(heading));
try {
TransformationContext txContext = new TransformationContext(headingXDOM, document.getSyntax(), parameters.isTransformationContextRestricted());
txContext.setTargetSyntax(parameters.getTargetSyntax());
transformationManager.performTransformations(headingXDOM, txContext);
Block headingBlock = headingXDOM.getChildren().size() > 0 ? headingXDOM.getChildren().get(0) : null;
if (headingBlock instanceof HeaderBlock) {
return new XDOM(headingBlock.getChildren());
}
} catch (TransformationException e) {
getLogger().warn("Failed to extract title from document content.");
}
}
}
return null;
}
Aggregations