ubbexport.java

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

JAVA
1,595 行
字号
                            // Same as thread creation date                            out.write("<CreationDate>");                            out.write(dateFormatter.format(messageCreationDate));                            out.write("</CreationDate><ModifiedDate>");                            out.write(dateFormatter.format(messageModifiedDate));                            out.write("</ModifiedDate>");                            //Write out properties of message.                            out.write("<PropertyList>");                            if (!isUser) {                                //Put the username and email in the extended properties to                                //mark this message as a "guest" message.                                if (!username.equals("")) {                                    out.write("<Property name=\"username\" ");                                    out.write("value=\"" + com.jivesoftware.util.StringUtils.escapeForXML(username) + "\"/>");                                }                                if (!email.equals("")) {                                    out.write("<Property name=\"email\" ");                                    out.write("value=\"" + com.jivesoftware.util.StringUtils.escapeForXML(email) + "\"/>");                                }                            }                            if (!IP.equals("")) {                                out.write("<Property name=\"IP\" ");                                out.write("value=\"" + IP + "\"/>");                            }                            if (!iconNumber.equals("")) {                                out.write("<Property name=\"iconNumber\" ");                                out.write("value=\"" + iconNumber + "\"/>");                            }                            out.write("</PropertyList>");                            out.write("</Message>");                        } // end of message processing                    } // end of token1 testing                } // end processing line            } // end process file            out.write("</MessageList>");            out.write("</Message>");            out.write("</Thread>");        } // End of directory processing        out.write("</ThreadList>");        out.write("</Forum>");        return -1;    }    public static int writeForumXML(File forumDir, int forumNumber, String name,                                    String description, Writer out) throws IOException    {        int messageCount = 0;        // Get the creation date and the modified date for this forum.        Date forumCreationDate = getForumCreationDate(forumDir, forumNumber);        Date forumModifiedDate = getForumModifiedDate(forumDir, forumNumber);        System.out.println("\n" + forumCreationDate.toString());        System.out.println("\n" + forumModifiedDate.toString());        // Write forum specific xml        out.write("<Forum><Name>");        out.write(StringUtils.escapeForXML(name));        out.write("</Name><Description>");        out.write(StringUtils.escapeForXML(description));        out.write("</Description><CreationDate>");        out.write(dateFormatter.format(forumCreationDate));        out.write("</CreationDate><ModifiedDate>");        out.write(dateFormatter.format(forumModifiedDate));        out.write("</ModifiedDate>");        // Begin processing messages        out.write("<ThreadList>");        //Read in each thread.        File threadList = new File(forumDir, "forum" + forumNumber + ".threads");        BufferedReader listIn = new BufferedReader(new FileReader(threadList));        String threadLine = null;        while ((threadLine = listIn.readLine()) != null) {            // Before we write out the thread, gather all the data for a thread            // and check it. Since we know the exact layout of the file, we'll            // only read the tokens we care about            FullStringTokenizer tokens = new FullStringTokenizer(threadLine, "|^|");            tokens.nextToken();            String threadID = tokens.nextToken();            String threadName = tokens.nextToken();            tokens.nextToken();            tokens.nextToken();            String modifiedDateString = tokens.nextToken();            Date creationDate = getThreadCreationDate(forumDir,threadID);            boolean threadInvalid = false;            if( (threadName==null || threadName.equals(""))                    || (modifiedDateString==null || modifiedDateString.equals(""))                    || (creationDate==null) )            {                threadInvalid = true;                println( "\nInvalid thread " + threadID + ", skipping" );            }            // Print out a thread            if( !threadInvalid ) {                Date modifiedDate = null;                try {                    modifiedDate = ubbThreadFormatter.parse(modifiedDateString);                }                catch (Exception e) {                    e.printStackTrace();                    println("invalid date= " + modifiedDateString);                }                out.write("<Thread>");                out.write("<CreationDate>");                out.write(dateFormatter.format(creationDate));                out.write("</CreationDate><ModifiedDate>");                //not ideal, but set the modified date to be the creation date                out.write(dateFormatter.format(modifiedDate));                out.write("</ModifiedDate>");                //The root message counts as one message:                messageCount++;                File messageList = new File(forumDir, threadID+".cgi");                BufferedReader messages = new BufferedReader(                        new FileReader(messageList)                );                // The second message in the file is the root message, so skip                // the first line                messages.readLine();                String rootMessage = messages.readLine();                out.write("<Message>");                writeMessageXML(out,threadName,creationDate,rootMessage);                //Now write out all messages below the current one.                boolean firstTime = true;                String messageLine = null;                String subject = StringUtils.escapeForXML("Re: " + threadName);                int counter = 0;                while ((messageLine = messages.readLine()) != null) {                    // print out a <MessageList> tag if this is the first time                    // in the loop                    if (firstTime) {                        out.write("<MessageList>");                        firstTime = false;                    }                    if (messageCount % 100 == 0) {                        print(".");                    }                    // Increment message count                    messageCount++;                    out.write("<Message>");                    writeMessageXML(out,subject,creationDate,messageLine);                    out.write("</Message>");                }                // If firstTime is false, that means we did print out a beginning                // <MessageList>, so we need to close it here                if (!firstTime) {                    out.write("</MessageList>");                }                out.write("</Message>");                out.write("</Thread>");                messages.close();            }        }        listIn.close();        out.write("</ThreadList>");        out.write("</Forum>");        return messageCount;    }    private static void writeMessageXML( Writer out, String subject, Date threadDate, String messageData )            throws IOException    {        FullStringTokenizer mTokens = new FullStringTokenizer(messageData, "||");        // skip the first 2 tokens        mTokens.nextToken(); mTokens.nextToken();        String username = mTokens.nextToken();        String date = mTokens.nextToken();        date += " " + mTokens.nextToken();        Date messageCreationDate = null;        try {            messageCreationDate = ubbMessageDateFormatter.parse(date);        } catch( ParseException e ) {            messageCreationDate = threadDate; // make equal to thread date        }        String email = mTokens.nextToken();        String body = mTokens.nextToken();        String IP = mTokens.nextToken();        mTokens.nextToken(); // "reg" status, ignore this, use "username" instead        String iconNumber = mTokens.nextToken();        // Convert the body        body = convertHTMLToNewlines(body);        body = toUBBCode(body);        body = StringUtils.escapeForXML(body);        // Output XML        out.write("<Subject>");        out.write(StringUtils.escapeForXML(subject));        out.write("</Subject><Body>");        out.write(body);        out.write("</Body>");        //If the regStats is "reg", it's a real user. Otherwise, a guest.        boolean isUser = (username!=null&&!username.equals(""));        if (isUser) {            out.write("<Username>");            out.write(StringUtils.escapeForXML(username));            out.write("</Username>");        }        out.write("<CreationDate>");        out.write(dateFormatter.format(messageCreationDate));        out.write("</CreationDate>");        out.write("<ModifiedDate>");        out.write(dateFormatter.format(messageCreationDate));        out.write("</ModifiedDate>");        //Write out properties of message.        out.write("<PropertyList>");        if (!isUser) {            //Put the username and email in the extended properties to            //mark this message as a "guest" message.            if (!username.equals("")) {                out.write("<Property name=\"username\" ");                out.write("value=\"" + StringUtils.escapeForXML(username) + "\"/>");            }            if (!email.equals("")) {                out.write("<Property name=\"email\" ");                out.write("value=\"" + StringUtils.escapeForXML(email) + "\"/>");            }        }        if (!IP.equals("")) {            out.write("<Property name=\"IP\" ");            out.write("value=\"" + IP + "\"/>");        }        if (!iconNumber.equals("")) {            out.write("<Property name=\"iconNumber\" ");            out.write("value=\"" + iconNumber + "\"/>");        }        out.write("</PropertyList>");        out.flush();    }    /**     * writeUserXML     *     * This method exports user details     *     */    public static int writeUserXML(File memberDir, Writer out) {        // Set default user count        int userCount = 0;        try {            out.write("<UserList>");            File [] files = memberDir.listFiles();            for (int i=0; i<files.length; i++) {                if (files[i].isFile()) {                    String filename = files[i].getName();                    //println("writeUserXML.filename " + filename);                    //We know that all user files should be 8 characters in                    //length and that they'll end in .cgi. This is a bit                    //"hackish" but should work with the UBB file format.                    if (filename.length() == 12 && filename.endsWith(".cgi")) {                        // Introduce a try catch to process most information if outofbounds exception occurs in loop                        // Convert file to an array                        String [] data = fileToArray(files[i]);                        // check for invalid user data (some user files might                        // be blank                        if( data.length > 0 ) {                            // Had a problem with out of bounds exceptions on dodgy data so included this try/catch                            // sorry i know its not nice butit works! martin farrell                            try {                                out.write("<User>");                                out.write("<Username>");                                // same in 6                                out.write(StringUtils.escapeForXML(data[0]));                                out.write("</Username><Password>");                                // same in 6                                String passwordHash = StringUtils.hash(data[1]);                                out.write(passwordHash);                                // same in 6                                boolean emailVisible = data[11].equals("yes");                                out.write("</Password><Email visible=\"" + emailVisible + "\">");                                // same in 6                                out.write(StringUtils.escapeForXML(data[2]));                                out.write("</Email>");                                // same in 6                                String userCreationDate                                        = dateFormatter.format(ubbFormatter.parse(data[10]));                                out.write("<CreationDate>");                                out.write(userCreationDate);                                out.write("</CreationDate><ModifiedDate>");                                out.write(userCreationDate);                                out.write("</ModifiedDate>");                                //All other ubb data is set as properties. They are:                                //URL, Occupat

⌨️ 快捷键说明

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