java生成opml

<?xml version="1.0" encoding="utf-8" ?> 
<opml version="1.0">
  <head>
   <title>${title}</title> 
   <dateCreated>${pubDate}</dateCreated> 
   <ownerName>${owner}</ownerName> 
   <ownerEmail>${email}</ownerEmail> 
   <description>${title}</description> 
   <createdBy>${builder}</createdBy> 
   <link>${link}</link> 
  </head>
  <body>
  <outline title="${title}" text="${title}" opml="" kind="nkPath">
    <#list typeList as type>
     <outline title="${type.title}" text="${type.title}" type="rss" xmlUrl="${buildPath}/news/rss/${type.typecode}.xml" interval="60" itemMax="400" htmlUrl="" description="${type.title}" kind="nkFeed" /> 
    </#list> 
   </outline>
  </body>
</opml>


完成所需要的模板文件后,就要对模板文件进行生成啦。我使用的技术是Struts2+Spring+Hibernate+Dwr实现的,生成文件的那点是使用Dwr去实现的。具体的生成代码如下:

public String opmlBuild(String typeCode, String fileName, String owner,
   String email, String builder, String link, String buildPath,
   HttpServletRequest request, HttpServletResponse response) {
  String result = "";
  try {
   response.setContentType("text/opml");
   response.setCharacterEncoding("utf-8");
   String ftlPath = request.getSession().getServletContext()
     .getRealPath("/")
     + "//WEB-INF//ftl//";
   String path = request.getSession().getServletContext().getRealPath(
     "/");
   // String ftlPath = ftl + "//ftl//";
   config.setDirectoryForTemplateLoading(new File(ftlPath));
   // 得到模板文件
   Template template = config.getTemplate("opml.ftl");
   // 设置编码格式
   template.setEncoding("utf-8");
   YxNewsType type = typeNews.getTypeByTypeCode(typeCode);
   List<YxNewsType> typeList = typeNews
     .getAllChildTypeByTypeCode(typeCode);
   if (typeList != null) {
    Map root = new HashMap();

  //设置发布的OPML的标题
    root.put("title", type.getTitle());

    //设置OPML的发布时间
    root.put("pubDate", new Date().toString());

    //设置OPML的所有者
    root.put("owner", owner);

  //设置OPML的发布者的EMail地址
    root.put("email", email);

    //设置OPML的创建人
    root.put("builder", builder);

    //设置OPML文件的连接地址,可以直接你网站的连接地址
    root.put("link", link);

  //设置自己的创建Rss的连接地址
    root.put("buildPath", buildPath);
    root.put("typeList", typeList);
    // 设置到生成的文件名称

    String file = "";

  //判断当前操作系统,如果是Windows系统的做就是反斜杠,如果是Linux系统就使用正斜杠
    Properties properties = System.getProperties();
    Object object = properties.get("os.name");
    // System.out.println("当前操作系统:" + String.valueOf(object));
    if ("Windows".equals(String.valueOf(object))) {
     file = path + "\\rss\\opml\\" + fileName + ".opml";
    } else {
     file = path + "//rss//opml//" + fileName + ".opml";
    }

    //设置要生成的文件
    Writer out = new OutputStreamWriter(new FileOutputStream(file),
      "utf-8");
    template.process(root, out);

  //清理写入流
    out.flush();

    // 关闭流文件
    out.close();
    result = "buildSuccess";
   } else {
    result = "noChild";
   }
  } catch (Exception e) {
   result = "error";
   e.printStackTrace();
  }
  return result;
 }

上面方法中的参数是通过Dwr传递过来的,下面是页面JSP文件:

<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
 <head>
  <title>信源管理</title>
  <link rel="stylesheet"
   href="${pageContext.request.contextPath}/common/css/admin/style.css"
   type="text/css" />
  <script type="text/javascript"
   src="${pageContext.request.contextPath}/dwr/engine.js"></script>
  <script type="text/javascript"
   src="${pageContext.request.contextPath}/dwr/util.js"></script>
  <script type="text/javascript"
   src="${pageContext.request.contextPath}/dwr/interface/rssBuild.js"></script>
  <script type="text/javascript">
   var typeCode;
   function showBuild(title,code){
    //alert(title);
    typeCode = code;
    var doc = document.getElementById("buildOpml");
    document.getElementById("tableTitle").innerHTML=title;
    document.getElementById("opmlName").value = "";
    document.getElementById("nameMessage").innerHTML = "请以英文名称命名";
    document.getElementById("startBuild").value = "生成OPML";
    doc.style.display = "block";
   }
   function buildOpml(){
    owner = document.getElementById("opmlOwner").value;
    email = document.getElementById("opmlEmail").value;
    builder = document.getElementById("opmlBuilder").value;
    url = document.getElementById("opmlUrl").value;
    fileName = document.getElementById("opmlName").value;
    prefix = document.getElementById("opmlPrefix").value;
    msg = document.getElementById("nameMessage");
    btnBuild = document.getElementById("startBuild");
    if(""!=fileName){
     msg.innerHTML = "<font color='green'>文件生成中,请等待……</font>";
     btnBuild.disabled = true;
     btnBuild.value = "文件生成中";
     rssBuild.opmlBuild(typeCode,fileName,owner,email,builder,url,prefix,callBackResult);
    }else{
     msg.innerHTML = "<font color='red'>请填写你的OPML文件名称</font>";
    }
   }
   function callBackResult(result){
    msg = document.getElementById("nameMessage");
    btnBuild = document.getElementById("startBuild");
    if(result=="buildSuccess"){
     msg.innerHTML = "<font color='green'>文件已经成功生成</font>";
     btnBuild.disabled = false;
     btnBuild.value = "生成成功";
    }else if(result=="noChild"){
     msg.innerHTML = "<font color='red'>此类别没有子信息,无法生成</font>";
     btnBuild.disabled = false;
     btnBuild.value = "没有子类可供生成";
    }else{
     msg.innserHTML = "<font color='red'>生成出现错误,请联系管理员</font>";
     btnBuild.disabled = false;
     btnBuild.value = "生成失败";
    }
   }
   function showHideDIV(){
    var doc = document.getElementById("buildOpml");
    doc.style.display =(doc.style.display == "none"?"block":"none");
   }
  </script>
 </head>
 <body>
  <div id="container">
   <table class="tableBorder" align="center" border="0" cellpadding="2"
    cellspacing="1" style="width: 98%;">
    <tr>
     <th height="25" colspan="10">
      类别名称
     </th>
    </tr>
    <tr>
     <s:iterator value="listType">
      <td class="TableRow2">
       <a href="javascript:showBuild('${title }','${typecode }');"
        style="text-shadow: none;"> <s:property value="title" /> </a>
      </td>
     </s:iterator>
    </tr>
   </table>
   <div id="buildOpml" style="display:none;">
    <table class="tableBorder" align="center" border="0" cellpadding="2"
    cellspacing="1" style="width: 98%;">
     <tr>
      <th colspan="3">
       <div id="tableTitle"></div>
      </th>
     </tr>
     <tr>
      <td class="TableRow2">请输入OPML的所有者:</td>
      <td class="TableRow2">
       <input type="text" id="opmlOwner" value="中国分告传媒" disabled="disabled"/>
      </td>
      <td class="TableRow2">
       <div id="ownerMessage">请正确填写OPML的所有者</div>
      </td>
     </tr>
     <tr>
      <td class="TableRow2">请输入OPML所有者的联系邮箱:</td>
      <td class="TableRow2">
       <input type="text" id="opmlEmail" value=" >
      </td>
      <td class="TableRow2">
       <div id="emailMessage">请正确填写邮箱地址</div>
      </td>
     </tr>
     <tr>
      <td class="TableRow2">请输入OPML创建者的名字:</td>
      <td class="TableRow2">
       <input type="text" id="opmlBuilder" value="河南亿禧软件有限公司" disabled="disabled"/>
      </td>
      <td class="TableRow2">
       <div id="builderMessage">请正确填写创建者</div>
      </td>
     </tr>
     <tr>
      <td class="TableRow2">请输入网站的连接URL:</td>
      <td class="TableRow2">
       <input type="text" id="opmlUrl" value="
www.fansgoo.com" disabled="disabled"/>
      </td>
      <td class="TableRow2">
       <div id="urlMessage">请输入合法的网络地址</div>
      </td>
     </tr>
     <tr>
      <td class="TableRow2">请填写您生成文件前缀地址:</td>
      <td class="TableRow2">
       <input type="text" id="opmlPrefix" value=" http://www.fansgoo.com" disabled="disabled"/>
      </td>
      <td class="TableRow2">请填写您生成文件所在的位置</td>
     </tr>
     <tr>
      <td class="TableRow2">请输入您的生成的OPML名称:</td>
      <td class="TableRow2"><input type="text" id="opmlName" name="opmlName"/></td>
      <td class="TableRow2"><div id="nameMessage">请以英文名命名</div></td>
     </tr>
     <tr>
      <td class="TableRow2" colspan="3" align="center">
       <input type="button" id="startBuild" value="生成OPML" οnclick="buildOpml();"/>
      </td>
     </tr>
    </table>
   </div>
   <br />
   <jsp:include page="admin_bottom.jsp" />
  </div>
 </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值