📄 cmsmoduleimportexporthandler.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/module/CmsModuleImportExportHandler.java,v $
* Date : $Date: 2006/03/27 14:53:03 $
* Version: $Revision: 1.33 $
*
* 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.module;
import org.opencms.configuration.CmsConfigurationException;
import org.opencms.configuration.CmsModuleConfiguration;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProject;
import org.opencms.i18n.CmsMessageContainer;
import org.opencms.importexport.CmsExport;
import org.opencms.importexport.CmsImport;
import org.opencms.importexport.CmsImportExportException;
import org.opencms.importexport.CmsImportExportManager;
import org.opencms.importexport.I_CmsImportExportHandler;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.report.CmsHtmlReport;
import org.opencms.report.I_CmsReport;
import org.opencms.security.CmsRole;
import org.opencms.security.CmsRoleViolationException;
import org.opencms.security.CmsSecurityException;
import org.opencms.xml.CmsXmlErrorHandler;
import org.opencms.xml.CmsXmlException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.digester.Digester;
import org.apache.commons.logging.Log;
import org.dom4j.Document;
import org.dom4j.Element;
import org.xml.sax.SAXException;
/**
* Import/export handler implementation for Cms modules.<p>
*
* @author Thomas Weckert
*
* @version $Revision: 1.33 $
*
* @since 6.0.0
*/
public class CmsModuleImportExportHandler implements I_CmsImportExportHandler {
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsModuleImportExportHandler.class);
/** The VFS resources to be exported additionally with the module.<p> */
private List m_additionalResources;
/** The description of this import/export handler.<p> */
private String m_description;
/** The name of the export file in the real file system.<p> */
private String m_fileName;
/** The module imported with the digester. */
private CmsModule m_importedModule;
/** The (package) name of the module to be exported.<p> */
private String m_moduleName;
/**
* Creates a new Cms module import/export handler.<p>
*/
public CmsModuleImportExportHandler() {
super();
m_description = org.opencms.importexport.Messages.get().getBundle().key(
org.opencms.importexport.Messages.GUI_CMSIMPORTHANDLER_DEFAULT_DESC_0);
}
/**
* Reads a module object from an external file source.<p>
*
* @param importResource the name of the input source
* @return the imported module
* @throws CmsConfigurationException if the module could not be imported
*/
public static CmsModule readModuleFromImport(String importResource) throws CmsConfigurationException {
// instantiate Digester and enable XML validation
Digester digester = new Digester();
digester.setUseContextClassLoader(true);
digester.setValidating(false);
digester.setRuleNamespaceURI(null);
digester.setErrorHandler(new CmsXmlErrorHandler());
// add this class to the Digester
CmsModuleImportExportHandler handler = new CmsModuleImportExportHandler();
digester.push(handler);
CmsModuleXmlHandler.addXmlDigesterRules(digester);
InputStream stream = null;
ZipFile importZip = null;
try {
File file = new File(importResource);
if (file.isFile()) {
importZip = new ZipFile(importResource);
ZipEntry entry = importZip.getEntry(CmsImportExportManager.EXPORT_MANIFEST);
if (entry != null) {
stream = importZip.getInputStream(entry);
} else {
CmsMessageContainer message = Messages.get().container(
Messages.ERR_NO_MANIFEST_MODULE_IMPORT_1,
importResource);
LOG.error(message.key());
throw new CmsConfigurationException(message);
}
} else if (file.isDirectory()) {
file = new File(file, CmsImportExportManager.EXPORT_MANIFEST);
stream = new FileInputStream(file);
}
// start the parsing process
digester.parse(stream);
} catch (IOException e) {
CmsMessageContainer message = Messages.get().container(Messages.ERR_IO_MODULE_IMPORT_1, importResource);
LOG.error(message.key(), e);
throw new CmsConfigurationException(message, e);
} catch (SAXException e) {
CmsMessageContainer message = Messages.get().container(Messages.ERR_SAX_MODULE_IMPORT_1, importResource);
LOG.error(message.key(), e);
throw new CmsConfigurationException(message, e);
} finally {
try {
if (importZip != null) {
importZip.close();
}
if (stream != null) {
stream.close();
}
} catch (Exception e) {
// noop
}
}
CmsModule importedModule = handler.getModule();
// the digester must have set the module now
if (importedModule == null) {
throw new CmsConfigurationException(Messages.get().container(
Messages.ERR_IMPORT_MOD_ALREADY_INSTALLED_1,
importResource));
}
return importedModule;
}
/**
* @see org.opencms.importexport.I_CmsImportExportHandler#exportData(org.opencms.file.CmsObject, org.opencms.report.I_CmsReport)
*/
public void exportData(CmsObject cms, I_CmsReport report)
throws CmsConfigurationException, CmsImportExportException, CmsRoleViolationException {
// check if the user has the required permissions
cms.checkRole(CmsRole.MODULE_MANAGER);
report.print(Messages.get().container(Messages.RPT_EXPORT_MODULE_BEGIN_0), I_CmsReport.FORMAT_HEADLINE);
if (report instanceof CmsHtmlReport) {
report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
"<i>" + getModuleName() + "</i>"));
} else {
report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
getModuleName()));
}
report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
if (!OpenCms.getModuleManager().hasModule(getModuleName())) {
// module not available
throw new CmsConfigurationException(Messages.get().container(
Messages.ERR_NO_MOD_FOR_EXPORT_1,
getModuleName()));
}
// generate module XML
CmsModule module = OpenCms.getModuleManager().getModule(getModuleName());
if (!module.getVersion().isUpdated()) {
// increment version number if not recently updated
module.getVersion().increment();
// update the XML configuration
OpenCms.writeConfiguration(CmsModuleConfiguration.class);
}
// reset update status so that all following exports auto-increment the number
module.getVersion().setUpdated(false);
Element moduleElement = CmsModuleXmlHandler.generateXml(module);
// export the module using the standard export
new CmsExport(cms, getFileName(), getAdditionalResources(), true, true, moduleElement, false, 0, report, true);
report.println(Messages.get().container(Messages.RPT_EXPORT_MODULE_END_0), I_CmsReport.FORMAT_HEADLINE);
}
/**
* Returns the VFS resources to be exported additionally with the module.<p>
*
* @return the VFS resources to be exported additionally with the module
*/
public List getAdditionalResources() {
return m_additionalResources;
}
/**
* @see org.opencms.importexport.I_CmsImportExportHandler#getDescription()
*/
public String getDescription() {
return m_description;
}
/**
* Returns the name of the export file in the real file system.<p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -