?? cmsexport.java
字號:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/importexport/CmsExport.java,v $
* Date : $Date: 2006/03/27 14:52:54 $
* Version: $Revision: 1.84 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.importexport;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsFolder;
import org.opencms.file.CmsGroup;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsUser;
import org.opencms.file.CmsVfsException;
import org.opencms.i18n.CmsMessageContainer;
import org.opencms.loader.CmsLoaderException;
import org.opencms.main.CmsEvent;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.I_CmsEventListener;
import org.opencms.main.OpenCms;
import org.opencms.report.CmsShellReport;
import org.opencms.report.I_CmsReport;
import org.opencms.security.CmsAccessControlEntry;
import org.opencms.security.CmsRole;
import org.opencms.security.CmsRoleViolationException;
import org.opencms.util.CmsDateUtil;
import org.opencms.util.CmsFileUtil;
import org.opencms.util.CmsUUID;
import org.opencms.util.CmsXmlSaxWriter;
import org.opencms.workplace.CmsWorkplace;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXWriter;
import org.xml.sax.SAXException;
/**
* Provides the functionality to export files from the OpenCms VFS to a ZIP file.<p>
*
* The ZIP file written will contain a copy of all exported files with their contents.
* It will also contain a <code>manifest.xml</code> file in wich all meta-information
* about this files are stored, like permissions etc.<p>
*
* @author Alexander Kandzior
* @author Michael Emmerich
*
* @version $Revision: 1.84 $
*
* @since 6.0.0
*/
public class CmsExport {
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsExport.class);
private static final int SUB_LENGTH = 4096;
/** The CmsObject to do the operations. */
private CmsObject m_cms;
/** Max file age of contents to export. */
private long m_contentAge;
/** Counter for the export. */
private int m_exportCount;
/** Set of all exported pages, required for later page body file export. */
private Set m_exportedPageFiles;
/** Set of all exported files, required for later page body file export. */
private Set m_exportedResources;
/** The export ZIP file to store resources in. */
private String m_exportFileName;
/** Indicates if the user data and group data should be included to the export. */
private boolean m_exportUserdata;
/** Indicates if the webuser data should be included to the export. */
private boolean m_exportWebusers;
/** The export ZIP stream to write resources to. */
private ZipOutputStream m_exportZipStream;
/** Indicates if the system should be included to the export. */
private boolean m_includeSystem;
/** Indicates if the unchanged resources should be included to the export .*/
private boolean m_includeUnchanged;
/** Recursive flag, if set the folders are exported recursively. */
private boolean m_recursive;
/** The report for the log messages. */
private I_CmsReport m_report;
/** The top level file node where all resources are appended to. */
private Element m_resourceNode;
/** The SAX writer to write the output to. */
private SAXWriter m_saxWriter;
/** Cache for previously added super folders. */
private List m_superFolders;
/**
* Constructs a new uninitialized export, required for special subclass data export.<p>
*/
public CmsExport() {
// empty constructor
}
/**
* Constructs a new export.<p>
*
* @param cms the cmsObject to work with
* @param exportFile the file or folder to export to
* @param resourcesToExport the paths of folders and files to export
* @param includeSystem if true, the system folder is included
* @param includeUnchanged <code>true</code>, if unchanged files should be included
* @throws CmsImportExportException if something goes wrong
* @throws CmsRoleViolationException if the current user has not the required role
*/
public CmsExport(
CmsObject cms,
String exportFile,
List resourcesToExport,
boolean includeSystem,
boolean includeUnchanged)
throws CmsImportExportException, CmsRoleViolationException {
this(cms, exportFile, resourcesToExport, includeSystem, includeUnchanged, null, false, 0, new CmsShellReport(
cms.getRequestContext().getLocale()));
}
/**
* Constructs a new export.<p>
*
* @param cms the cmsObject to work with
* @param exportFile the file or folder to export to
* @param resourcesToExport the paths of folders and files to export
* @param includeSystem if true, the system folder is included
* @param includeUnchanged <code>true</code>, if unchanged files should be included
* @param moduleElement module informations in a Node for module export
* @param exportUserdata if true, the user and group data will also be exported
* @param exportWebusers if true, the webuser data will also be exported
* @param contentAge export contents changed after this date/time
* @param report to handle the log messages
* @param recursive recursive flag
*
* @throws CmsImportExportException if something goes wrong
* @throws CmsRoleViolationException if the current user has not the required role
*/
public CmsExport(
CmsObject cms,
String exportFile,
List resourcesToExport,
boolean includeSystem,
boolean includeUnchanged,
Element moduleElement,
boolean exportUserdata,
boolean exportWebusers,
long contentAge,
I_CmsReport report,
boolean recursive)
throws CmsImportExportException, CmsRoleViolationException {
setCms(cms);
setReport(report);
setExportFileName(exportFile);
// check if the user has the required permissions
cms.checkRole(CmsRole.EXPORT_DATABASE);
m_includeSystem = includeSystem;
m_includeUnchanged = includeUnchanged;
m_exportUserdata = exportUserdata;
m_exportWebusers = exportWebusers;
m_contentAge = contentAge;
m_exportCount = 0;
m_recursive = recursive;
// clear all caches
report.println(Messages.get().container(Messages.RPT_CLEARCACHE_0), I_CmsReport.FORMAT_NOTE);
OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, Collections.EMPTY_MAP));
try {
Element exportNode = openExportFile();
if (moduleElement != null) {
// add the module element
exportNode.add(moduleElement);
// write the XML
digestElement(exportNode, moduleElement);
}
exportAllResources(exportNode, resourcesToExport);
// export userdata and groupdata if selected
if (m_exportUserdata || m_exportWebusers) {
Element userGroupData = exportNode.addElement(CmsImportExportManager.N_USERGROUPDATA);
getSaxWriter().writeOpen(userGroupData);
exportGroups(userGroupData);
exportUsers(userGroupData);
getSaxWriter().writeClose(userGroupData);
exportNode.remove(userGroupData);
}
closeExportFile(exportNode);
} catch (SAXException se) {
getReport().println(se);
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_EXPORTING_TO_FILE_1,
getExportFileName());
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), se);
}
throw new CmsImportExportException(message, se);
} catch (IOException ioe) {
getReport().println(ioe);
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_EXPORTING_TO_FILE_1,
getExportFileName());
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), ioe);
}
throw new CmsImportExportException(message, ioe);
}
}
/**
* Constructs a new export.<p>
*
* @param cms the cmsObject to work with
* @param exportFile the file or folder to export to
* @param resourcesToExport the paths of folders and files to export
* @param includeSystem if true, the system folder is included
* @param includeUnchanged <code>true</code>, if unchanged files should be included
* @param moduleElement module informations in a Node for module export
* @param exportUserdata if true, the user and grou pdata will also be exported
* @param contentAge export contents changed after this date/time
* @param report to handle the log messages
*
* @throws CmsImportExportException if something goes wrong
* @throws CmsRoleViolationException if the current user has not the required role
*/
public CmsExport(
CmsObject cms,
String exportFile,
List resourcesToExport,
boolean includeSystem,
boolean includeUnchanged,
Element moduleElement,
boolean exportUserdata,
long contentAge,
I_CmsReport report)
throws CmsImportExportException, CmsRoleViolationException {
this(
cms,
exportFile,
resourcesToExport,
includeSystem,
includeUnchanged,
moduleElement,
exportUserdata,
contentAge,
report,
true);
}
/**
* Constructs a new export.<p>
*
* @param cms the cmsObject to work with
* @param exportFile the file or folder to export to
* @param resourcesToExport the paths of folders and files to export
* @param includeSystem if true, the system folder is included
* @param includeUnchanged <code>true</code>, if unchanged files should be included
* @param moduleElement module informations in a Node for module export
* @param exportUserdata if true, the user and grou pdata will also be exported
* @param contentAge export contents changed after this date/time
* @param report to handle the log messages
* @param recursive recursive flag
*
* @throws CmsImportExportException if something goes wrong
* @throws CmsRoleViolationException if the current user has not the required role
*/
public CmsExport(
CmsObject cms,
String exportFile,
List resourcesToExport,
boolean includeSystem,
boolean includeUnchanged,
Element moduleElement,
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -