<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.rendering.internal.macro.rss;

import java.io.StringReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.commons.lang3.StringUtils;
import org.xwiki.bridge.SkinAccessBridge;
import org.xwiki.component.annotation.Component;
import org.xwiki.context.Execution;
import org.xwiki.rendering.block.Block;
import org.xwiki.rendering.block.GroupBlock;
import org.xwiki.rendering.block.ImageBlock;
import org.xwiki.rendering.block.LinkBlock;
import org.xwiki.rendering.block.ParagraphBlock;
import org.xwiki.rendering.block.RawBlock;
import org.xwiki.rendering.listener.reference.ResourceReference;
import org.xwiki.rendering.listener.reference.ResourceType;
import org.xwiki.rendering.macro.MacroExecutionException;
import org.xwiki.rendering.macro.box.AbstractBoxMacro;
import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor;
import org.xwiki.rendering.macro.rss.RssMacroParameters;
import org.xwiki.rendering.parser.ParseException;
import org.xwiki.rendering.parser.Parser;
import org.xwiki.rendering.syntax.Syntax;
import org.xwiki.rendering.transformation.MacroTransformationContext;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;

/**
 * Macro that output latest feed entries from a RSS feed.
 * 
 * @version $Id$
 * @since 1.8RC1
 */
@Component
@Named("rss")
@Singleton
public class RssMacro extends AbstractBoxMacro<RssMacroParameters>
{
    /**
     * The name of the CSS class attribute.
     */
    private static final String CLASS_ATTRIBUTE = "class";

    private static final String FEED_CLASS_VALUE = "rssfeed";

    private static final String FEED_PROPERTY = "RssMacro.feed";

    /**
     * The description of the macro.
     */
    private static final String DESCRIPTION = "Output latest feed entries from a RSS feed.";

    /**
     * The relative skin path of the feed icon to be displayed in the channel title.
     */
    private static final String FEED_ICON_RESOURCE_PATH = "icons/silk/feed.png";

    /**
     * Used to get the RSS icon.
     */
    @Inject
    private SkinAccessBridge skinAccessBridge;

    /**
     * Needed to parse the ordinary text.
     */
    @Inject
    @Named("plain/1.0")
    private Parser plainTextParser;

    /**
     * Needed to keep the feed information.
     */
    @Inject
    private Execution execution;

    /**
     * Create a Feed object from a feed specified as a URL.
     */
    private RomeFeedFactory romeFeedFactory = new DefaultRomeFeedFactory();

    /**
     * Create and initialize the descriptor of the macro.
     */
    public RssMacro()
    {
        super("RSS", DESCRIPTION, new DefaultContentDescriptor(DESCRIPTION, false), RssMacroParameters.class);
        setDefaultCategory(DEFAULT_CATEGORY_CONTENT);
    }

    @Override
    public boolean supportsInlineMode()
    {
        return false;
    }

    /**
     * Allow to save the current processed feed in the context, to be able to retrieve without concurrency issue.
     * @param feed the feed to save in the current context.
     */
    private void setFeed(SyndFeed feed)
    {
        this.execution.getContext().setProperty(FEED_PROPERTY, feed);
    }

    /**
     * Retrieve the feed of the current context.
     * @return the feed that is being processed in the macro.
     */
    private SyndFeed getFeed()
    {
        return (SyndFeed) this.execution.getContext().getProperty(FEED_PROPERTY);
    }

    /**
     * Remove the feed information from the current context.
     */
    private void removeContextFeed()
    {
        this.execution.getContext().removeProperty(FEED_PROPERTY);
    }

    @Override
    protected ResourceReference getImageReference(RssMacroParameters parameters, String content,
        MacroTransformationContext context)
    {
        if (parameters.isDecoration() && parameters.isImage()) {
            return new ResourceReference(getFeed().getImage().getUrl(), ResourceType.URL);
        } else {
            return super.getImageReference(parameters, content, context);
        }
    }

    @Override
    protected List<? extends Block> getBlockTitle(RssMacroParameters parameters, String content,<commit_msg>XWIKI-15882: RssMacro should rely on AbstractBoxMacro helper methods<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.rendering.internal.macro.rss;

import java.io.StringReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.commons.lang3.StringUtils;
import org.xwiki.bridge.SkinAccessBridge;
import org.xwiki.component.annotation.Component;
import org.xwiki.context.Execution;
import org.xwiki.rendering.block.Block;
import org.xwiki.rendering.block.GroupBlock;
import org.xwiki.rendering.block.ImageBlock;
import org.xwiki.rendering.block.LinkBlock;
import org.xwiki.rendering.block.ParagraphBlock;
import org.xwiki.rendering.block.RawBlock;
import org.xwiki.rendering.listener.reference.ResourceReference;
import org.xwiki.rendering.listener.reference.ResourceType;
import org.xwiki.rendering.macro.MacroExecutionException;
import org.xwiki.rendering.macro.box.AbstractBoxMacro;
import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor;
import org.xwiki.rendering.macro.rss.RssMacroParameters;
import org.xwiki.rendering.parser.ParseException;
import org.xwiki.rendering.parser.Parser;
import org.xwiki.rendering.syntax.Syntax;
import org.xwiki.rendering.transformation.MacroTransformationContext;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;

/**
 * Macro that output latest feed entries from a RSS feed.
 * 
 * @version $Id$
 * @since 1.8RC1
 */
@Component
@Named("rss")
@Singleton
public class RssMacro extends AbstractBoxMacro<RssMacroParameters>
{
    /**
     * The name of the CSS class attribute.
     */
    private static final String CLASS_ATTRIBUTE = "class";

    private static final String FEED_CLASS_VALUE = "rssfeed";

    private static final String FEED_PROPERTY = "RssMacro.feed";

    /**
     * The description of the macro.
     */
    private static final String DESCRIPTION = "Output latest feed entries from a RSS feed.";

    /**
     * The relative skin path of the feed icon to be displayed in the channel title.
     */
    private static final String FEED_ICON_RESOURCE_PATH = "icons/silk/feed.png";

    /**
     * Used to get the RSS icon.
     */
    @Inject
    private SkinAccessBridge skinAccessBridge;

    /**
     * Needed to parse the ordinary text.
     */
    @Inject
    @Named("plain/1.0")
    private Parser plainTextParser;

    /**
     * Needed to keep the feed information.
     */
    @Inject
    private Execution execution;

    /**
     * Create a Feed object from a feed specified as a URL.
     */
    private RomeFeedFactory romeFeedFactory = new DefaultRomeFeedFactory();

    /**
     * Create and initialize the descriptor of the macro.
     */
    public RssMacro()
    {
        super("RSS", DESCRIPTION, new DefaultContentDescriptor(DESCRIPTION, false), RssMacroParameters.class);
        setDefaultCategory(DEFAULT_CATEGORY_CONTENT);
    }

    @Override
    public boolean supportsInlineMode()
    {
        return false;
    }

    /**
     * Allow to save the current processed feed in the context, to be able to retrieve without concurrency issue.
     * @param feed the feed to save in the current context.
     */
    private void setFeed(SyndFeed feed)
    {
        this.execution.getContext().setProperty(FEED_PROPERTY, feed);
    }

    /**
     * Retrieve the feed of the current context.
     * @return the feed that is being processed in the macro.
     */
    private SyndFeed getFeed()
    {
        return (SyndFeed) this.execution.getContext().getProperty(FEED_PROPERTY);
    }

    /**
     * Remove the feed information from the current context.
     */
    private void removeContextFeed()
    {
        this.execution.getContext().removeProperty(FEED_PROPERTY);
    }

    @Override
    protected ResourceReference getImageReference(RssMacroParameters parameters, String content,
        MacroTransformationContext context)
    {
        if (parameters.isImage() && getFeed().getImage() != null) {
            return new ResourceReference(getFeed().getImage().getUrl(), ResourceType.URL);
        } else {
            return super.getImageReference(parameters, content, context);
        }
    }

    @Override
    protected List<? extends Block> getBlockTitle(RssMacroParameters parameters, String content,
{'fraction_non_alphanumeric': 0.08792675948884227, 'fraction_numerical': 0.004100705702841884, 'mean_word_length': 3.4455277660025434, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5656317', 'n_tokens_mistral': 2981, 'n_tokens_neox': 2907, 'n_words': 909}