📄 cmsexport.java
字号:
}
/**
* Exports one single file with all its data and content.<p>
*
* @param file the file to be exported
* @throws CmsImportExportException if something goes wrong
* @throws SAXException if something goes wrong procesing the manifest.xml
* @throws CmsLoaderException if an "old style" XML page could be exported
* @throws IOException if the ZIP entry for the file could be appended to the ZIP archive
*/
private void exportFile(CmsFile file)
throws CmsImportExportException, SAXException, CmsLoaderException, IOException {
String source = trimResourceName(getCms().getSitePath(file));
I_CmsReport report = getReport();
m_exportCount++;
report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_SUCCESSION_1,
String.valueOf(m_exportCount)), I_CmsReport.FORMAT_NOTE);
report.print(Messages.get().container(Messages.RPT_EXPORT_0), I_CmsReport.FORMAT_NOTE);
report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
getCms().getSitePath(file)));
report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
// store content in zip-file
// check if the content of this resource was not already exported
if (!m_exportedResources.contains(file.getResourceId())) {
ZipEntry entry = new ZipEntry(source);
// save the time of the last modification in the zip
entry.setTime(file.getDateLastModified());
getExportZipStream().putNextEntry(entry);
getExportZipStream().write(file.getContents());
getExportZipStream().closeEntry();
// add the resource id to the storage to mark that this resource was already exported
m_exportedResources.add(file.getResourceId());
// create the manifest-entrys
appendResourceToManifest(file, true);
} else {
// only create the manifest-entrys
appendResourceToManifest(file, false);
}
// check if the resource is a page of the old style. if so, export the body as well
if (OpenCms.getResourceManager().getResourceType(file.getTypeId()).getTypeName().equals("page")) {
m_exportedPageFiles.add("/" + source);
}
if (LOG.isInfoEnabled()) {
LOG.info(Messages.get().getBundle().key(Messages.LOG_EXPORTING_OK_2, String.valueOf(m_exportCount), source));
}
report.println(
org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
I_CmsReport.FORMAT_OK);
}
/**
* Exports one single group with all it's data.<p>
*
* @param parent the parent node to add the groups to
* @param group the group to be exported
* @throws CmsImportExportException if something goes wrong
* @throws SAXException if something goes wrong procesing the manifest.xml
*/
private void exportGroup(Element parent, CmsGroup group) throws CmsImportExportException, SAXException {
try {
String parentgroup;
if (group.getParentId().isNullUUID()) {
parentgroup = "";
} else {
parentgroup = getCms().getParent(group.getName()).getName();
}
Element e = parent.addElement(CmsImportExportManager.N_GROUPDATA);
e.addElement(CmsImportExportManager.N_NAME).addText(group.getName());
e.addElement(CmsImportExportManager.N_DESCRIPTION).addCDATA(group.getDescription());
e.addElement(CmsImportExportManager.N_FLAGS).addText(Integer.toString(group.getFlags()));
e.addElement(CmsImportExportManager.N_PARENTGROUP).addText(parentgroup);
// write the XML
digestElement(parent, e);
} catch (CmsException e) {
CmsMessageContainer message = org.opencms.db.Messages.get().container(
org.opencms.db.Messages.ERR_GET_PARENT_GROUP_1,
group.getName());
if (LOG.isDebugEnabled()) {
LOG.debug(message.key(), e);
}
throw new CmsImportExportException(message, e);
}
}
/**
* Exports all groups with all data.<p>
*
* @param parent the parent node to add the groups to
* @throws CmsImportExportException if something goes wrong
* @throws SAXException if something goes wrong procesing the manifest.xml
*/
private void exportGroups(Element parent) throws CmsImportExportException, SAXException {
try {
I_CmsReport report = getReport();
List allGroups = OpenCms.getOrgUnitManager().getGroups(getCms(), "/", true);
for (int i = 0, l = allGroups.size(); i < l; i++) {
CmsGroup group = (CmsGroup)allGroups.get(i);
report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_SUCCESSION_2,
String.valueOf(i + 1),
String.valueOf(l)), I_CmsReport.FORMAT_NOTE);
report.print(Messages.get().container(Messages.RPT_EXPORT_GROUP_0), I_CmsReport.FORMAT_NOTE);
report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
group.getName()));
report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
exportGroup(parent, group);
report.println(
org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
I_CmsReport.FORMAT_OK);
}
} catch (CmsImportExportException e) {
throw e;
} catch (CmsException e) {
if (LOG.isDebugEnabled()) {
LOG.debug(e.getLocalizedMessage(), e);
}
throw new CmsImportExportException(e.getMessageContainer(), e);
}
}
/**
* Exports one single user with all its data.<p>
*
* @param parent the parent node to add the users to
* @param user the user to be exported
* @throws CmsImportExportException if something goes wrong
* @throws SAXException if something goes wrong procesing the manifest.xml
*/
private void exportUser(Element parent, CmsUser user) throws CmsImportExportException, SAXException {
try {
// add user node to the manifest.xml
Element e = parent.addElement(CmsImportExportManager.N_USERDATA);
e.addElement(CmsImportExportManager.N_NAME).addText(user.getName());
// encode the password, using a base 64 decoder
String passwd = new String(Base64.encodeBase64(user.getPassword().getBytes()));
e.addElement(CmsImportExportManager.N_PASSWORD).addCDATA(passwd);
e.addElement(CmsImportExportManager.N_FIRSTNAME).addText(user.getFirstname());
e.addElement(CmsImportExportManager.N_LASTNAME).addText(user.getLastname());
e.addElement(CmsImportExportManager.N_EMAIL).addText(user.getEmail());
e.addElement(CmsImportExportManager.N_FLAGS).addText(Integer.toString(user.getFlags()));
e.addElement(CmsImportExportManager.N_DATECREATED).addText(Long.toString(user.getDateCreated()));
Element userInfoNode = e.addElement(CmsImportExportManager.N_USERINFO);
List keys = new ArrayList(user.getAdditionalInfo().keySet());
Collections.sort(keys);
Iterator itInfoKeys = keys.iterator();
while (itInfoKeys.hasNext()) {
String key = (String)itInfoKeys.next();
if (key == null) {
continue;
}
Object value = user.getAdditionalInfo(key);
if (value == null) {
continue;
}
Element entryNode = userInfoNode.addElement(CmsImportExportManager.N_USERINFO_ENTRY);
entryNode.addAttribute(CmsImportExportManager.A_NAME, key);
entryNode.addAttribute(CmsImportExportManager.A_TYPE, value.getClass().getName());
try {
// serialize the user info and write it into a file
entryNode.addCDATA(CmsDataTypeUtil.dataExport(value));
} catch (IOException ioe) {
getReport().println(ioe);
if (LOG.isErrorEnabled()) {
LOG.error(Messages.get().getBundle().key(
Messages.ERR_IMPORTEXPORT_ERROR_EXPORTING_USER_1,
user.getName()), ioe);
}
}
}
// append the node for groups of user
List userGroups = getCms().getGroupsOfUser(user.getName(), true);
Element g = e.addElement(CmsImportExportManager.N_USERGROUPS);
for (int i = 0; i < userGroups.size(); i++) {
String groupName = ((CmsGroup)userGroups.get(i)).getName();
g.addElement(CmsImportExportManager.N_GROUPNAME).addElement(CmsImportExportManager.N_NAME).addText(
groupName);
}
// write the XML
digestElement(parent, e);
} catch (CmsException e) {
if (LOG.isDebugEnabled()) {
LOG.debug(e.getLocalizedMessage(), e);
}
throw new CmsImportExportException(e.getMessageContainer(), e);
}
}
/**
* Exports all users with all data.<p>
*
* @param parent the parent node to add the users to
* @throws CmsImportExportException if something goes wrong
* @throws SAXException if something goes wrong procesing the manifest.xml
*/
private void exportUsers(Element parent) throws CmsImportExportException, SAXException {
try {
I_CmsReport report = getReport();
List allUsers = new ArrayList();
if (m_exportUserdata) {
// add system users
allUsers.addAll(OpenCms.getOrgUnitManager().getUsers(getCms(), "", true));
}
for (int i = 0, l = allUsers.size(); i < l; i++) {
CmsUser user = (CmsUser)allUsers.get(i);
report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_SUCCESSION_2,
String.valueOf(i + 1),
String.valueOf(l)), I_CmsReport.FORMAT_NOTE);
report.print(Messages.get().container(Messages.RPT_EXPORT_USER_0), I_CmsReport.FORMAT_NOTE);
report.print(org.opencms.report.Messages.get().container(
org.opencms.report.Messages.RPT_ARGUMENT_1,
user.getName()));
report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
exportUser(parent, user);
report.println(
org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
I_CmsReport.FORMAT_OK);
}
} catch (CmsImportExportException e) {
throw e;
} catch (CmsException e) {
if (LOG.isDebugEnabled()) {
LOG.debug(e.getLocalizedMessage(), e);
}
throw new CmsImportExportException(e.getMessageContainer(), e);
}
}
/**
* Checks if a resource is belongs to the correct project for exporting.<p>
* @param res the resource to check
* @return true if the resource can be exported, false otherwiese
*/
private boolean isInExportableProject(CmsResource res) {
boolean retValue = true;
// the "only modified in current project flag" is checked
if (m_inProject) {
// resource state is new or changed
if ((res.getState() == CmsResource.STATE_CHANGED) || (res.getState() == CmsResource.STATE_NEW)) {
// the resource belongs not to the curent project, so it must not be exported
if (!res.getProjectLastModified().equals(m_cms.getRequestContext().currentProject().getUuid())) {
retValue = false;
}
} else {
// state is unchanged, so do not export it
retValue = false;
}
}
return retValue;
}
/**
* Cuts leading and trailing '/' from the given resource name.<p>
*
* @param resourceName the absolute path of a resource
* @return the trimmed resource name
*/
private String trimResourceName(String resourceName) {
if (resourceName.startsWith("/")) {
resourceName = resourceName.substring(1);
}
if (resourceName.endsWith("/")) {
resourceName = resourceName.substring(0, resourceName.length() - 1);
}
return resourceName;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -