Search in sources :

Example 6 with CompositeBlock

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

the class MessageFormatTranslationMessage method render.

@Override
public Block render(Locale locale, Collection<TranslationBundle> bundles, Object... parameters) {
    // Directly return cache if any available
    if (parameters.length == 0 && this.noParamCache != null) {
        return this.noParamCache.clone();
    }
    // Format the message
    String result;
    if (parameters.length > 0) {
        try {
            result = MessageFormat.format(this.message, parameters);
        } catch (IllegalArgumentException e) {
            // TODO: log the error ?
            result = this.message;
        }
    } else {
        result = this.message;
    }
    // Parse it to rendering blocks
    Block block;
    try {
        List<Block> blocks = this.plainParser.parse(new StringReader(result)).getChildren();
        PARSERUTILS.removeTopLevelParagraph(blocks);
        if (blocks.size() == 0) {
            block = new CompositeBlock();
        } else if (blocks.size() == 1) {
            block = blocks.get(0);
        } else {
            block = new CompositeBlock(blocks);
        }
        // Store cache of the message if there is no parameters
        if (parameters.length == 0) {
            this.noParamCache = block.clone();
        }
    } catch (ParseException e) {
        // Should never happen since plain text parser cannot fail
        block = null;
    }
    return block;
}
Also used : StringReader(java.io.StringReader) Block(org.xwiki.rendering.block.Block) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) ParseException(org.xwiki.rendering.parser.ParseException)

Example 7 with CompositeBlock

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

the class MessageToolTranslationMessageParserTest method messageWithExpectedParameter.

@Test
public void messageWithExpectedParameter() 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));
}
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 8 with CompositeBlock

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

the class TranslationMacro method execute.

@Override
public List<Block> execute(TranslationMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    Locale locale = parameters.getLocale() != null ? parameters.getLocale() : this.localizationContext.getCurrentLocale();
    Translation translation = this.localization.getTranslation(parameters.getKey(), locale);
    List<Block> blocks;
    if (translation != null) {
        Block block = parameters.getParameters() != null ? translation.render(locale, (Object[]) parameters.getParameters()) : translation.render(locale);
        if (block instanceof CompositeBlock) {
            blocks = block.getChildren();
        } else {
            blocks = Arrays.asList(block);
        }
        if (!context.getCurrentMacroBlock().isInline()) {
            // Make the content standalone
            blocks = Arrays.<Block>asList(new GroupBlock(blocks));
        }
    } else if (content != null) {
        blocks = this.macroContentParser.parse(content, context, false, context.getCurrentMacroBlock().isInline()).getChildren();
    } else {
        try {
            blocks = this.plainParser.parse(new StringReader(parameters.getKey())).getChildren();
            if (context.getCurrentMacroBlock().isInline()) {
                PARSERUTILS.removeTopLevelParagraph(blocks);
            }
        } catch (ParseException e) {
            throw new MacroExecutionException("Failed to parse key [" + parameters.getKey() + "]", e);
        }
    }
    return blocks;
}
Also used : Locale(java.util.Locale) Translation(org.xwiki.localization.Translation) GroupBlock(org.xwiki.rendering.block.GroupBlock) StringReader(java.io.StringReader) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) Block(org.xwiki.rendering.block.Block) GroupBlock(org.xwiki.rendering.block.GroupBlock) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) ParseException(org.xwiki.rendering.parser.ParseException)

Example 9 with CompositeBlock

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

the class PanelWikiUIExtension method execute.

@Override
public Block execute() {
    // We need to clone the xdom to avoid transforming the original and make it useless after the first
    // transformation
    final XDOM transformedXDOM = this.xdom.clone();
    this.progress.startStep(getDocumentReference(), "panel.progress.execute", "Execute panel [{}]", getDocumentReference());
    // Perform panel transformations with the right of the panel author
    try {
        this.authorExecutor.call(() -> {
            TransformationContext transformationContext = new TransformationContext(transformedXDOM, syntax);
            transformationContext.setId(getRoleHint());
            ((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, transformationContext, transformedXDOM);
            return null;
        }, getAuthorReference());
    } catch (Exception e) {
        LOGGER.error("Error while executing transformation for panel [{}]", this.panelReference);
    } finally {
        this.progress.endStep(getDocumentReference());
    }
    return new CompositeBlock(transformedXDOM.getChildren());
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) CompositeBlock(org.xwiki.rendering.block.CompositeBlock) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

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