use of org.xwiki.rendering.block.GroupBlock in project xwiki-platform by xwiki.
the class RssMacro method generateEntries.
/**
* Renders the given RSS's entries.
*
* @param parentBlock the parent Block to which the output is going to be added
* @param feed the RSS Channel we retrieved via the Feed URL
* @param parameters our parameter helper object
* @throws MacroExecutionException if the content cannot be rendered
*/
private void generateEntries(Block parentBlock, SyndFeed feed, RssMacroParameters parameters) throws MacroExecutionException {
int maxElements = parameters.getCount();
int count = 0;
for (Object item : feed.getEntries()) {
++count;
if (count > maxElements) {
break;
}
SyndEntry entry = (SyndEntry) item;
ResourceReference titleResourceReference = new ResourceReference(entry.getLink(), ResourceType.URL);
Block titleBlock = new LinkBlock(parsePlainText(entry.getTitle()), titleResourceReference, true);
ParagraphBlock paragraphTitleBlock = new ParagraphBlock(Collections.singletonList(titleBlock));
paragraphTitleBlock.setParameter(CLASS_ATTRIBUTE, "rssitemtitle");
parentBlock.addChild(paragraphTitleBlock);
if (parameters.isContent() && entry.getDescription() != null) {
// We are wrapping the feed entry content in a HTML macro, not considering what the declared content
// is, because some feed will declare text while they actually contain HTML.
// See http://stuffthathappens.com/blog/2007/10/29/i-hate-rss/
// A case where doing this might hurt is if a feed declares "text" and has any XML inside it does
// not want to be interpreted as such, but displayed as is instead. But this certainly is too rare
// compared to mis-formed feeds that say text while they want to say HTML.
Block html = new RawBlock(entry.getDescription().getValue(), Syntax.XHTML_1_0);
parentBlock.addChild(new GroupBlock(Arrays.asList(html), Collections.singletonMap(CLASS_ATTRIBUTE, "rssitemdescription")));
}
}
}
use of org.xwiki.rendering.block.GroupBlock in project xwiki-platform by xwiki.
the class UserAvatarMacro method execute.
@Override
public List<Block> execute(UserAvatarMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
DocumentReference userReference = this.currentDocumentReferenceResolver.resolve(parameters.getUsername(), new EntityReference(USER_SPACE, EntityType.SPACE));
// Find the avatar attachment name or null if not defined or an error happened when locating it
String fileName = null;
if (this.documentAccessBridge.exists(userReference)) {
Object avatarProperty = this.documentAccessBridge.getProperty(userReference, new DocumentReference(userReference.getWikiReference().getName(), USER_SPACE, "XWikiUsers"), "avatar");
if (avatarProperty != null) {
fileName = avatarProperty.toString();
}
} else {
throw new MacroExecutionException("User [" + this.compactWikiEntityReferenceSerializer.serialize(userReference) + "] is not registered in this wiki");
}
// Initialize with the default avatar.
ResourceReference imageReference = new ResourceReference(this.skinAccessBridge.getSkinFile("icons/xwiki/noavatar.png"), ResourceType.URL);
// Try to use the configured avatar.
if (!StringUtils.isBlank(fileName)) {
AttachmentReference attachmentReference = new AttachmentReference(fileName, userReference);
// Check if the configured avatar file actually exists.
try {
if (documentAccessBridge.getAttachmentVersion(attachmentReference) != null) {
// Use it.
imageReference = new ResourceReference(this.compactWikiEntityReferenceSerializer.serialize(attachmentReference), ResourceType.ATTACHMENT);
}
} catch (Exception e) {
// Log and fallback on default.
logger.warn("Failed to get the avatar for user [{}]: [{}]. Using default.", this.compactWikiEntityReferenceSerializer.serialize(userReference), ExceptionUtils.getRootCauseMessage(e));
}
}
ImageBlock imageBlock = new ImageBlock(imageReference, false);
imageBlock.setParameter("alt", "Picture of " + userReference.getName());
imageBlock.setParameter("title", userReference.getName());
if (parameters.getWidth() != null) {
imageBlock.setParameter("width", String.valueOf(parameters.getWidth()));
}
if (parameters.getHeight() != null) {
imageBlock.setParameter("height", String.valueOf(parameters.getHeight()));
}
List<Block> result = Collections.singletonList(imageBlock);
return context.isInline() ? result : Collections.singletonList(new GroupBlock(result));
}
use of org.xwiki.rendering.block.GroupBlock in project xwiki-platform by xwiki.
the class InternalTemplateManager method generateError.
private XDOM generateError(Throwable throwable) {
List<Block> errorBlocks = new ArrayList<Block>();
// Add short message
Map<String, String> errorBlockParams = Collections.singletonMap("class", "xwikirenderingerror");
errorBlocks.add(new GroupBlock(Arrays.<Block>asList(new WordBlock("Failed to render step content")), errorBlockParams));
// Add complete error
StringWriter writer = new StringWriter();
throwable.printStackTrace(new PrintWriter(writer));
Block descriptionBlock = new VerbatimBlock(writer.toString(), false);
Map<String, String> errorDescriptionBlockParams = Collections.singletonMap("class", "xwikirenderingerrordescription hidden");
errorBlocks.add(new GroupBlock(Arrays.asList(descriptionBlock), errorDescriptionBlockParams));
return new XDOM(errorBlocks);
}
use of org.xwiki.rendering.block.GroupBlock in project xwiki-platform by xwiki.
the class ColumnsDashboardRenderer method renderGadgets.
@Override
public List<Block> renderGadgets(List<Gadget> gadgets, GadgetRenderer gadgetsRenderer, MacroTransformationContext context) throws MacroExecutionException {
// transform the passed gagdets in a list of column gadgets
List<ColumnGadget> columnGadgets = new ArrayList<ColumnGadget>();
List<Gadget> invalidGadgets = new ArrayList<Gadget>();
for (Gadget gadget : gadgets) {
ColumnGadget cGadget = new ColumnGadget(gadget);
if (cGadget.getColumn() != null && cGadget.getIndex() != null) {
columnGadgets.add(cGadget);
} else {
invalidGadgets.add(gadget);
}
}
// sort the column gadgets by first the column number then by index in column, for all those which have a valid
// position
Collections.sort(columnGadgets, new Comparator<ColumnGadget>() {
public int compare(ColumnGadget g1, ColumnGadget g2) {
return g1.getColumn().equals(g2.getColumn()) ? g1.getIndex() - g2.getIndex() : g1.getColumn() - g2.getColumn();
}
});
// get the maximmum column number in the gadgets list and create that number of columns. This is the column
// number of the last gadget in the ordered list. Default is 1 column, the empty dashboard is made of one empty
// column
int columns = 1;
if (!columnGadgets.isEmpty()) {
// prevent bad configurations to mess up the dashboard layout
int lastGadgetsColumn = columnGadgets.get(columnGadgets.size() - 1).getColumn();
if (lastGadgetsColumn > 1) {
columns = lastGadgetsColumn;
}
}
// create the list of gadget containers
List<Block> gadgetContainers = new ArrayList<Block>();
for (int i = 0; i < columns; i++) {
GroupBlock gContainer = new GroupBlock();
gContainer.setParameter(CLASS, DashboardMacro.GADGET_CONTAINER);
// and generate the ids of the gadget containers, which are column numbers, 1 based
gContainer.setParameter(ID, DashboardMacro.GADGET_CONTAINER_PREFIX + (i + 1));
gadgetContainers.add(gContainer);
}
// render them as columns using the container macro and appropriate parameters
ContainerMacroParameters containerParams = new ContainerMacroParameters();
containerParams.setLayoutStyle("columns");
BlocksContainerMacro containerMacro = new BlocksContainerMacro();
containerMacro.setComponentManager(this.componentManager);
containerMacro.setContent(gadgetContainers);
List<Block> layoutedResult = containerMacro.execute(containerParams, null, context);
for (ColumnGadget gadget : columnGadgets) {
int columnIndex = gadget.getColumn() - 1;
gadgetContainers.get(columnIndex).addChildren(gadgetsRenderer.decorateGadget(gadget));
}
// and return the result
return layoutedResult;
}
use of org.xwiki.rendering.block.GroupBlock in project xwiki-platform by xwiki.
the class DefaultGadgetRenderer method decorateGadget.
@Override
public List<Block> decorateGadget(Gadget gadget) {
List<Block> result;
// a gadget or not.
if (!this.emptyXDOMChecker.check(gadget.getContent())) {
// prepare the title of the gadget, in a heading 2
HeaderBlock titleBlock = new HeaderBlock(gadget.getTitle(), HeaderLevel.LEVEL1);
titleBlock.setParameter(CLASS, "gadget-title");
// And then the content wrapped in a group block with class, to style it
GroupBlock contentGroup = new GroupBlock();
contentGroup.setParameter(CLASS, "gadget-content");
contentGroup.addChildren(gadget.getContent());
// and wrap everything in a container, to give it a class
GroupBlock gadgetBlock = new GroupBlock();
String idPrefix = "gadget";
gadgetBlock.setParameter(CLASS, idPrefix);
// put an underscore here because it doesn't hurt at this level and it helps scriptaculous on the frontend
gadgetBlock.setParameter(ID, idPrefix + "_" + gadget.getId());
gadgetBlock.addChild(titleBlock);
gadgetBlock.addChild(contentGroup);
result = Collections.singletonList(gadgetBlock);
} else {
result = Collections.emptyList();
}
return result;
}
Aggregations