⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rssgenerator.java

📁 源码/软件简介: 云网论坛1.1RC国际版是采用JSP开发的集论坛、CMS(网站内容管理系统)、博客、聊天室、商城、交友、语音灌水等于一体的门户式社区。拥有CWBBS ( Cloud Web BBS
💻 JAVA
字号:
package com.redmoon.forum.tools;

/**
 * <p>Title: 输出RSS</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */

import com.sun.syndication.feed.synd.*;
import com.sun.syndication.io.SyndFeedOutput;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import com.redmoon.forum.MsgDb;
import org.apache.log4j.Logger;
import cn.js.fan.web.Global;
import com.redmoon.forum.Leaf;
import cn.js.fan.util.StrUtil;
import javax.servlet.jsp.JspWriter;
import com.redmoon.forum.ThreadBlockIterator;
import com.redmoon.forum.SQLBuilder;
import com.redmoon.blog.UserConfigDb;
import com.redmoon.blog.UserDirDb;

/**
 * @author   bbjwerner
 */
public class RSSGenerator {
    Logger logger = Logger.getLogger(RSSGenerator.class.getName());

    public final int CONTENT_LENGTH = 500;

    private static String fileName = null;
    private static String feedType = null;

    public RSSGenerator() {
    }

    public void generateForumRSS(JspWriter out, String feedType, int n) {
        this.feedType = feedType;
        populateForumArrayToJSP(out, n);
    }

    public void generateBoardRSS(JspWriter out, String feedType, int n, String boardCode) {
        this.feedType = feedType;
        populateBoardArrayToJSP(out, n, boardCode);
    }

    public void generateForumRSS(String feedType, String fileName, int n) {
        this.feedType = feedType;
        this.fileName = fileName;
        populateForumArrayToFile(n);
    }

    public void generateUserBlogRSS(JspWriter out, String feedType, int n, String userName, String blogUserDir) {
        this.feedType = feedType;
        this.fileName = fileName;
        populateUserBlogArrayToJSP(out, n, userName, blogUserDir);
    }

    private void populateForumArrayToFile(int n) {
        Leaf lf = new Leaf();
        ArrayList entries = new ArrayList();
        ArrayList categories = new ArrayList();
        MsgDb md = new MsgDb();
        long[] newIds = md.getForumNewMsgIds(n);
        int len = newIds.length;
        for (int i=0; i<len; i++) {
            md = md.getMsgDb((int)newIds[i]);
            addEntry(entries, categories, md.getTitle(),
                     Global.getRootPath() + "/forum/showtopic.jsp?rootid=" +
                     md.getId(),
                     md.getAddDate(),
                     StrUtil.getLeft(md.getContent(), CONTENT_LENGTH),
                     lf.getLeaf(md.getboardcode()).getName(),
                     md.getName());
        }

        this.doSyndicationToFile(entries, Global.AppName,
                           Global.getRootPath() + "/forum/index.jsp",
                           Global.desc,
                           Global.copyright,
                               fileName);
    }

    private void populateForumArrayToJSP(JspWriter out, int n) {
        Leaf lf = new Leaf();
        ArrayList entries = new ArrayList();
        ArrayList categories = new ArrayList();
        MsgDb md = new MsgDb();
        long[] newIds = md.getForumNewMsgIds(n);
        int len = newIds.length;
        for (int i=0; i<len; i++) {
            md = md.getMsgDb((int)newIds[i]);
            addEntry(entries, categories, md.getTitle(),
                     Global.getRootPath() + "/forum/showtopic.jsp?rootid=" +
                     md.getId(),
                     md.getAddDate(),
                     md.getContent(),
                     lf.getLeaf(md.getboardcode()).getName(),
                     md.getName());
        }

        this.doSyndicationToJSP(entries, Global.AppName,
                           Global.getRootPath() + "/forum/index.jsp",
                           Global.desc,
                           Global.copyright, out);
    }

    private void populateBoardArrayToJSP(JspWriter out, int n, String boardCode) {
        Leaf lf = new Leaf();
        lf = lf.getLeaf(boardCode);
        if (lf==null || !lf.isLoaded())
            return;
        ArrayList entries = new ArrayList();
        ArrayList categories = new ArrayList();
        MsgDb md = new MsgDb();

        String sql = SQLBuilder.getListtopicSql(boardCode, "", "all"); // "select id from sq_thread where boardcode="+StrUtil.sqlstr(boardCode)+" and level<=" + MsgDb.LEVEL_TOP_BOARD + " ORDER BY level desc,redate desc";

        ThreadBlockIterator ir = md.getThreads(sql, boardCode, 0, n-1);
        while (ir.hasNext()) {
            md = (MsgDb) ir.next();
            addEntry(entries, categories, md.getTitle(),
                     Global.getRootPath() + "/forum/showtopic.jsp?rootid=" +
                     md.getId(),
                     md.getAddDate(),
                     md.getContent(),
                     lf.getName(),
                     md.getName());
        }

        this.doSyndicationToJSP(entries, Global.AppName + "-" + lf.getName(),
                           Global.getRootPath() + "/forum/index.jsp",
                           Global.desc,
                           Global.copyright, out);
    }

    private void populateUserBlogArrayToJSP(JspWriter out, int n, String userName, String blogUserDir) {
        ArrayList entries = new ArrayList();
        ArrayList categories = new ArrayList();

        UserConfigDb ucd = new UserConfigDb();
        ucd = ucd.getUserConfigDb(userName);
        if (ucd==null || !ucd.isLoaded()) {
            return;
        }

        UserDirDb udd = new UserDirDb();
        udd = udd.getUserDirDb(userName, blogUserDir);
        String blogUserDirName = "";
        if (udd!=null && udd.isLoaded())
            blogUserDirName = udd.getDirName();

        String sql = SQLBuilder.getMyblogSql(blogUserDir, userName);
        MsgDb md = new MsgDb();
        ThreadBlockIterator ir = md.getThreads(sql, md.getVirtualBoardcodeOfBlogUser(userName, blogUserDir), 0, n-1);
        while (ir.hasNext()) {
            md = (MsgDb) ir.next();
            addEntry(entries, categories, md.getTitle(),
                     Global.getRootPath() + "/forum/showblog.jsp?rootid=" +
                     md.getId(),
                     md.getAddDate(),
                     md.getContent(),
                     ucd.getTitle(),
                     md.getName());
        }

        if (blogUserDirName.equals(""))
            doSyndicationToJSP(entries, Global.AppName + "-blog-" + ucd.getTitle(),
                           Global.getRootPath() + "/blog/index.jsp",
                           Global.desc,
                           Global.copyright, out);
        else
            doSyndicationToJSP(entries, Global.AppName + "-blog-" + ucd.getTitle() + "-" + blogUserDirName,
                           Global.getRootPath() + "/blog/index.jsp",
                           Global.desc,
                           Global.copyright, out);
    }

    /**
     * This method adds an entry to the ArrayList() which will be published when GenerateRSS()
     * is called.
     * <p>
     * @param title The title of the blog entry (not the blog itself)
     * @param link The PermaLink that will point to your entry
     * @param date The date of the blog entry
     * @param blogContent The content or synopsis you wish to publish
     * @param cat The category of the entry. This has been added to integrate
     *        with Technorati and match WordPress functionality
     * @param author The author of the entry to be published.
     *
     */
    private void addEntry(ArrayList entries, ArrayList categories, String title, String link, java.util.Date date,
                          String content, String cat, String author) {

        try {
            SyndEntry entry;
            SyndContent description;
            SyndCategory category;

            entry = new SyndEntryImpl();
            entry.setAuthor(author);
            entry.setTitle(title);
            entry.setLink(link);
            // System.out.println("RSSGenerator: date=" + date);
            entry.setPublishedDate(date);
            description = new SyndContentImpl();
            description.setType("text/plain");
            description.setValue(content);
            entry.setDescription(description);

            category = new SyndCategoryImpl();
            category.setName(cat);
            categories.add(category);
            entry.setCategories(categories);
            categories.remove(category);
            entries.add(entry);
        }
        catch (Exception ex) {
            ex.printStackTrace();
            logger.error("addEntry: " + ex.getMessage());
        }

    }

    /**
     * This method is called last after you have added all your entries and have specified your
     * feed type and filename. This actually does the work
     * <p>
     * NOTE: This has static content entered in to the fields! You must have access to the source
     * code edit this method or else you will be publishing content as the Post Modern Banter Blog
     * Yes, I should change this immediately. Ideally, it would take values from the web.xml file itself.
     * <p>
     * @throws Exception
     */
    private void doSyndicationToFile(ArrayList entries, String title, String link,
                               String description_loc, String copyright,
                               String xml) {
        try {
            final SyndFeed feed = new SyndFeedImpl();
            feed.setFeedType(feedType);

            feed.setTitle(title);
            feed.setLink(link);
            feed.setDescription(description_loc);
            feed.setCopyright(copyright);

            feed.setEntries(entries);

            final Writer writer = new FileWriter(xml);
            final SyndFeedOutput output = new SyndFeedOutput();
            output.output(feed, writer);
            writer.close();
            System.out.println(
                    "************* The feed has been written to the file [" +
                    xml + "]");
        }
        catch (Exception ex) {
            ex.printStackTrace();
            logger.error("doSyndication: " + ex.getMessage());
        }
    }

    /**
     * 输出至页面,rss_2.0时,在rss.jsp页面中有时看不到输出
     * @param entries ArrayList
     * @param title String
     * @param link String
     * @param description_loc String
     * @param copyright String
     * @param out JspWriter
     */
    private void doSyndicationToJSP(ArrayList entries, String title, String link,
                               String description_loc, String copyright,
                               JspWriter out) {
        try {
            final SyndFeed feed = new SyndFeedImpl();
            feed.setFeedType(feedType);

            feed.setTitle(title);
            feed.setLink(link);
            feed.setDescription(description_loc);
            feed.setCopyright(copyright);

            feed.setEntries(entries);
            SyndImage si = new SyndImageImpl();
            si.setTitle(title);
            si.setLink(link);
            si.setUrl(Global.getRootPath() + "/logo.gif");
            feed.setImage(si);

            // final Writer writer = new FileWriter(xml);
            final SyndFeedOutput output = new SyndFeedOutput();
            output.output(feed, out);
            out.flush();
            // out.close();
            // System.out.println(
            //         "RSSGenerator: doSyndicationToJSP--The feed has been written!");
        }
        catch (Exception ex) {
            ex.printStackTrace();
            logger.error("doSyndication: " + ex.getMessage());
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -