📄 ubbexport.java
字号:
File threadList = new File(forumDir, "forum" + forumNumber + ".threads");
// Convert that file listing to an array of strings
String[] lines = fileToArray(threadList);
// Loop through the array starting at the end (because the most recent
// thread is at the end. We loop because we want to find a valid thread
// listing and the file may contain invalid thread data.
for( int i=lines.length-1; i>=0; i-- ) {
// Tokenize the string
FullStringTokenizer tokens = new FullStringTokenizer(lines[i],"|^|");
// The Julian date is the first token:
try {
double jDate = Double.parseDouble(tokens.nextToken());
//forumModifiedDate = (JulianDate.computeGregorianDate(jDate)).getTime();
forumModifiedDate = (new JulianDate(jDate)).getGregorianDate().getTime();
tokens.nextToken();tokens.nextToken();tokens.nextToken();tokens.nextToken();
//Date d = ubbThreadFormatter.parse(tokens.nextToken());
//getGregorianDate(jDate);
//System.err.println("date was: " + forumModifiedDate.toString());
//System.err.println("time diff: " +
// (forumModifiedDate.getTime() - d.getTime()) );
// At this point, we've found the correct date, so break out
// of this loop
break;
}
catch( Exception e ) {e.printStackTrace();}
}
System.out.println("forum mod date: " + forumNumber + " " + forumModifiedDate.toString());
return forumModifiedDate;
/*
BufferedReader listIn = new BufferedReader(new FileReader(threadList));
String threadLine = null;
int threadCount = 0;
while ((threadLine = listIn.readLine()) != null) {
if( (threadCount++ % 50) == 0 ) {
print(".");
}
FullStringTokenizer tokens = new FullStringTokenizer(threadLine, "|^|");
String jDate = tokens.nextToken();
// convert julian date:
double julianDate = 0.0;
try {
julianDate = Double.parseDouble(jDate);
}
catch(Exception e) {}
Date threadModDate = (JulianDate.computeGregorianDate(julianDate)).getTime();
String threadID = tokens.nextToken();
File threadFile = new File( forumDir, threadID+".cgi" );
if( threadFile.exists() ) {
BufferedReader messages = new BufferedReader(
new FileReader(threadFile)
);
messages.readLine(); // skip the first line
String message = null;
while( (message=messages.readLine()) != null ) {
FullStringTokenizer mTokens = new FullStringTokenizer(message,"||");
mTokens.nextToken(); mTokens.nextToken(); mTokens.nextToken();
String date = mTokens.nextToken();
date += " " + mTokens.nextToken();
Date d = new Date(0L);
try {
d = ubbMessageDateFormatter.parse(date);
} catch( ParseException e ) {
System.err.println("(getForumModifiedDate): unparseable date: " + date);
}
if( d.getTime() > modifiedDate.getTime() ) {
modifiedDate = d;
}
}
messages.close();
}
System.err.println("\n");
System.err.println("for this thread,");
System.err.println("julian date: " + threadModDate.toString() );
System.err.println("old date: " + modifiedDate.toString() );
}
listIn.close();
return modifiedDate;
*/
}
private static Date getThreadCreationDate( File forumDir, String threadID )
throws IOException
{
Date creationDate = null;
File messageList = new File(forumDir, threadID+".cgi");
if( messageList.exists() ) {
BufferedReader messages = new BufferedReader(
new FileReader(messageList)
);
// skip the first line
messages.readLine();
String rootMessage = messages.readLine();
if( rootMessage == null ) {
return null;
}
FullStringTokenizer mTokens = new FullStringTokenizer(rootMessage,"||");
// skip the first 3 tokens
mTokens.nextToken(); mTokens.nextToken(); mTokens.nextToken();
String date = mTokens.nextToken();
date += " " + mTokens.nextToken();
try {
creationDate = ubbMessageDateFormatter.parse(date);
} catch( ParseException e ) {
System.err.println("(getThreadCreationDate): unparseable date: " + date);
}
messages.close();
}
return creationDate;
}
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();
}
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>");
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 /*&& counter++<500*/ ) {
// print out a <MessageList> tag if this is the first time
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -