后台是spring + hibernate + freemarker
这个模块也是整个系统中的一小部门。从中对于XSL的使用,XSL很好很强。
并且对EXT中的vtype验证方式有了更加全面的总结。
贴代码:RssAction.java
public class RssAction extends BaseActionSuport {
private static RssService rssService;
private Collection<Rss> rsses;
private static final Logger logger = Logger.getLogger(RssAction.class);
private String rssname; // RSS名称
private String rssurl; // RSS地址
private String defaultUrl; // defaultUrl地址
private String url;// URl地址
private String rssPath;
private Long deleteId;
public Long getDeleteId() {
return deleteId;
}
public void setDeleteId(Long deleteId) {
this.deleteId = deleteId;
}
public String getRssname() {
return rssname;
}
public void setRssname(String rssname) {
this.rssname = rssname;
}
public String getRssurl() {
return rssurl;
}
public void setRssurl(String rssurl) {
this.rssurl = rssurl;
}
public String deal() {
try {
Rss rss = new Rss();
logger.debug("rssname:" + rssname);
logger.debug("rssurl:" + rssurl);
rss.setName(rssname);
rss.setUrl(rssurl);
rssService.create(rss);
} catch (Exception e) {
logger.error(e.toString());
return this.ERROR;
}
return this.SUCCESS;
}
public String rssDelete() {
try {
logger.debug("deleteId:" + deleteId);
rssService.deleteById(deleteId);
} catch (Exception e) {
logger.error(e.toString());
return this.ERROR;
}
return this.SUCCESS;
}
public String listAll() {
try {
rsses = rssService.listAllRss();
return this.SUCCESS;
} catch (Exception e) {
logger.error(e.toString());
return this.ERROR;
}
}
public String getRss() {
HttpURLConnection conn = null;
InputStream ios = null;
ByteArrayOutputStream out = null;
ActionContext ctx = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
try {
URL addr = null;
if (url == null || url.trim().length() == 0) {
addr = new URL(defaultUrl);
} else {
addr = new URL(url);
}
conn = (HttpURLConnection) addr.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
conn.setRequestProperty("Accept-Language", "zh-cn");
conn.setRequestProperty("Accept-Charset", "gb2312,gbk,utf-8;q=0.7,*;q=0.7");
String contentType = conn.getContentType();
String contentEncoding = conn.getContentEncoding();
int statusCode = conn.getResponseCode();
int length = conn.getContentLength();
if (logger.isDebugEnabled()) {
// logger.debug("contentType:" + contentType);
// logger.debug("contentEncoding:" + contentEncoding);
// logger.debug("statusCode:" + statusCode);
// logger.debug("length:" + length);
}
if (contentEncoding == null) {
if (contentType != null) {
contentEncoding = getCharSet(contentType);
}
if (contentEncoding == null) {
contentEncoding = "UTF-8";
}
else if(contentEncoding.equals("ansi"))
contentEncoding = "GBK";
}
ios = conn.getInputStream();
out = new ByteArrayOutputStream();
byte buffer[] = new byte[1024];
int len = 0;
while ((len = ios.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
logger.debug("contentEncoding:" + contentEncoding);
String rssData = new String(out.toByteArray(), contentEncoding);
//logger.debug("rssData:"+rssData);
String rssDa = rssxsl(rssData);
request.setAttribute("rssData", rssDa);
request.setAttribute("Encoding", contentEncoding);
return this.SUCCESS;
} catch (Exception e) {
logger.error(e.toString());
e.printStackTrace();
return this.ERROR;
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
logger.error(e.toString());
}
}
if (ios != null) {
try {
ios.close();
} catch (IOException e) {
logger.error(e.toString());
}
}
if (conn != null)
conn.disconnect();
}
}
public RssService getRssService() {
return rssService;
}
public void setRssService(RssService rssService) {
this.rssService = rssService;
}
public Collection<Rss> getRsses() {
return rsses;
}
public void setRsses(Collection<Rss> rsses) {
this.rsses = rsses;
}
public String getName() {
return rssname;
}
public void setName(String name) {
this.rssname = rssname;
}
public String getRssPath() {
return rssPath;
}
public void setRssPath(String rssPath) {
this.rssPath = rssPath;
}
public String getRssUrl() {
return rssurl;
}
public void setRssUrl(String rssUrl) {
this.rssurl = rssurl;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDefaultUrl() {
return defaultUrl;
}
public void setDefaultUrl(String defaultUrl) {
this.defaultUrl = defaultUrl;
}
private String getCharSet(String contentTypeEnd) {
if (contentTypeEnd != null) {
int pointer = contentTypeEnd.indexOf('=');
if (pointer > 0) {
String result = contentTypeEnd.substring(pointer + 1);
result = result.trim();
if (result != null) {
if (result.length() > 0) {
if (result.charAt(0) == '"') {
if (result.charAt(result.length() - 1) == '"') {
result = result.substring(1, result.length() - 1);
}
} else if (result.charAt(0) == '"'') {
if (result.charAt(result.length() - 1) == '"'') {
result = result.substring(1, result.length() - 1);
}
}
}
}
return result.trim().toLowerCase();
}
}
return null;
}
private String rssxsl(String rssData) {
String formerStr = "";
HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
String path = request.getRealPath("");
path = path + rssPath;
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLWriter writer = null;
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new javax.xml.transform.stream.StreamSource(path));
transformer.transform(new javax.xml.transform.stream.StreamSource(new StringReader(rssData)), new javax.xml.transform.stream.StreamResult(out));
formerStr = new String(out.toByteArray(), "UTF-8");
formerStr = removeXmlHead(formerStr);
} catch (Exception e) {
logger.error(e.toString());
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
logger.error(e.toString());
}
}
try {
out.close();
} catch (IOException e) {
logger.error(e.toString());
}
}
return formerStr;
}
private String removeXmlHead(String content) {
if (content == null) {
return null;
}
content = content.trim();
if (content.indexOf("<?xml") != 0) {
return content;
}
int index = content.indexOf("?>");
if (index < 0) {
return content;
}
content = content.substring(index + 2);
return content;
}
}
RssServic.java代码
import java.util.Collection;
import gov.ict.mcg.vbm.pojo.Rss;
public interface RssService {
public void create(Rss rss) throws Exception;
public void deleteById(Long id) throws Exception;
public Collection<Rss> listAllRss() throws Exception;
}
RssServiceImp.java 代码:
import java.util.Collection;
import org.apache.log4j.Logger;
import gov.ict.mcg.vbm.dao.RssDao;
import gov.ict.mcg.vbm.pojo.Rss;
import gov.ict.mcg.vbm.services.RssService;
public class RssServiceImpl extends BaseService implements RssService {
private static final Logger logger = Logger.getLogger(RssServiceImpl.class);
private RssDao rssDao;
public RssDao getRssDao() {
return rssDao;
}
public void setRssDao(RssDao rssDao) {
this.rssDao = rssDao;
}
public void create(Rss rss) throws Exception {
try {
rssDao.create(rss);
} catch (Exception e) {
throw new Exception();
}
}
public Collection<Rss> listAllRss() throws Exception {
return rssDao.listAll();
}
public void deleteById(Long id) throws Exception {
try {
rssDao.deleteByIdCommit(id);
} catch (Exception e) {
throw new Exception();
}
}
}
下面是控制显示的simple.xsl
<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema"
version="1.0" exclude-result-prefixes="xsl ddwrt msxsl rssaggwrt"
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
xmlns:rssaggwrt="http://schemas.microsoft.com/WebParts/v3/rssagg/runtime"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:rssFeed="urn:schemas-microsoft-com:sharepoint:RSSAggregatorWebPart"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:rss1="http://purl.org/rss/1.0/" xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
xmlns:atom2="http://purl.org/atom/ns#" xmlns:ddwrt2="urn:frontpage:internal">
<xsl:template match="/">
<xsl:for-each select="/rss/channel/item">
<div class="item">
<div><h4><a href="{./link}" target="_blank"><xsl:value-of select="./title"/></a></h4></div>
<div>
<font style="font-weight: bold" face="Courier New" size="2" bold="true">标签: </font><font face="Courier New" size="2">
<xsl:value-of select="./itunes:keywords"/> ||
<xsl:value-of select="./pubDate"/></font>
</div>
<div class="content" id="content">
<xsl:value-of select="./description" disable-output-escaping="yes"/>
<xsl:if test="system-property('xsl:vendor')='Transformiix'">
<script type="text/javascript">
var element = document.getElementById("content");
element.innerHTML = element.textContent;
</script>
</xsl:if>
</div>
</div>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>