最近用rome做了一个rss服务,大致的内容如下,有不妥的地方,请指正:
//RssChannelDO类
public class RssChannelDO {
private String title;
private String description;
private String link;
private String uri;[size=x-large;][/size][size=large][/size]
@SuppressWarnings("unchecked")
private List items;
private String language;
private String copyright;
private Date pubDate;
private String feedType;
private String encoding;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
@SuppressWarnings("unchecked")
public List getItems() {
return items;
}
@SuppressWarnings("unchecked")
public void setItems(List items) {
this.items = items;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getCopyright() {
return copyright;
}
public void setCopyright(String copyright) {
this.copyright = copyright;
}
public Date getPubDate() {
return pubDate;
}
public void setPubDate(Date pubDate) {
this.pubDate = pubDate;
}
public String getFeedType() {
return feedType;
}
public void setFeedType(String feedType) {
this.feedType = feedType;
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
}
///
//RssChannelItemDO类
public class RssChannelItemDO {
private String title;// Rss文件中Item的标题
private String link;// Rss文件中Item对应的连接
private String description;// Item的描述
private Date pubDate;// Item发布的时间
private String author;// Item作者
private String category;// Item所属的频道范畴
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getLink()
{
return link;
}
public void setLink(String link)
{
this.link = link;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
//articleDO.getArticlePages().get(0).get
}
public Date getPubDate()
{
return pubDate;
}
public void setPubDate(Date pubDate)
{
this.pubDate = pubDate;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getCategory()
{
return category;
}
public void setCategory(String category)
{
this.category = category;
}
}
///
public class RssChannelEItemDO extends RssChannelItemDO{
private String enclosure;// 流媒体文件
public String getEnclosure()
{
return enclosure;
}
public void setEnclosure(String enclosure)
{
this.enclosure = enclosure;
}
}
///
public interface RssServiceBase {
/**
* rss服务发布接口
*
* @param rssSiteId
* 网站服务ID
* @param channel
* 频道名
* @param itemList
* 频道中所包含的项
* @param rssType
* rss协议类型
* @param encoding
* 编码类型
* @param layOut
* 排版类型 默认为0,用于以后扩展
*/
public boolean rssBuild(String rssSiteId,RssChannelDO channel,
ArrayList itemList,String rssType,String encoding,int layOut);
}
//
public class RssFactory implements RssServiceBase{
private SyndFeed feed; //创建RSS
@SuppressWarnings("unchecked")
private List entries; //生成item集合
private SyndEntry entry; //生成item
@SuppressWarnings("unused")
private RssChannelDO rssChannel; //频道域
@SuppressWarnings("unchecked")
private static Map rssMap=new HashMap(); //保存所对应的rss服务内容,当map中有时,无需再创建,直接读取
private static RssFactory rssFactory;
@SuppressWarnings("unchecked")
private RssFactory() {
feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
entries = new ArrayList();
}
/**
* 创建一个频道
*
* @param title
* 频道标题
* @param link
* 频道对应的连接
* @param description
* 频道描述
* @param language
* 频道所用语言
* @param pubDate
* 频道发布时期
* @param copyright
* 版权所有
* @throws Exception
*/
public void buildChannel(String title, String link, String description,
String language, Date pubDate, String copyright)
throws RuntimeException {
feed.setTitle(title);
feed.setLink(link);
feed.setDescription(description);
feed.setLanguage(language);
feed.setPublishedDate(pubDate);
feed.setCopyright(copyright);
}
/**
* 添加频道的子内容
*
* @param item
* {@link ChannelItem}
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void buildItems(RssChannelItemDO item) throws RuntimeException {
entry = new SyndEntryImpl();
// 设置新闻标题
entry.setTitle(item.getTitle());
// 设置新闻的连接地址
entry.setLink(item.getLink());
// 设置新闻简介
SyndContent content = new SyndContentImpl();
content.setType("text/plain");
content.setValue(item.getDescription());
entry.setDescription(content);
// 设置发布时间
entry.setPublishedDate(item.getPubDate());
// 设置频道所属的范围
SyndCategory cate = new SyndCategoryImpl();
cate.setName(item.getCategory());
List cateList = new ArrayList();
cateList.add(cate);
entry.setCategories(cateList);
// 设置作者
entry.setAuthor(item.getAuthor());
// 将新闻项添加至数组中
entries.add(entry);
}
/**
* 添加频道的内容项
*
* @param item
* {@link ChannelEItem}此类继承自ChannelItem类
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void buildItems(RssChannelEItemDO item) throws RuntimeException {
entry = new SyndEntryImpl();
// 设置新闻标题
entry.setTitle(item.getTitle());
// 设置新闻的连接地址
entry.setLink(item.getLink());
// 设置新闻简介
SyndContent content = new SyndContentImpl();
content.setValue(item.getDescription());
entry.setDescription(content);
// 设置发布时间
entry.setPublishedDate(item.getPubDate());
// 设置频道所属的范围
SyndCategory cate = new SyndCategoryImpl();
cate.setName(item.getCategory());
List cateList = new ArrayList();
cateList.add(cate);
entry.setCategories(cateList);
// 设置作者
entry.setAuthor(item.getAuthor());
// 设置流媒体播放文件
SyndEnclosure en = new SyndEnclosureImpl();
en.setUrl(item.getEnclosure());
List enList = new ArrayList();
enList.add(en);
entry.setEnclosures(enList);
// 将新闻项添加至数组中
entries.add(entry);
}
/**
* rss服务发布接口
*
* @param rssSiteId
* 网站服务ID
* @param channel
* 频道名
* @param itemList
* 频道中所包含的项
* @param rssType
* rss协议类型
* @param encoding
* 编码类型
* @param layOut
* 排版类型 默认为0,用于以后扩展
*/
public boolean rssBuild(String rssSiteId,RssChannelDO channel,
ArrayList<RssChannelItemDO> itemList,String rssType,String encoding,int layOut)
{
buildChannel(channel.getTitle(),channel.getLink(),channel.getDescription(),
channel.getLanguage(),channel.getPubDate(),channel.getCopyright());
String rss=null;
int size=itemList.size();
for(int i=0;i<size;i++)
{
buildItems(itemList.get(i));
}
feed.setEntries(entries);
feed.setEncoding(encoding);
SyndFeedOutput output = new SyndFeedOutput();
try{
rss=output.outputString(feed); //取得所创建的rss服务内容
}catch(FeedException e)
{
e.printStackTrace();
}
if(rss==null)
return false;
String rsskey="rss-"+rssSiteId+"-"+channel.getTitle()+"-"+rssType;
rssMap.put(rsskey, rss); //根据rss的siteId、频道名,和协议类型把所创建的rss内容保存到map中
return true;
}
public String getRss(String rsskey)
{
return (String)rssMap.get(rsskey);
}
public static RssFactory getInstance()
{
if(rssFactory==null)
{
rssFactory=new RssFactory();
}
return rssFactory;
}
}
//RssChannelDO类
public class RssChannelDO {
private String title;
private String description;
private String link;
private String uri;[size=x-large;][/size][size=large][/size]
@SuppressWarnings("unchecked")
private List items;
private String language;
private String copyright;
private Date pubDate;
private String feedType;
private String encoding;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
@SuppressWarnings("unchecked")
public List getItems() {
return items;
}
@SuppressWarnings("unchecked")
public void setItems(List items) {
this.items = items;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getCopyright() {
return copyright;
}
public void setCopyright(String copyright) {
this.copyright = copyright;
}
public Date getPubDate() {
return pubDate;
}
public void setPubDate(Date pubDate) {
this.pubDate = pubDate;
}
public String getFeedType() {
return feedType;
}
public void setFeedType(String feedType) {
this.feedType = feedType;
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
}
///
//RssChannelItemDO类
public class RssChannelItemDO {
private String title;// Rss文件中Item的标题
private String link;// Rss文件中Item对应的连接
private String description;// Item的描述
private Date pubDate;// Item发布的时间
private String author;// Item作者
private String category;// Item所属的频道范畴
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getLink()
{
return link;
}
public void setLink(String link)
{
this.link = link;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
//articleDO.getArticlePages().get(0).get
}
public Date getPubDate()
{
return pubDate;
}
public void setPubDate(Date pubDate)
{
this.pubDate = pubDate;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getCategory()
{
return category;
}
public void setCategory(String category)
{
this.category = category;
}
}
///
public class RssChannelEItemDO extends RssChannelItemDO{
private String enclosure;// 流媒体文件
public String getEnclosure()
{
return enclosure;
}
public void setEnclosure(String enclosure)
{
this.enclosure = enclosure;
}
}
///
public interface RssServiceBase {
/**
* rss服务发布接口
*
* @param rssSiteId
* 网站服务ID
* @param channel
* 频道名
* @param itemList
* 频道中所包含的项
* @param rssType
* rss协议类型
* @param encoding
* 编码类型
* @param layOut
* 排版类型 默认为0,用于以后扩展
*/
public boolean rssBuild(String rssSiteId,RssChannelDO channel,
ArrayList itemList,String rssType,String encoding,int layOut);
}
//
public class RssFactory implements RssServiceBase{
private SyndFeed feed; //创建RSS
@SuppressWarnings("unchecked")
private List entries; //生成item集合
private SyndEntry entry; //生成item
@SuppressWarnings("unused")
private RssChannelDO rssChannel; //频道域
@SuppressWarnings("unchecked")
private static Map rssMap=new HashMap(); //保存所对应的rss服务内容,当map中有时,无需再创建,直接读取
private static RssFactory rssFactory;
@SuppressWarnings("unchecked")
private RssFactory() {
feed = new SyndFeedImpl();
feed.setFeedType("rss_2.0");
entries = new ArrayList();
}
/**
* 创建一个频道
*
* @param title
* 频道标题
* @param link
* 频道对应的连接
* @param description
* 频道描述
* @param language
* 频道所用语言
* @param pubDate
* 频道发布时期
* @param copyright
* 版权所有
* @throws Exception
*/
public void buildChannel(String title, String link, String description,
String language, Date pubDate, String copyright)
throws RuntimeException {
feed.setTitle(title);
feed.setLink(link);
feed.setDescription(description);
feed.setLanguage(language);
feed.setPublishedDate(pubDate);
feed.setCopyright(copyright);
}
/**
* 添加频道的子内容
*
* @param item
* {@link ChannelItem}
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void buildItems(RssChannelItemDO item) throws RuntimeException {
entry = new SyndEntryImpl();
// 设置新闻标题
entry.setTitle(item.getTitle());
// 设置新闻的连接地址
entry.setLink(item.getLink());
// 设置新闻简介
SyndContent content = new SyndContentImpl();
content.setType("text/plain");
content.setValue(item.getDescription());
entry.setDescription(content);
// 设置发布时间
entry.setPublishedDate(item.getPubDate());
// 设置频道所属的范围
SyndCategory cate = new SyndCategoryImpl();
cate.setName(item.getCategory());
List cateList = new ArrayList();
cateList.add(cate);
entry.setCategories(cateList);
// 设置作者
entry.setAuthor(item.getAuthor());
// 将新闻项添加至数组中
entries.add(entry);
}
/**
* 添加频道的内容项
*
* @param item
* {@link ChannelEItem}此类继承自ChannelItem类
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void buildItems(RssChannelEItemDO item) throws RuntimeException {
entry = new SyndEntryImpl();
// 设置新闻标题
entry.setTitle(item.getTitle());
// 设置新闻的连接地址
entry.setLink(item.getLink());
// 设置新闻简介
SyndContent content = new SyndContentImpl();
content.setValue(item.getDescription());
entry.setDescription(content);
// 设置发布时间
entry.setPublishedDate(item.getPubDate());
// 设置频道所属的范围
SyndCategory cate = new SyndCategoryImpl();
cate.setName(item.getCategory());
List cateList = new ArrayList();
cateList.add(cate);
entry.setCategories(cateList);
// 设置作者
entry.setAuthor(item.getAuthor());
// 设置流媒体播放文件
SyndEnclosure en = new SyndEnclosureImpl();
en.setUrl(item.getEnclosure());
List enList = new ArrayList();
enList.add(en);
entry.setEnclosures(enList);
// 将新闻项添加至数组中
entries.add(entry);
}
/**
* rss服务发布接口
*
* @param rssSiteId
* 网站服务ID
* @param channel
* 频道名
* @param itemList
* 频道中所包含的项
* @param rssType
* rss协议类型
* @param encoding
* 编码类型
* @param layOut
* 排版类型 默认为0,用于以后扩展
*/
public boolean rssBuild(String rssSiteId,RssChannelDO channel,
ArrayList<RssChannelItemDO> itemList,String rssType,String encoding,int layOut)
{
buildChannel(channel.getTitle(),channel.getLink(),channel.getDescription(),
channel.getLanguage(),channel.getPubDate(),channel.getCopyright());
String rss=null;
int size=itemList.size();
for(int i=0;i<size;i++)
{
buildItems(itemList.get(i));
}
feed.setEntries(entries);
feed.setEncoding(encoding);
SyndFeedOutput output = new SyndFeedOutput();
try{
rss=output.outputString(feed); //取得所创建的rss服务内容
}catch(FeedException e)
{
e.printStackTrace();
}
if(rss==null)
return false;
String rsskey="rss-"+rssSiteId+"-"+channel.getTitle()+"-"+rssType;
rssMap.put(rsskey, rss); //根据rss的siteId、频道名,和协议类型把所创建的rss内容保存到map中
return true;
}
public String getRss(String rsskey)
{
return (String)rssMap.get(rsskey);
}
public static RssFactory getInstance()
{
if(rssFactory==null)
{
rssFactory=new RssFactory();
}
return rssFactory;
}
}