📄 cmsexportmoduledata.java
字号:
}
// write the XML
digestElement(parent, master);
getReport().println(
org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
I_CmsReport.FORMAT_OK);
}
/**
* Exports a content definition content in a "dataset_xxx.xml" file and a number of
* "datayyy_xxx.dat" files.<p>
*
* @param dataset data for the content definition object instance
* @param filename name of the zip file for the module data export
* @param masterNr id of master
* @param subId id of content definition
*
* @throws CmsException if something goes wrong
*/
private void exportCosModuleData(CmsMasterDataSet dataset, String filename, int masterNr, int subId)
throws CmsException {
// create a new xml document
Document doc = DocumentHelper.createDocument();
Element data = doc.addElement(com.opencms.core.I_CmsConstants.C_EXPORT_TAG_MODULEXPORT).addElement(
C_EXPORT_TAG_MASTER_DATASET);
// add the data of the contentdefinition
// get the name of the owner
String ownerName = "";
try {
ownerName = getCms().readUser(dataset.m_userId).getName();
} catch (CmsException e) {
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Unable to read user with id " + dataset.m_userId, e);
}
}
// get the name of the group
String groupName = "";
try {
groupName = getCms().readGroup(dataset.m_groupId).getName();
} catch (CmsException e) {
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Unable to read group with id " + dataset.m_groupId, e);
}
}
data.addElement(C_EXPORT_TAG_MASTER_ID).addText(dataset.m_masterId.toString());
data.addElement(C_EXPORT_TAG_MASTER_USER).addText(ownerName);
data.addElement(C_EXPORT_TAG_MASTER_GROUP).addText(groupName);
data.addElement(C_EXPORT_TAG_MASTER_ACCESSFLAGS).addText(Integer.toString(dataset.m_accessFlags));
data.addElement(C_EXPORT_TAG_MASTER_PUBLICATIONDATE).addText(
CmsDateUtil.getDateTimeShort(dataset.m_publicationDate));
data.addElement(C_EXPORT_TAG_MASTER_PURGEDATE).addText(CmsDateUtil.getDateTimeShort(dataset.m_purgeDate));
data.addElement(C_EXPORT_TAG_MASTER_FLAGS).addText(Integer.toString(dataset.m_flags));
data.addElement(C_EXPORT_TAG_MASTER_FEEDID).addText(Integer.toString(dataset.m_feedId));
data.addElement(C_EXPORT_TAG_MASTER_FEEDREFERENCE).addText(Integer.toString(dataset.m_feedReference));
data.addElement(C_EXPORT_TAG_MASTER_FEEDFILENAME).addText(
dataset.m_feedFilename != null ? dataset.m_feedFilename : "");
data.addElement(C_EXPORT_TAG_MASTER_TITLE).addCDATA(dataset.m_title != null ? dataset.m_title : "");
// get the values of data_big from the string array
for (int i = 0; i < dataset.m_dataBig.length; i++) {
String value = dataset.m_dataBig[i];
String dataFile = new String();
if (value != null && !"".equals(value)) {
// the name of the file where the value of the field is stored
dataFile = "databig_" + subId + "_" + masterNr + "_" + i + ".dat";
writeExportFile(dataFile, value.getBytes());
}
data.addElement(C_EXPORT_TAG_MASTER_DATABIG + i).addText(dataFile);
}
// get the values of data_medium from the string array
for (int i = 0; i < dataset.m_dataMedium.length; i++) {
String value = dataset.m_dataMedium[i];
String dataFile = new String();
if (value != null && !"".equals(value)) {
// the name of the file where the value of the field is stored
dataFile = "datamedium_" + subId + "_" + masterNr + "_" + i + ".dat";
writeExportFile(dataFile, value.getBytes());
}
data.addElement(C_EXPORT_TAG_MASTER_DATAMEDIUM + i).addText(dataFile);
}
// get the values of data_small from the string array
for (int i = 0; i < dataset.m_dataSmall.length; i++) {
String value = dataset.m_dataSmall[i];
String dataFile = new String();
if (value != null && !"".equals(value)) {
// the name of the file where the value of the field is stored
dataFile = "datasmall_" + subId + "_" + masterNr + "_" + i + ".dat";
writeExportFile(dataFile, value.getBytes());
}
data.addElement(C_EXPORT_TAG_MASTER_DATASMALL + i).addText(dataFile);
}
// get the values of data_int from the int array
for (int i = 0; i < dataset.m_dataInt.length; i++) {
String value = "" + dataset.m_dataInt[i];
data.addElement(C_EXPORT_TAG_MASTER_DATAINT + i).addText(value);
}
// get the values of data_reference from the int array
for (int i = 0; i < dataset.m_dataReference.length; i++) {
String value = "" + dataset.m_dataReference[i];
data.addElement(C_EXPORT_TAG_MASTER_DATAREFERENCE + i).addText(value);
}
// get the values of data_reference from the int array
for (int i = 0; i < dataset.m_dataDate.length; i++) {
String value = CmsDateUtil.getDateTimeShort(dataset.m_dataDate[i]);
data.addElement(C_EXPORT_TAG_MASTER_DATADATE + i).addText(value);
}
try {
// set up new zip entry
ZipEntry entry = new ZipEntry(filename);
getExportZipStream().putNextEntry(entry);
// generate the SAX XML writer
CmsXmlSaxWriter saxHandler = new CmsXmlSaxWriter(
new OutputStreamWriter(getExportZipStream()),
OpenCms.getSystemInfo().getDefaultEncoding());
// write the document
(new SAXWriter(saxHandler, saxHandler)).write(doc);
// close zip entry
getExportZipStream().closeEntry();
} catch (SAXException e) {
getReport().println(e);
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Unable to write ZIP dataset file " + filename, e);
}
throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, e);
} catch (IOException e) {
getReport().println(e);
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Unable to write ZIP dataset file " + filename, e);
}
throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, e);
}
}
/**
* Exports a media object, creates a "media_xxx.xml" and a "mediacontent_xxx.dat" data file.<p>
*
* @param media data for the media object instance
* @param filename name of the xml file for the media data export
* @param masterNr id of master
* @param subId id of content definition
* @param mediaId if od media object
*
* @throws CmsException if something goes wrong
*/
private void exportCosModuleMedia(CmsMasterMedia media, String filename, int masterNr, int subId, int mediaId)
throws CmsException {
// create a new xml document
Document doc = DocumentHelper.createDocument();
Element data = doc.addElement(com.opencms.core.I_CmsConstants.C_EXPORT_TAG_MODULEXPORT);
// add the data element
Element em = data.addElement(C_EXPORT_TAG_MASTER_MEDIA);
// add the data of the contentdefinition
em.addElement(C_EXPORT_TAG_MEDIA_POSITION).addText(Integer.toString(media.getPosition()));
em.addElement(C_EXPORT_TAG_MEDIA_WIDTH).addText(Integer.toString(media.getWidth()));
em.addElement(C_EXPORT_TAG_MEDIA_HEIGHT).addText(Integer.toString(media.getHeight()));
em.addElement(C_EXPORT_TAG_MEDIA_SIZE).addText(Integer.toString(media.getSize()));
em.addElement(C_EXPORT_TAG_MEDIA_MIMETYPE).addText(media.getMimetype() != null ? media.getMimetype() : "");
em.addElement(C_EXPORT_TAG_MEDIA_TYPE).addText(Integer.toString(media.getType()));
em.addElement(C_EXPORT_TAG_MEDIA_TITLE).addCDATA(media.getTitle() != null ? media.getTitle() : "");
em.addElement(C_EXPORT_TAG_MEDIA_NAME).addCDATA(media.getName() != null ? media.getName() : "");
em.addElement(C_EXPORT_TAG_MEDIA_DESCRIPTION).addCDATA(
media.getDescription() != null ? media.getDescription() : "");
// now add the name of the file where the media content is stored and write this file
String contentFilename = "mediacontent_" + subId + "_" + masterNr + "_" + mediaId + ".dat";
em.addElement(C_EXPORT_TAG_MEDIA_CONTENT).addText(contentFilename);
writeExportFile(contentFilename, media.getMedia());
try {
// set up new zip entry
ZipEntry entry = new ZipEntry(filename);
getExportZipStream().putNextEntry(entry);
// generate the SAX XML writer
CmsXmlSaxWriter saxHandler = new CmsXmlSaxWriter(
new OutputStreamWriter(getExportZipStream()),
OpenCms.getSystemInfo().getDefaultEncoding());
// write the document
(new SAXWriter(saxHandler, saxHandler)).write(doc);
// close zip entry
getExportZipStream().closeEntry();
} catch (SAXException e) {
getReport().println(e);
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Unable to write ZIP media content file " + contentFilename, e);
}
throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, e);
} catch (IOException e) {
getReport().println(e);
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Unable to write ZIP media content file " + contentFilename, e);
}
throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, e);
}
}
/**
* Returns a master content definition object instance created with the reflection API.<p>
*
* @param classname name of the content definition class
* @param classes required for constructor generation
* @param objects instances to be used as parameters for class instance generation
*
* @return a master content definition object instance created with the reflection API
*/
private CmsMasterContent getContentDefinition(String classname, Class[] classes, Object[] objects) {
CmsMasterContent cd = null;
try {
Class cdClass = Class.forName(classname);
Constructor co = cdClass.getConstructor(classes);
cd = (CmsMasterContent)co.newInstance(objects);
} catch (InvocationTargetException e) {
getReport().println(e);
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Error generating instance for class " + classname, e);
}
} catch (NoSuchMethodException e) {
getReport().println(e);
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Error generating instance for class " + classname, e);
}
} catch (InstantiationException e) {
getReport().println(e);
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Error generating instance for class " + classname, e);
}
} catch (ClassNotFoundException e) {
getReport().println(e);
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Error generating instance for class " + classname, e);
}
} catch (IllegalArgumentException e) {
getReport().println(e);
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Error generating instance for class " + classname, e);
}
} catch (IllegalAccessException e) {
getReport().println(e);
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Error generating instance for class " + classname, e);
}
}
return cd;
}
/**
* Writes a binary content to a "*.dat" file in the export zip.<p>
*
* @param filename name of the file, usually ends with .dat
* @param content contents to write to the file
*/
private void writeExportFile(String filename, byte[] content) {
try {
// store the userinfo in zip-file
ZipEntry entry = new ZipEntry(filename);
getExportZipStream().putNextEntry(entry);
getExportZipStream().write(content);
getExportZipStream().closeEntry();
} catch (IOException ioex) {
getReport().println(ioex);
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Unable to write ZIP for filename " + filename, ioex);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -