Search in sources :

Example 1 with CompositeBlock

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

the class HelloWorldUIExtension method execute.

@Override
public Block execute() {
    List<Block> blocks = new ArrayList<Block>();
    blocks.add(new WordBlock("HelloWorld"));
    return new CompositeBlock(blocks);
}
Also used : WordBlock(org.xwiki.rendering.block.WordBlock) ArrayList(java.util.ArrayList) Block(org.xwiki.rendering.block.Block) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) WordBlock(org.xwiki.rendering.block.WordBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock)

Example 2 with CompositeBlock

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

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

the class MessageToolTranslationMessageParserTest method messageWithEscapedParameter.

@Test
public void messageWithEscapedParameter() throws ComponentLookupException {
    TranslationMessage translationMessage = getMockedComponent().parse("'{0}");
    Assert.assertEquals("'{0}", translationMessage.getRawSource());
    Assert.assertEquals(new CompositeBlock(Arrays.<Block>asList(new SpecialSymbolBlock('{'), new WordBlock("0"), new SpecialSymbolBlock('}'))), translationMessage.render(null, null, "word"));
}
Also used : SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) WordBlock(org.xwiki.rendering.block.WordBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) TranslationMessage(org.xwiki.localization.message.TranslationMessage) Test(org.junit.Test)

Example 4 with CompositeBlock

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

the class MessageToolTranslationMessageParserTest method messageEmpty.

@Test
public void messageEmpty() throws ComponentLookupException {
    TranslationMessage translationMessage = getMockedComponent().parse("");
    Assert.assertEquals("", translationMessage.getRawSource());
    Assert.assertEquals(new CompositeBlock(), translationMessage.render(null, null));
}
Also used : CompositeBlock(org.xwiki.rendering.block.CompositeBlock) TranslationMessage(org.xwiki.localization.message.TranslationMessage) Test(org.junit.Test)

Example 5 with CompositeBlock

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

the class ParameterTranslationMessageElement method render.

@Override
public Block render(Locale locale, Collection<TranslationBundle> bundles, Object... parameters) {
    Object parameter = parameters[index];
    Block block;
    if (parameter instanceof Block) {
        block = (Block) parameter;
    } else if (parameter != null) {
        try {
            XDOM xdom = this.plainParser.parse(new StringReader(parameter.toString()));
            List<Block> blocks = xdom.getChildren();
            PARSERUTILS.removeTopLevelParagraph(blocks);
            if (blocks.isEmpty()) {
                block = null;
            } else if (blocks.size() == 1) {
                block = blocks.get(0);
            } else {
                block = new CompositeBlock(blocks);
            }
        } catch (ParseException e) {
            // make the #render fail instead ?
            block = null;
        }
    } else {
        block = null;
    }
    return block;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) Block(org.xwiki.rendering.block.Block) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) List(java.util.List) ParseException(org.xwiki.rendering.parser.ParseException)

Aggregations

CompositeBlock (org.xwiki.rendering.block.CompositeBlock)9 Block (org.xwiki.rendering.block.Block)6 StringReader (java.io.StringReader)3 Test (org.junit.Test)3 TranslationMessage (org.xwiki.localization.message.TranslationMessage)3 WordBlock (org.xwiki.rendering.block.WordBlock)3 XDOM (org.xwiki.rendering.block.XDOM)3 ParseException (org.xwiki.rendering.parser.ParseException)3 SpecialSymbolBlock (org.xwiki.rendering.block.SpecialSymbolBlock)2 MutableRenderingContext (org.xwiki.rendering.internal.transformation.MutableRenderingContext)2 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)2 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 Translation (org.xwiki.localization.Translation)1 GroupBlock (org.xwiki.rendering.block.GroupBlock)1