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