?? importmvnforum.java
字號(hào):
/*
* $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/importexport/mvnforum/ImportMvnForum.java,v 1.12 2006/04/14 17:36:29 minhnn Exp $
* $Author: minhnn $
* $Revision: 1.12 $
* $Date: 2006/04/14 17:36:29 $
*
* ====================================================================
*
* Copyright (C) 2002-2006 by MyVietnam.net
*
* All copyright notices regarding mvnForum MUST remain
* intact in the scripts and in the outputted HTML.
* The "powered by" text/logo with a link back to
* http://www.mvnForum.com and http://www.MyVietnam.net in
* the footer of the pages MUST remain visible when the pages
* are viewed on the internet or intranet.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Support can be obtained from support forums at:
* http://www.mvnForum.com/mvnforum/index
*
* Correspondence and Marketing Questions can be sent to:
* info at MyVietnam net
*
* @author: Igor Manic
*/
package com.mvnforum.admin.importexport.mvnforum;
import java.io.*;
import java.util.Calendar;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mvnforum.MVNForumConfig;
import com.mvnforum.MVNForumGlobal;
import com.mvnforum.admin.ImportWebHelper;
import com.mvnforum.admin.importexport.SetParentRule;
import net.myvietnam.mvncore.exception.*;
import org.apache.commons.digester.Digester;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.SAXException;
/**
* @author Igor Manic
* @version $Revision: 1.12 $, $Date: 2006/04/14 17:36:29 $
* <br/>
* <code>ImportMvnForum</code> class encapsulates processing
* of MVN Forum's XML or ZIP backups, and imports all the data into MVN Forum.
* For details see {@link #importXml(File, HttpServletRequest, HttpServletResponse, int, String, Calendar, String, boolean, int)}
* and {@link #importZip(File, HttpServletRequest, HttpServletResponse, int, String, Calendar, String, boolean, int)}
* <br/>
* This class cannot be instantiated.
*/
public class ImportMvnForum extends ImportWebHelper {
/** Message log. */
private static Log log = LogFactory.getLog(ImportMvnForum.class);
/** Cannot instantiate. */
private ImportMvnForum() {
}
// =================================================================
// ===================== MAIN PUBLIC METHODS =======================
// =================================================================
/**
* This method performs processing of MVN Forum's XML backup file <code>importFile</code>
* and imports the data into the MVN Forum system. It clears the database
* and files, does neccessary setup (including startup of message output),
* and calls {@link #processXml(File, Calendar)} to do actual processing.<br/>
*
* @param importFile MVN Forum XML backup file to be imported.
* @param request Current session's <code>HttpServletRequest</code> object.
* @param response Current session's <code>HttpServletResponse</code> object.
* @param logonMemberID MemberID of user who is logged in, and who initiated import process.
* @param logonMemberName MemberName of user who is logged in, and who initiated import process.
* @param importTime The moment when import process was started.
* @param importIP From this IP address admin requested import.
* @param clearIfError Should it clear/reset the database in case of error.
* @param messageLevel What messages should be written to output during the process.
* For details see {@link com.mvnforum.MVNForumConfig#MESSAGE_LEVEL_ALL_MESSAGES},
* {@link com.mvnforum.MVNForumConfig#MESSAGE_LEVEL_IMPORTANT_MESSAGES} and
* {@link com.mvnforum.MVNForumConfig#MESSAGE_LEVEL_ONLY_ERRORS}.
*
* @exception ImportException If there is an error during the process. See {@link net.myvietnam.mvncore.exception.ImportException}.
*/
public static void importXml(File importFile,
HttpServletRequest request, HttpServletResponse response,
int logonMemberID, String logonMemberName,
Calendar importTime, String importIP,
boolean clearIfError, int messageLevel)
throws ImportException {
try {
response.setContentType("text/html; charset=utf-8");
setOutputHtmlWriter(response.getWriter());
setMessageOutputLevel(messageLevel);
startHtml(request);
clearDatabase();
clearFiles(request.getSession().getServletContext());
} catch (DatabaseException e) {
handleFatalError("Database error while clearing previous contents.",
e, clearIfError, request);
} catch (IOException e) {
handleFatalError("I/O error while clearing previous contents.",
e, clearIfError, request);
}
try {
processXml(importFile, importTime);
handleSuccess(request);
} catch (ImportException e) {
handleFatalError(e.getMessage(), e.getException(),
clearIfError, request);
} catch (IOException e) {
handleFatalError("I/O error while reading XML file.",
e, clearIfError, request);
} catch (SAXException e) {
if (e.getException()==null) {
handleFatalError("Error while parsing uploaded XML file.",
e, clearIfError, request);
} else {
handleFatalError("Error while parsing uploaded XML file. Detail: "+
e.getException().getMessage(), e.getException(),
clearIfError, request);
}
} finally {
/* Don't delete this XML since this method was maybe started from the
* command-line, which means this file is not temporary (uploaded)
* //if ((importFile!=null) && (importFile.exists())) importFile.delete();
* Anyway, if neccessary, this XML will be deleted in calling WebHandler
*/
}
}
/**
* This method performs processing of MVN Forum's ZIP backup file <code>importFile</code>
* and imports the data into the MVN Forum system. It clears the database
* and files, does neccessary setup (including startup of message output),
* extracts the files from ZIP, and calls {@link #processXml(File, Calendar)}
* to do actual processing of the main XML file (found in ZIP).<br/>
*
* @param importFile MVN Forum ZIP backup file to be imported.
* @param request Current session's <code>HttpServletRequest</code> object.
* @param response Current session's <code>HttpServletResponse</code> object.
* @param logonMemberID MemberID of user who is logged in, and who initiated import process.
* @param logonMemberName MemberName of user who is logged in, and who initiated import process.
* @param importTime The moment when import process was started.
* @param importIP From this IP address admin requested import.
* @param clearIfError If <code>true</code>, the database will be cleared/reset
* in case of error rises during the import. Default <code>Guest</code>
* and <code>Admin</code> users will be created.
* @param messageLevel What amount of messages (informational, important, error)
* will be written to output.
*
* @exception ImportException If there is an error during the process. See {@link net.myvietnam.mvncore.exception.ImportException}.
*/
public static void importZip(File importFile,
HttpServletRequest request, HttpServletResponse response,
int logonMemberID, String logonMemberName,
Calendar importTime, String importIP,
boolean clearIfError, int messageLevel)
throws ImportException {
File importXml=null;
try {
response.setContentType("text/html; charset=utf-8");
setOutputHtmlWriter(response.getWriter());
setMessageOutputLevel(messageLevel);
startHtml(request);
clearDatabase();
clearFiles(request.getSession().getServletContext());
} catch (DatabaseException e) {
handleFatalError("Database error while clearing previous contents.",
e, clearIfError, request);
} catch (IOException e) {
handleFatalError("I/O error while clearing previous contents.",
e, clearIfError, request);
}
/* Now extract ZIP file into mvnForumHome, and give me back only XML file.
* IMPORTANT: DON'T CALL clearFiles() AFTER THE EXTRACTION OF THE ZIP!
* Otherwise, all extracted files will be deleted too
*/
try {
addImportantMessage("Extracting the ZIP file.");
importXml=extractImportZip(importFile, request);
if (importXml==null) {
handleFatalError("Error while extracting the file: can't find IMPORT.xml in ZIP file.",
null/*error*/, clearIfError, request);
}
} catch (IOException e) {
handleFatalError("I/O error while extracting the file.",
e, clearIfError, request);
} finally {
importFile.delete(); importFile=null;
}
//from this point on, use only importXml file (importFile is deleted)
try {
processXml(importXml, importTime);
handleSuccess(request);
} catch (ImportException e) {
handleFatalError(e.getMessage(), e.getException(),
clearIfError, request);
} catch (IOException e) {
handleFatalError("I/O error while reading XML file.",
e, clearIfError, request);
} catch (SAXException e) {
if (e.getException()==null) {
handleFatalError("Error while parsing uploaded XML file.",
e, clearIfError, request);
} else {
handleFatalError("Error while parsing uploaded XML file. Detail: "+
e.getException().getMessage(), e.getException(),
clearIfError, request);
}
} finally {
//now delete temporary XML file (extracted from ZIP, and already imported to database)
if ((importFile!=null) && (importFile.exists())) importFile.delete();
if ((importXml!=null) && (importXml.exists())) importXml.delete();
}
}
/**
* This method should unpack the zip into mvnForumHome.
* It should also return importXml file (which was also extracted)
*/
private static File extractImportZip(File importZipFile, HttpServletRequest request)
throws IOException {
String avatarsDir = request.getSession().getServletContext().getRealPath(MVNForumGlobal.UPLOADED_AVATAR_DIR);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -