2009-07-31 28 views
1

我正在使用Rome將幾個提要合併爲一個。 它主要基於羅馬網站上的this example使用羅馬設置每個項目的來源

我正在創建一個RSS 2.0提要,我保存爲(W3C)文檔,然後傳遞給樣式表以轉換爲HTML。

我的一個要求是顯示每個條目的來源(鏈接到和起源網站的名稱)(因爲它們可以來自各種來源)。

根據RSS規範,每個項目有一個optional source attribute。 羅馬似乎用SyndEntry接口上的setSource方法來支持這一點。 但是,將其設置爲原始Feed的SyndFeed不會設置此屬性。

我輸出的文檔在項目中不包含源元素。

任何關於我可能做錯的線索或對做我想做的替代方法的建議?

在此先感謝Darren。

回答

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的原因是,調用,僅設置了筆者,如果是零,我們需要始終將其設置爲我們的價值。

然後,我將其引用爲我的XSL樣式表中的dc:creator。

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放在資源的根部即可。 不要忘了都柏林核心添加到您的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 

而這一切。