/**
*
*/
package com.goldnet.webwork.action.news;
import com.opensymphony.xwork.ActionSupport;
import com.sun.syndication.feed.synd.SyndCategory;
import com.sun.syndication.feed.synd.SyndCategoryImpl;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author Tin
*
*/
public class TestFeedCreateAction extends ActionSupport {
private static final long serialVersionUID = -2207516408313865979L;
private transient final Log log = LogFactory.getLog(TestFeedCreateAction.class);
private int maxEntryNumber = 25;
private String siteUrl = "http://127.0.0.1";
private SyndFeed feed = null;
public TestFeedCreateAction() {
super();
}
@Override
public String execute() {
List<News> newsList = getNewsList();
if (log.isDebugEnabled()) {
log.debug("Geting feed! and got news " + newsList.size() +
" pieces.");
}
feed = new SyndFeedImpl();
feed.setTitle(converttoISO("測(cè)試中的新聞系統(tǒng)"));
feed.setDescription(converttoISO("測(cè)試中的新聞系統(tǒng):測(cè)試Rome Result"));
feed.setAuthor(converttoISO("測(cè)試Tin"));
feed.setLink("http://www.justatest.cn");
List<SyndEntry> entries = new ArrayList<SyndEntry>();
feed.setEntries(entries);
for (News news : newsList) {
SyndEntry entry = new SyndEntryImpl();
entry.setAuthor(converttoISO(news.getAuthor()));
SyndCategory cat = new SyndCategoryImpl();
cat.setName(converttoISO(news.getCategory()));
List<SyndCategory> cats = new ArrayList<SyndCategory>();
cats.add(cat);
entry.setCategories(cats);
SyndContent content = new SyndContentImpl();
content.setValue(converttoISO(news.getContent()));
List<SyndContent> contents = new ArrayList<SyndContent>();
contents.add(content);
entry.setContents(contents);
entry.setDescription(content);
entry.setLink(siteUrl + "/common/news/displayNews.action?id=" +
news.getId());
entry.setTitle(converttoISO(news.getTitle()));
entry.setPublishedDate(news.getPublishDate());
entries.add(entry);
}
return SUCCESS;
}
private static String converttoISO(String s) {
try {
byte[] abyte0 = s.getBytes("UTF-8");
return new String(abyte0, "ISO-8859-1");
} catch (Exception exception) {
return s;
}
}
private List<News> getNewsList() {
List<News> newsList = new ArrayList<News>();
for (int i = 0; i < maxEntryNumber; i++) {
News news = new News();
news.setTitle("測(cè)試標(biāo)題" + i);
news.setContent(
"<p>測(cè)試內(nèi)容測(cè)試內(nèi)容<span style=\"color:red\">測(cè)試內(nèi)容</span></p>");
news.setPublishDate(new Date());
news.setId(new Long(i));
news.setAuthor("Tin");
newsList.add(news);
}
return newsList;
}
/**
* @return Returns the maxEntryNumber.
*/
public long getMaxEntryNumber() {
return maxEntryNumber;
}
/**
* @param maxEntryNumber The maxEntryNumber to set.
*/
public void setMaxEntryNumber(int maxEntryNumber) {
this.maxEntryNumber = maxEntryNumber;
}
/**
* @param siteUrl The siteUrl to set.
*/
public void setSiteUrl(String siteUrl) {
this.siteUrl = siteUrl;
}
/**
* @return Returns the feed.
*/
public SyndFeed getFeed() {
return feed;
}
private class News {
private Long id;
private String title;
private String content;
private Date publishDate;
private String author;
private String category;
/**
* Getter/Setter都省略了,使用了內(nèi)部類,就是圖個(gè)方便
* 本意是模仿我們常常使用的Pojo,大家的實(shí)現(xiàn)都不一樣,我突簡單,里面其實(shí)可以有復(fù)雜類型的
*/
}
}