Search in sources :

Example 1 with TransformationException

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());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) TransformationException(org.xwiki.rendering.transformation.TransformationException) XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) XWikiException(com.xpn.xwiki.XWikiException)

Example 2 with TransformationException

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);
        }
    }
}
Also used : TransformationException(org.xwiki.rendering.transformation.TransformationException) XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) Map(java.util.Map) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) TransformationException(org.xwiki.rendering.transformation.TransformationException) WikiComponentRuntimeException(org.xwiki.component.wiki.WikiComponentRuntimeException) ConversionException(org.xwiki.properties.converter.ConversionException) WikiComponentRuntimeException(org.xwiki.component.wiki.WikiComponentRuntimeException) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext)

Example 3 with TransformationException

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());
    }
}
Also used : Transformation(org.xwiki.rendering.transformation.Transformation) TransformationException(org.xwiki.rendering.transformation.TransformationException) XDOM(org.xwiki.rendering.block.XDOM) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) ContentParser(org.xwiki.rendering.parser.ContentParser) ContentExecutorException(org.xwiki.rendering.executor.ContentExecutorException) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) Test(org.junit.Test)

Example 4 with TransformationException

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;
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) TransformationException(org.xwiki.rendering.transformation.TransformationException) XDOM(org.xwiki.rendering.block.XDOM) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) Block(org.xwiki.rendering.block.Block) TransformationContext(org.xwiki.rendering.transformation.TransformationContext)

Aggregations

XDOM (org.xwiki.rendering.block.XDOM)4 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)4 TransformationException (org.xwiki.rendering.transformation.TransformationException)4 HashMap (java.util.HashMap)2 MutableRenderingContext (org.xwiki.rendering.internal.transformation.MutableRenderingContext)2 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 Map (java.util.Map)1 Test (org.junit.Test)1 WikiComponentRuntimeException (org.xwiki.component.wiki.WikiComponentRuntimeException)1 ConversionException (org.xwiki.properties.converter.ConversionException)1 Block (org.xwiki.rendering.block.Block)1 CompositeBlock (org.xwiki.rendering.block.CompositeBlock)1 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)1 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)1 ContentExecutorException (org.xwiki.rendering.executor.ContentExecutorException)1 ContentParser (org.xwiki.rendering.parser.ContentParser)1 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)1 Transformation (org.xwiki.rendering.transformation.Transformation)1