📄 cmsexport.java
字号:
List resourcesToExport,
boolean includeSystem,
boolean includeUnchanged,
Element moduleElement,
boolean exportUserdata,
long contentAge,
I_CmsReport report,
boolean recursive,
boolean inProject)
throws CmsImportExportException, CmsRoleViolationException {
setCms(cms);
setReport(report);
setExportFileName(exportFile);
// check if the user has the required permissions
OpenCms.getRoleManager().checkRole(cms, CmsRole.DATABASE_MANAGER);
m_includeSystem = includeSystem;
m_includeUnchanged = includeUnchanged;
m_exportUserdata = exportUserdata;
m_contentAge = contentAge;
m_exportCount = 0;
m_recursive = recursive;
m_inProject = inProject;
// 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) {
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);
}
}
/**
* Exports the given folder and all child resources.<p>
*
* @param folderName to complete path to the resource to export
* @throws CmsImportExportException if something goes wrong
* @throws SAXException if something goes wrong procesing the manifest.xml
* @throws IOException if not all resources could be appended to the ZIP archive
*/
protected void addChildResources(String folderName) throws CmsImportExportException, IOException, SAXException {
try {
// get all subFolders
List subFolders = getCms().getSubFolders(folderName, CmsResourceFilter.IGNORE_EXPIRATION);
// get all files in folder
List subFiles = getCms().getFilesInFolder(folderName, CmsResourceFilter.IGNORE_EXPIRATION);
// walk through all files and export them
for (int i = 0; i < subFiles.size(); i++) {
CmsResource file = (CmsResource)subFiles.get(i);
CmsResourceState state = file.getState();
long age = file.getDateLastModified() < file.getDateCreated() ? file.getDateCreated()
: file.getDateLastModified();
if (getCms().getRequestContext().currentProject().isOnlineProject()
|| (m_includeUnchanged)
|| state.isNew()
|| state.isChanged()) {
if (!state.isDeleted() && !CmsWorkplace.isTemporaryFile(file) && (age >= m_contentAge)) {
String export = getCms().getSitePath(file);
if (checkExportResource(export)) {
if (isInExportableProject(file)) {
exportFile(getCms().readFile(export, CmsResourceFilter.IGNORE_EXPIRATION));
}
}
}
}
// release file header memory
subFiles.set(i, null);
}
// all files are exported, release memory
subFiles = null;
// walk through all subfolders and export them
for (int i = 0; i < subFolders.size(); i++) {
CmsResource folder = (CmsResource)subFolders.get(i);
if (folder.getState() != CmsResource.STATE_DELETED) {
// check if this is a system-folder and if it should be included.
String export = getCms().getSitePath(folder);
if (checkExportResource(export)) {
long age = folder.getDateLastModified() < folder.getDateCreated() ? folder.getDateCreated()
: folder.getDateLastModified();
// export this folder only if age is above selected age
// default for selected age (if not set by user) is <code>long 0</code> (i.e. 1970)
if (age >= m_contentAge) {
// only export folder data to manifest.xml if it has changed
appendResourceToManifest(folder, false);
}
// export all sub-resources in this folder
addChildResources(getCms().getSitePath(folder));
}
}
// release folder memory
subFolders.set(i, null);
}
} catch (CmsImportExportException e) {
throw e;
} catch (CmsException e) {
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_ADDING_CHILD_RESOURCES_1,
folderName);
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), e);
}
throw new CmsImportExportException(message, e);
}
}
/**
* Closes the export ZIP file and saves the XML document for the manifest.<p>
*
* @param exportNode the export root node
* @throws SAXException if something goes wrong procesing the manifest.xml
* @throws IOException if something goes wrong while closing the export file
*/
protected void closeExportFile(Element exportNode) throws IOException, SAXException {
// close the <export> Tag
getSaxWriter().writeClose(exportNode);
// close the XML document
CmsXmlSaxWriter xmlSaxWriter = (CmsXmlSaxWriter)getSaxWriter().getContentHandler();
xmlSaxWriter.endDocument();
// create zip entry for the manifest XML document
ZipEntry entry = new ZipEntry(CmsImportExportManager.EXPORT_MANIFEST);
getExportZipStream().putNextEntry(entry);
// complex substring operation is required to ensure handling for very large export manifest files
StringBuffer result = ((StringWriter)xmlSaxWriter.getWriter()).getBuffer();
int steps = result.length() / SUB_LENGTH;
int rest = result.length() % SUB_LENGTH;
int pos = 0;
for (int i = 0; i < steps; i++) {
String sub = result.substring(pos, pos + SUB_LENGTH);
getExportZipStream().write(sub.getBytes(OpenCms.getSystemInfo().getDefaultEncoding()));
pos += SUB_LENGTH;
}
if (rest > 0) {
String sub = result.substring(pos, pos + rest);
getExportZipStream().write(sub.getBytes(OpenCms.getSystemInfo().getDefaultEncoding()));
}
// close the zip entry for the manifest XML document
getExportZipStream().closeEntry();
// finally close the zip stream
getExportZipStream().close();
}
/**
* Writes the output element to the XML output writer and detaches it
* from it's parent element.<p>
*
* @param parent the parent element
* @param output the output element
* @throws SAXException if something goes wrong procesing the manifest.xml
*/
protected void digestElement(Element parent, Element output) throws SAXException {
m_saxWriter.write(output);
parent.remove(output);
}
/**
* Exports all resources and possible sub-folders form the provided list of resources.
*
* @param parent the parent node to add the resources to
* @param resourcesToExport the list of resources to export
* @throws CmsImportExportException if something goes wrong
* @throws SAXException if something goes wrong procesing the manifest.xml
* @throws IOException if not all resources could be appended to the ZIP archive
*/
protected void exportAllResources(Element parent, List resourcesToExport)
throws CmsImportExportException, IOException, SAXException {
// export all the resources
String resourceNodeName = getResourceNodeName();
m_resourceNode = parent.addElement(resourceNodeName);
getSaxWriter().writeOpen(m_resourceNode);
if (m_recursive) {
// remove the possible redundancies in the list of resources
resourcesToExport = CmsFileUtil.removeRedundancies(resourcesToExport);
}
// distinguish folder and file names
List folderNames = new ArrayList();
List fileNames = new ArrayList();
Iterator it = resourcesToExport.iterator();
while (it.hasNext()) {
String resource = (String)it.next();
if (CmsResource.isFolder(resource)) {
folderNames.add(resource);
} else {
fileNames.add(resource);
}
}
// init sets required for the body file exports
m_exportedResources = new HashSet();
m_exportedPageFiles = new HashSet();
// export the folders
for (int i = 0; i < folderNames.size(); i++) {
String path = (String)folderNames.get(i);
if (m_recursive) {
// first add superfolders to the xml-config file
addParentFolders(path);
addChildResources(path);
} else {
CmsFolder folder;
try {
folder = getCms().readFolder(path, CmsResourceFilter.IGNORE_EXPIRATION);
} catch (CmsException e) {
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_ADDING_PARENT_FOLDERS_1,
path);
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), e);
}
throw new CmsImportExportException(message, e);
}
CmsResourceState state = folder.getState();
long age = folder.getDateLastModified() < folder.getDateCreated() ? folder.getDateCreated()
: folder.getDateLastModified();
if (getCms().getRequestContext().currentProject().isOnlineProject()
|| (m_includeUnchanged)
|| state.isNew()
|| state.isChanged()) {
if (!state.isDeleted() && (age >= m_contentAge)) {
// check if this is a system-folder and if it should be included.
String export = getCms().getSitePath(folder);
if (checkExportResource(export)) {
appendResourceToManifest(folder, false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -