📄 cmsexport.java
字号:
/**
* adds all files with names in fileNames to the xml-config file
* Creation date: (10.08.00 17:12:05)
* @param fileNames java.util.Vector of Strings, e.g. /folder/index.html
*/
public void addSingleFiles(Vector fileNames) throws CmsException {
if (fileNames != null) {
for (int i = 0; i < fileNames.size(); i++) {
String fileName = (String) fileNames.elementAt(i);
try {
CmsFile file = m_cms.readFile(fileName);
if((file.getState() != C_STATE_DELETED) && (!file.getName().startsWith("~"))) {
addSuperFolders(fileName);
exportFile(file);
}
} catch(CmsException exc) {
if(exc.getType() != exc.C_RESOURCE_DELETED) {
throw exc;
}
}
}
}
}
/**
* Adds the superfolders of path to the config file, starting at the top, excluding the root folder
* Creation date: (10.08.00 11:07:58)
* @param path java.lang.String the path of the folder in the filesystem
*/
public void addSuperFolders(String path) throws CmsException {
// Initialize the "previously added folder cache"
if(m_superFolders == null) {
m_superFolders = new Vector();
}
Vector superFolders = new Vector();
// Check, if the path is really a folder
if(path.lastIndexOf(C_ROOT) != (path.length()-1)) {
path = path.substring(0, path.lastIndexOf(C_ROOT)+1);
}
while (path.length() > C_ROOT.length()) {
superFolders.addElement(path);
path = path.substring(0, path.length() - 1);
path = path.substring(0, path.lastIndexOf(C_ROOT)+1);
}
for (int i = superFolders.size()-1; i >= 0; i--) {
String addFolder = (String)superFolders.elementAt(i);
if(!m_superFolders.contains(addFolder)) {
// This super folder was NOT added previously. Add it now!
CmsFolder folder = m_cms.readFolder(addFolder);
writeXmlEntrys(folder);
// Remember that this folder was added
m_superFolders.addElement(addFolder);
}
}
}
/** Check whether some of the resources are redundant because a superfolder has also
* been selected or a file is included in a folder and change the parameter Vectors
*
* @param folderNames contains the full pathnames of all folders
* @param fileNames contains the full pathnames of all files
*/
private void checkRedundancies(Vector folderNames, Vector fileNames) {
int i, j;
if (folderNames == null) {
return;
}
Vector redundant = new Vector();
int n = folderNames.size();
if (n > 1) {
// otherwise no check needed, because there is only one resource
for (i = 0; i < n; i++) {
redundant.addElement(new Boolean(false));
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (((String) folderNames.elementAt(i)).length() < ((String) folderNames.elementAt(j)).length()) {
if (((String) folderNames.elementAt(j)).startsWith((String) folderNames.elementAt(i))) {
redundant.setElementAt(new Boolean(true), j);
}
} else {
if (((String) folderNames.elementAt(i)).startsWith((String) folderNames.elementAt(j))) {
redundant.setElementAt(new Boolean(true), i);
}
}
}
}
for (i = n - 1; i >= 0; i--) {
if (((Boolean) redundant.elementAt(i)).booleanValue()) {
folderNames.removeElementAt(i);
}
}
}
// now remove the files who are included automatically in a folder
// otherwise there would be a zip exception
for (i = fileNames.size() - 1; i >= 0; i--) {
for (j = 0; j < folderNames.size(); j++) {
if (((String) fileNames.elementAt(i)).startsWith((String) folderNames.elementAt(j))) {
fileNames.removeElementAt(i);
}
}
}
}
/**
* Exports one single file with all its data and content.
*
* @param file the file to be exported,
* @exception throws a CmsException if something goes wrong.
*/
private void exportFile(CmsFile file)
throws CmsException {
String source = getSourceFilename(file.getAbsolutePath());
System.out.print("Exporting " + source + " ...");
try {
// create the manifest-entrys
writeXmlEntrys((CmsResource) file);
// store content in zip-file
ZipEntry entry = new ZipEntry(source);
m_exportZipStream.putNextEntry(entry);
m_exportZipStream.write(file.getContents());
m_exportZipStream.closeEntry();
} catch(Exception exc) {
System.out.println("Error");
throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
}
System.out.println("OK");
}
/**
* Exports all needed sub-resources to the zip-file.
*
* @param path to complete path to the resource to export
* @exception throws CmsException if something goes wrong.
*/
private void exportResources(String path)
throws CmsException {
// get all subFolders
Vector subFolders = m_cms.getSubFolders(path);
// get all files in folder
Vector subFiles = m_cms.getFilesInFolder(path);
// walk through all files and export them
for(int i = 0; i < subFiles.size(); i++) {
CmsResource file = (CmsResource) subFiles.elementAt(i);
int state = file.getState();
if(m_isOnlineProject || (!m_excludeUnchanged) || state == C_STATE_NEW || state == C_STATE_CHANGED) {
if((state != C_STATE_DELETED) && (!file.getName().startsWith("~"))) {
exportFile(m_cms.readFile(file.getAbsolutePath()));
}
}
}
// walk through all subfolders and export them
for(int i = 0; i < subFolders.size(); i++) {
CmsResource folder = (CmsResource) subFolders.elementAt(i);
if(folder.getState() != C_STATE_DELETED){
// check if this is a system-folder and if it should be included.
if(folder.getAbsolutePath().startsWith("/system/") ||
folder.getAbsolutePath().startsWith("/pics/system/") ||
folder.getAbsolutePath().startsWith("/moduledemos/")) {
if(!m_excludeSystem) {
// export this folder
writeXmlEntrys(folder);
// export all resources in this folder
exportResources(folder.getAbsolutePath());
}
} else {
// export this folder
writeXmlEntrys(folder);
// export all resources in this folder
exportResources(folder.getAbsolutePath());
}
}
}
}
/**
* Gets the import resource and stores it in object-member.
*/
private void getExportResource()
throws CmsException {
try {
// add zip-extension, if needed
if( !m_exportFile.toLowerCase().endsWith(".zip") ) {
m_exportFile += ".zip";
}
// create the export-zipstream
m_exportZipStream = new ZipOutputStream(new FileOutputStream(m_exportFile));
} catch(Exception exc) {
throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
}
}
/**
* Substrings the source-filename, so it is shrinked to the needed part for
* import/export.
* @param absoluteName The absolute path of the resource.
* @return The shrinked path.
*/
private String getSourceFilename(String absoluteName) {
// String path = absoluteName.substring(m_exportPath.length());
String path = absoluteName; // keep absolute name to distinguish resources
if (path.startsWith("/")) {
path = path.substring(1);
}
if(path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
return path;
}
/**
* Creates the xml-file and appends the initial tags to it.
* @param Node moduleNode a node with module informations.
*/
private void getXmlConfigFile(Node moduleNode)
throws CmsException {
try {
// creates the document
m_docXml = A_CmsXmlContent.getXmlParser().createEmptyDocument(C_EXPORT_TAG_EXPORT);
// abbends the initital tags
Node exportNode = m_docXml.getFirstChild();
// add the info element. it contains all infos for this export
Element info = m_docXml.createElement(C_EXPORT_TAG_INFO);
m_docXml.getDocumentElement().appendChild(info);
addElement(info, C_EXPORT_TAG_CREATOR, m_cms.getRequestContext().currentUser().getName());
addElement(info, C_EXPORT_TAG_OC_VERSION, m_cms.version());
addElement(info, C_EXPORT_TAG_DATE, Utils.getNiceDate(new Date().getTime()));
addElement(info, C_EXPORT_TAG_PROJECT, m_cms.getRequestContext().currentProject().getName());
addElement(info, C_EXPORT_TAG_VERSION, C_EXPORT_VERSION);
if(moduleNode != null) {
// this is a module export - import module informations here
exportNode.appendChild(A_CmsXmlContent.getXmlParser().importNode(m_docXml, moduleNode));
// now remove the unused file informations
NodeList files = ((Element)exportNode).getElementsByTagName("files");
if(files.getLength() == 1) {
files.item(0).getParentNode().removeChild(files.item(0));
}
}
m_filesElement = m_docXml.createElement(C_EXPORT_TAG_FILES);
m_docXml.getDocumentElement().appendChild(m_filesElement);
// add userdata
if (m_exportUserdata){
m_userdataElement = m_docXml.createElement(C_EXPORT_TAG_USERGROUPDATA);
m_docXml.getDocumentElement().appendChild(m_userdataElement);
}
} catch(Exception exc) {
throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -