📄 cmsimport.java
字号:
} catch (IOException e) {
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_CLOSING_ZIP_ARCHIVE_1,
m_importZip.getName());
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), e);
}
throw new CmsImportExportException(message, e);
}
}
return conflictNames;
}
/**
* Returns a list of resource names that are needed to create a project for this import.<p>
*
* It calls the method getConflictingFileNames if needed, to calculate these resources.
*
* @return a list of resource names that are needed to create a project for this import
* @throws CmsImportExportException if ZIP archive could not be closed
*/
public List getResourcesForProject() throws CmsImportExportException {
List resources = new ArrayList();
// get all file-nodes
List fileNodes = m_docXml.selectNodes("//" + CmsImportExportManager.N_FILE);
// walk through all files in manifest
for (int i = 0; i < fileNodes.size(); i++) {
Element currentElement = (Element)fileNodes.get(i);
String destination = CmsImport.getChildElementTextValue(
currentElement,
CmsImportExportManager.N_DESTINATION);
// get the resources for a project
try {
String resource = destination.substring(0, destination.indexOf("/", 1) + 1);
resource = m_importPath + resource;
// add the resource, if it dosen't already exist
if ((!resources.contains(resource)) && (!resource.equals(m_importPath))) {
try {
m_cms.readFolder(resource, CmsResourceFilter.IGNORE_EXPIRATION);
// this resource exists in the current project -> add it
resources.add(resource);
} catch (CmsException exc) {
// this resource is missing - we need the root-folder
resources.add("/");
}
}
} catch (StringIndexOutOfBoundsException exc) {
// this is a resource in root-folder: ignore the excpetion
}
}
closeImportFile();
if (resources.contains("/")) {
// we have to import root - forget the rest!
resources.clear();
resources.add("/");
}
return resources;
}
/**
* Imports the resources and writes them to the cms VFS, even if there
* already exist files with the same name.<p>
*
* @throws CmsImportExportException if something goes wrong
* @throws CmsXmlException if the manifest of the import file could not be unmarshalled
*/
public void importResources() throws CmsImportExportException, CmsXmlException {
// initialize the import
boolean run = false;
openImportFile();
m_report.println(
Messages.get().container(Messages.RPT_IMPORT_VERSION_1, String.valueOf(m_importVersion)),
I_CmsReport.FORMAT_NOTE);
try {
// now find the correct import implementation
Iterator i = m_importImplementations.iterator();
while (i.hasNext()) {
I_CmsImport imp = (I_CmsImport)i.next();
if (imp.getVersion() == m_importVersion) {
// this is the correct import version, so call it for the import process
imp.importResources(m_cms, m_importPath, m_report, m_importResource, m_importZip, m_docXml);
run = true;
break;
}
}
if (!run) {
m_report.println(
Messages.get().container(Messages.RPT_IMPORT_DB_NO_CLASS_0),
I_CmsReport.FORMAT_WARNING);
}
} finally {
// close the import file
closeImportFile();
OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_CLEAR_OFFLINE_CACHES, Collections.EMPTY_MAP));
}
}
/**
* Closes the import file.<p>
*
* @throws CmsImportExportException if the ZIP archive could not be closed
*/
protected void closeImportFile() throws CmsImportExportException {
if (m_importZip != null) {
try {
m_importZip.close();
} catch (IOException e) {
m_report.println(e);
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_CLOSING_ZIP_ARCHIVE_1,
m_importZip.getName());
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), e);
}
throw new CmsImportExportException(message, e);
}
}
}
/**
* Returns a byte array containing the content of the file.<p>
*
* @param filename the name of the file to read
* @return a byte array containing the content of the file
*/
protected byte[] getFileBytes(String filename) {
try {
// is this a zip-file?
if (m_importZip != null) {
// yes
ZipEntry entry = m_importZip.getEntry(filename);
InputStream stream = m_importZip.getInputStream(entry);
int size = new Long(entry.getSize()).intValue();
return CmsFileUtil.readFully(stream, size);
} else {
// no - use directory
File file = new File(m_importResource, filename);
return CmsFileUtil.readFile(file);
}
} catch (FileNotFoundException fnfe) {
m_report.println(fnfe);
} catch (IOException ioe) {
m_report.println(ioe);
}
// this will only be returned in case there was an exception
return "".getBytes();
}
/**
* Gets the import resource and stores it in object-member.<p>
*
* @throws CmsImportExportException if the import file could not be opened
*/
protected void getImportResource() throws CmsImportExportException {
try {
// get the import resource
m_importResource = new File(OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(m_importFile));
// if it is a file it must be a zip-file
if (m_importResource.isFile()) {
m_importZip = new ZipFile(m_importResource);
}
} catch (IOException e) {
m_report.println(e);
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_OPENING_ZIP_ARCHIVE_1,
m_importFile);
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), e);
}
throw new CmsImportExportException(message, e);
}
}
/**
* Initalizes the import.<p>
*
* @throws CmsImportExportException if the import file could not be opened
* @throws CmsXmlException if the manifest of the import could not be unmarshalled
*/
protected void openImportFile() throws CmsXmlException, CmsImportExportException {
// open the import resource
getImportResource();
// read the xml-config file
m_docXml = CmsXmlUtils.unmarshalHelper(getFileBytes(CmsImportExportManager.EXPORT_MANIFEST), null);
// try to read the export version number
try {
m_importVersion = Integer.parseInt(((Element)m_docXml.selectNodes("//" + CmsImportExportManager.N_VERSION).get(
0)).getTextTrim());
} catch (Exception e) {
// ignore the exception, the export file has no version nummber (version 0)
// should never happen
if (LOG.isErrorEnabled()) {
LOG.error(e.getLocalizedMessage(), e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -