📄 cmsimportversion2.java
字号:
bodyparams = new HashMap();
}
bodyparams.put((paramElement.attribute("name")).getText(), paramElement.getTextTrim());
}
}
if ((mastertemplate == null) || (bodyname == null)) {
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_CANNOT_MERGE_PAGE_FILE_3,
resourcename,
mastertemplate,
bodyname);
if (LOG.isDebugEnabled()) {
LOG.debug(message.key());
}
throw new CmsImportExportException(message);
}
// lock the resource, so that it can be manipulated
m_cms.lockResource(resourcename);
// get all properties
List properties = m_cms.readPropertyObjects(resourcename, false);
// now get the content of the bodyfile and insert it into the control file
CmsFile bodyfile = m_cms.readFile(bodyname, CmsResourceFilter.IGNORE_EXPIRATION);
//get the encoding
String encoding = CmsProperty.get(CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, properties).getValue();
if (encoding == null) {
encoding = OpenCms.getSystemInfo().getDefaultEncoding();
}
if (m_convertToXmlPage) {
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_START_CONVERTING_TO_XML_0));
}
CmsXmlPage xmlPage = CmsXmlPageConverter.convertToXmlPage(m_cms, bodyfile.getContents(), getLocale(
resourcename,
properties), encoding);
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_END_CONVERTING_TO_XML_0));
}
if (xmlPage != null) {
pagefile.setContents(xmlPage.marshal());
// set the type to xml page
pagefile.setType(CmsResourceTypeXmlPage.getStaticTypeId());
}
}
// add the template and other required properties
CmsProperty newProperty = new CmsProperty(CmsPropertyDefinition.PROPERTY_TEMPLATE, mastertemplate, null);
// property lists must not contain equal properties
properties.remove(newProperty);
properties.add(newProperty);
// if set, add the bodyclass as property
if (CmsStringUtil.isNotEmpty(bodyclass)) {
newProperty = new CmsProperty(CmsPropertyDefinition.PROPERTY_TEMPLATE, mastertemplate, null);
newProperty.setAutoCreatePropertyDefinition(true);
properties.remove(newProperty);
properties.add(newProperty);
}
// if set, add bodyparams as properties
if (bodyparams != null) {
for (Iterator p = bodyparams.entrySet().iterator(); p.hasNext();) {
Map.Entry entry = (Map.Entry)p.next();
String key = (String)entry.getKey();
String value = (String)entry.getValue();
newProperty = new CmsProperty(key, value, null);
newProperty.setAutoCreatePropertyDefinition(true);
properties.remove(newProperty);
properties.add(newProperty);
}
}
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_START_IMPORTING_XML_PAGE_0));
}
// now import the resource
m_cms.importResource(resourcename, pagefile, pagefile.getContents(), properties);
// finally delete the old body file, it is not needed anymore
m_cms.lockResource(bodyname);
m_cms.deleteResource(bodyname, CmsResource.DELETE_PRESERVE_SIBLINGS);
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_END_IMPORTING_XML_PAGE_0));
}
m_report.println(
org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
I_CmsReport.FORMAT_OK);
} else {
// there are more than one template nodes in this control file
// convert the resource into a plain text file
// lock the resource, so that it can be manipulated
m_cms.lockResource(resourcename);
// set the type to plain
pagefile.setType(CmsResourceTypePlain.getStaticTypeId());
// write all changes
m_cms.writeFile(pagefile);
// done, unlock the resource
m_cms.unlockResource(resourcename);
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(
Messages.LOG_IMPORTEXPORT_CANNOT_CONVERT_XML_STRUCTURE_1,
resourcename));
}
m_report.println(Messages.get().container(Messages.RPT_NOT_CONVERTED_0), I_CmsReport.FORMAT_OK);
}
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_END_MERGING_1, resourcename));
}
} catch (CmsXmlException e) {
throw e;
} catch (CmsException e) {
m_report.println(e);
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_MERGING_PAGE_FILE_1,
resourcename);
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), e);
}
throw new CmsImportExportException(message, e);
}
}
/**
* Merges the page control files and their corresponding bodies into a single files.<p>
*
* @throws CmsImportExportException if something goes wrong
* @throws CmsXmlException if the page file could not be unmarshalled
*/
private void mergePageFiles() throws CmsXmlException, CmsImportExportException {
try {
// check if the template property exists. If not, create it.
try {
m_cms.readPropertyDefinition(CmsPropertyDefinition.PROPERTY_TEMPLATE);
} catch (CmsException e) {
// the template propertydefintion does not exist. So create it.
m_cms.createPropertyDefinition(CmsPropertyDefinition.PROPERTY_TEMPLATE);
}
// copy all propertydefinitions of the old page to the new page
List definitions = m_cms.readAllPropertyDefinitions();
Iterator j = definitions.iterator();
while (j.hasNext()) {
CmsPropertyDefinition definition = (CmsPropertyDefinition)j.next();
// check if this propertydef already exits
try {
m_cms.readPropertyDefinition(definition.getName());
} catch (Exception e) {
m_cms.createPropertyDefinition(definition.getName());
}
}
} catch (CmsException e) {
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_COPYING_PROPERTY_DEFINITIONS_0);
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), e);
}
throw new CmsImportExportException(message);
}
// iterate through the list of all page controlfiles found during the import process
int size = m_pageStorage.size();
m_report.println(Messages.get().container(Messages.RPT_MERGE_START_0), I_CmsReport.FORMAT_HEADLINE);
Iterator i = m_pageStorage.iterator();
int counter = 1;
while (i.hasNext()) {
String resname = (String)i.next();
// adjust the resourcename if nescessary
if (!resname.startsWith("/")) {
resname = "/" + resname;
}
m_report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_SUCCESSION_2,
String.valueOf(counter),
String.valueOf(size)), I_CmsReport.FORMAT_NOTE);
m_report.print(Messages.get().container(Messages.RPT_MERGE_0), I_CmsReport.FORMAT_NOTE);
m_report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
resname));
mergePageFile(resname);
if (LOG.isInfoEnabled()) {
LOG.info(Messages.get().getBundle().key(
Messages.LOG_MERGING_3,
String.valueOf(counter),
String.valueOf(size),
resname));
}
counter++;
}
// free mem
m_pageStorage.clear();
}
/**
* Deletes the folder structure which has been creating while importing the body files..<p>
*
* @throws CmsImportExportException if something goes wrong
*/
private void removeFolders() throws CmsImportExportException {
try {
int size = m_folderStorage.size();
m_report.println(Messages.get().container(Messages.RPT_DELFOLDER_START_0), I_CmsReport.FORMAT_HEADLINE);
// iterate though all collected folders. Iteration must start at the end of the list,
// as folders habe to be deleted in the reverse order.
int counter = 1;
for (int j = (size - 1); j >= 0; j--) {
String resname = (String)m_folderStorage.get(j);
resname = (resname.startsWith("/") ? "" : "/") + resname + (resname.endsWith("/") ? "" : "/");
// now check if the folder is really empty. Only delete empty folders
List files = m_cms.getFilesInFolder(resname, CmsResourceFilter.IGNORE_EXPIRATION);
if (files.size() == 0) {
List folders = m_cms.getSubFolders(resname, CmsResourceFilter.IGNORE_EXPIRATION);
if (folders.size() == 0) {
m_report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_SUCCESSION_2,
String.valueOf(counter),
String.valueOf(size)), I_CmsReport.FORMAT_NOTE);
m_report.print(Messages.get().container(Messages.RPT_DELFOLDER_0), I_CmsReport.FORMAT_NOTE);
m_report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
resname), I_CmsReport.FORMAT_DEFAULT);
m_cms.lockResource(resname);
m_cms.deleteResource(resname, CmsResource.DELETE_PRESERVE_SIBLINGS);
m_report.println(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_OK_0), I_CmsReport.FORMAT_OK);
counter++;
}
}
}
} catch (CmsException e) {
CmsMessageContainer message = Messages.get().container(
Messages.ERR_IMPORTEXPORT_ERROR_REMOVING_FOLDERS_OF_IMPORTED_BODY_FILES_0);
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), e);
}
throw new CmsImportExportException(message, e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -