ubbexport.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,595 行 · 第 1/5 页

JAVA
1,595
字号
package com.jivesoftware.forum.tool;import java.io.*;import java.util.*;import java.text.*;import com.jivesoftware.util.StringUtils;import com.jivesoftware.util.UnicodeFilterWriter;import com.jivesoftware.oro.text.*;import com.jivesoftware.oro.text.perl.*;/** * A utility to export UBB data in the Jive XML format. */public class UBBExport {    /**     * Standard Jive XML date format (don't modify this, as this format     * is exptected in the import and export classes).     */    public static final String XML_DATE_FORMAT = "yyyy/MM/dd HH:mm:ss.SS z";    /**     * The version number of the tool.     */    public static final String TOOL_VERSION = "1.0";    /**     * The version number of the Jive XML format that the tool uses.     */    public static final String XML_VERSION = "1.0";    /**     * UBB Version - added by Martin Farrell     *     * this ensures that the UBBExport tool can work with both UBB5 and UBB6     *     * Default version is 5     */    public static int UBB_VERSION = 6;    /**     * A simple date formatter that will convert Date objects to the Jive     * date format. For example: 1978/11/15 12:00:00.00 PST     */    private static SimpleDateFormat dateFormatter =            new SimpleDateFormat(XML_DATE_FORMAT);    private static SimpleDateFormat fileDateFormatter =            new SimpleDateFormat("yyyy-MM-dd");    private static SimpleDateFormat ubbFormatter =            new SimpleDateFormat("MM-dd-yy");    private static SimpleDateFormat ubbThreadFormatter =            new SimpleDateFormat("yyyyMMddHHmm");    private static SimpleDateFormat ubbMessageDateFormatter =            new SimpleDateFormat("MM-dd-yy hh:mm a");    private static String ubbcgiDir = null;    private static String ubbDir = null;    private static String usersFile = "ubbUsers-" + fileDateFormatter.format(new Date()) + ".xml";    private static String fullFile = "ubbData-" + fileDateFormatter.format(new Date()) + ".xml";    // Amendment based on standard build    private static final String[] HTMLToUBBCode = {        // Images        "s%<img src=\"smile.gif\" border=\"0\">%:-)%isg",        "s%<img src=\"frown.gif\" border=\"0\">%:-(%isg",        "s%<img src=\"biggrin.gif\" border=\"0\">%:D%isg",        "s%<img src=\"rolleyes.gif\" border=\"0\">%;\\%isg",        "s%<img src=\"cool.gif\" border=\"0\">%B-)%isg",        "s%<img src=\"tongue.gif\" border=\"0\">%:p%isg",        "s%<img src=\"wink.gif\" border=\"0\">%;-)%isg",        "s%<img src=\"redface.gif\" border=\"0\">%:8}%isg",        "s%<img src=\"confused.gif\" border=\"0\">%?:|%isg",        "s%<img src=\"eek.gif\" border=\"0\">%?O%isg",        // Images        "s%<IMG SRC=\"(\\S+?)\".*?>%$1%isg",        "s%<IMG SRC=\"(\\S+?)\".*?ALT=\"(.*?)\".*?>%$1%isg",        // Links        "s%<A HREF=\"mailto:(\\S+?)\".*?>(.+?)</A>%$1%isg",        "s%<A HREF=mailto:(\\S+?).*?>(.+?)</A>%$1%isg",        "s%<A HREF=\"(\\S+?)\".*?>(.+?)</A>%$1%isg",        "s%<A HREF=(\\S+?).*?>(.+?)</A>%$1%isg",        // Quotes        "s%<BLOCKQUOTE><FONT.*?>.*?by (.+?):</FONT><HR>(.+?)<HR></BLOCKQUOTE>%$1%isg",        "s%<BLOCKQUOTE><FONT.*?>quote:</FONT><HR>(.+?)<HR></BLOCKQUOTE>%$1%isg"    };    // Static compiled regexp patterns    // So when a filter is created we don't have to recompile the regexp    // Set to a capacity of 100 unique patterns, this should be enough    // for most users.    private static PatternCacheLRU regexp = new PatternCacheLRU(100);    private static Perl5Util perl = new Perl5Util(regexp);    /**     * Called to change the version of UBB     *     */    public static void changeVersion() {        // Prompt user        println(" ");        println("Enter either 5 for UBB version 5 or 6 for UBB version 6:");        println("[" + UBB_VERSION + "]");        print("> ");        // Read in users selection        String version = readLine();        println("UBB_VERSION *" + version + "*");        if (!version.equals("") && version != null) {            int ubb_version = new Integer(version).intValue();            // Set UBB Version            if (ubb_version == 5 || ubb_version == 6) {                UBB_VERSION = ubb_version;            }        }    }    /**     *  Converts the HTML &lt;br&gt; and &lt;p&gt; tags into actual newline     *  characters.     */    public static String convertHTMLToNewlines(String string) {        string = StringUtils.replaceIgnoreCase(string, "<br>", "\n");        return StringUtils.replaceIgnoreCase(string, "<p>", "\n\n");    }    /**     * Reads a text file and returns it as an array of Strings.     */    public static String [] fileToArray(File file) {        try {            // Convert File to Buffered stream            BufferedReader in = new BufferedReader(new FileReader(file));            String line = null;            // Generate store array list and read in files            ArrayList buffer = new ArrayList();            while ((line = in.readLine()) != null) {                buffer.add(line);            }            // Process array list into array            String [] results = new String[buffer.size()];            for (int i=0; i<results.length; i++) {                results[i] = (String)buffer.get(i);            }            // close buffered stream            in.close();            return results;        }        catch (Exception e) {            e.printStackTrace();            return new String [] { };        }    }    /**     * Exports a single forum. Note: this method only supports UBB6     */    public static void forumExport() {        // Check the directories and retrieve them        File ubbDir = getUbbDirectory();        File ubbCGIDir = getUbbCGIDirectory();        // Get forum info        File forumsFile = new File(ubbCGIDir, "vars_forums.cgi");        if (!forumsFile.exists()) {            println("There seems to be an error - please check your file structures for vars_forums.cgi");            return;        }        // Parse data to an array        int forumCount = 0;        String [] forumData = fileToArray(forumsFile);        forumCount = forumData.length;        // Output number of users found to export and number of forums to export.        String foundString = "Found " + forumCount + " forum";        if (forumCount > 1) {            foundString += "s";        }        println(foundString);        println(" ");        println("Enter the number corresponding to the forum that you'd like to export.");        // Generate a list of forums        for (int i = 0; i<forumCount; i++) {            FullStringTokenizer tokens = new FullStringTokenizer(forumData[i], "|");            // Create default string holder            String nt = null;            // Create holder string            String forumName = null;            int forumID = -1;            for (int j = 0; j < 18; j++) {                // Process Required tokens                if ( j == 3 ) {                    forumName = nt;                }                else if (j==17) {                    forumID = Integer.parseInt(nt);                    println(" (" + (i+1) + ") Forum #" + forumID + " --  " + forumName);                }                nt = tokens.nextToken();            } // end forum line counter        } // end forum file loop        print("> ");        String response = readLine();        println(" ");        int forumChoice = -1;        try {            // get users choice            forumChoice = Integer.parseInt(response);        } catch (NumberFormatException nfe) {            println("ERROR - command not recognized. Please try again.");        }        // Get output file name        println(" ");        println("Enter the name of the file you'd like to write the data out to: ");        println("[" + fullFile + "] >");        String xmlName = readLine();        if (xmlName.equals("")) {            xmlName = fullFile;        }        fullFile = xmlName;        // Get users select output forum        boolean done = false;        while (!done) {            println(" ");            println("Ready to begin export. Enter the number of the command you'd like to perform:");            println(" (1) Proceed with export to file \"" + xmlName + "\".");            println(" (2) Abandon export.");            print("> ");            response = readLine();            println(" ");            try {                int choice = Integer.parseInt(response);                switch (choice) {                    case 1:                        println("Starting data export...");                        // Output data to file. Write data as UTF-8 so that                        // extended chars turn out correctly                        try {                            Writer out = new UnicodeFilterWriter(                                    new BufferedWriter(                                            new OutputStreamWriter(                                                    new FileOutputStream(xmlName),                                                    "UTF-8"),2097152));                            out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");                            out.write("<!DOCTYPE Jive SYSTEM \"http://www.jivesoftware.com/jive.dtd\">");                            out.write("<Jive xmlversion=\"" + XML_VERSION + "\" exportDate=\"" +                                    dateFormatter.format(new Date()) + "\">"                            );                            out.write("<ForumList>");                            String nt = null; // set next token string                            String forumName = null; // Holder for forumName                            String forumDescription = null; // Holder for forumDescription                            int fileForumId = -1; // forum id from file                            // Generate a list of forums                            for (int i = 0; i<forumCount; i++) {                                FullStringTokenizer tokens = new FullStringTokenizer(forumData[i], "|");                                // process line                                for (int j = 0; j < 18; j++) {                                    // Process Required tokens                                    if (j == 3) {                                        forumName = nt;                                    } else if (j == 5) {                                        forumDescription = nt;                                    } else if (j == 17) {                                        fileForumId = Integer.parseInt(nt);                                        // check that this isnt the selected forum id                                        if (forumChoice == fileForumId) {                                            println("forumChoice matches fileForumID");                                            // set forum dir                                            File forumDir = new File (ubbDir, "Forum" + forumChoice);                                            // check forum dir if exists                                            if (forumDir.exists()) {                                                println("Exporting data from " + forumDir.getAbsolutePath());                                                int messageCount = writeForumXMLUBBV6(forumDir, forumChoice, forumName, forumDescription, out);                                                println("Finished exporting");                                            }                                        }                                    }                                    nt = tokens.nextToken();                                } // end forum line counter                            } // end forum file loop                            out.write("</ForumList>");                            out.write("</Jive>");                            out.close();

⌨️ 快捷键说明

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