使用罗马设置每个项目的来源

时间:2009-07-31 14:34:17

标签: java rss rome

我正在使用Rome将多个Feed合并为一个。 它主要基于罗马网站上的this example

我正在创建一个RSS 2.0提要,我将其保存为(W3C)文档,然后传递给样式表以转换为HTML。

我的一个要求是显示每个条目的来源(原始网站的链接和名称)(因为它们可能来自各种来源)。

根据RSS规范,每个项目有optional source attribute个。 罗马似乎在SyndEntry接口上使用setSource方法支持这一点。 但是,将此设置为原始Feed的SyndFeed似乎不会设置此属性。

我输出的文档在项目中不包含源元素。

有关我可能做错的任何线索或有关做我想做的事情的其他方法的建议?

提前感谢,Darren。

2 个答案:

答案 0 :(得分:2)

我知道答案有点晚了,但也许有人会在以后使用它。 我用罗马1.0做了。

您可以定义自己的转换器和生成器。

我需要的是一个带有源字段的RSS 2.0提要。因此,对于转换器和生成器,我通过ROME扩展了RSS 2.0的实现。

首先我们需要一个转换器。这是填补来源的人

/**
 * This is a convertor for RSS 2.0 setting source on output items
 */
public class ConverterForRSS20WithSource extends ConverterForRSS20 {

    /**
     * Default Constructor
     */
    public ConverterForRSS20WithSource() {
        this("rss_2.0_withSource");
    }

    /**
     * Constructor with type
     * @param type
     */
    protected ConverterForRSS20WithSource(String type) {
        super(type);
    }

    /** 
     * @see com.sun.syndication.feed.synd.impl.ConverterForRSS094#createRSSItem(com.sun.syndication.feed.synd.SyndEntry)
     */
    @Override
    protected Item createRSSItem(SyndEntry sEntry) {
        Item item = super.createRSSItem(sEntry);
        if(sEntry.getSource() != null 
                && StringUtils.isNotBlank(sEntry.getSource().getUri())) {
            Source s = new Source();
            s.setUrl(sEntry.getSource().getUri());
            s.setValue(sEntry.getSource().getTitle());
            item.setSource(s);
        }

        return item;
    }
}

然后我们需要一个发电机。这没什么特别的。它只是必须

/**
 *  Rss 2.0 Generator with source field
 */
public class RSS020GeneratorWithSource extends RSS20Generator {

    /**
     * 
     */
    public RSS020GeneratorWithSource() {
        super("rss_2.0_withSource","2.0");
    }

}

我们需要做最后一件事,向罗马宣布我们的课程。为此,只需将rome.properties放在资源的根目录下即可。 别忘了将dublin核心添加到你的rss.items中...... 在那个文件中放入

Converter.classes=my.package.ConverterForRSS20WithSource

WireFeedGenerator.classes=my.package.RSS020GeneratorWithSource

# Parsers for RSS 2.0 with source item modules
#
rss_2.0_withSource.item.ModuleParser.classes=com.sun.syndication.io.impl.DCModuleParser

# Generators for RSS_2.0 entry modules
#
rss_2.0_withSource.item.ModuleGenerator.classes=com.sun.syndication.io.impl.DCModuleGenerator

就是这样。

答案 1 :(得分:1)

我现在找到了解决方法。

由于我只需要提供一个名称作为归属,我将覆盖作者字段,如下所示。

SyndEntry entry = // fetched from SyndFeed
Module dcModule = entry.getModule(DCModule.URI);
String title = // My overridden title
if (dcModule != null && title != null) {
    ((DCModule)dcModule).setCreator(title);
}

我使用此代码而不是SyndEntry.setAuthor的原因是调用只设置作者是否为null,我们需要始终将其设置为我们的值。

然后我在我的XSL样式表中将其称为dc:creator。