📄 cmsimport.java
字号:
boolean exists = true;
try {
CmsResource res = m_cms.readFileHeader(m_importPath + destination);
if (res.getState() == C_STATE_DELETED) {
exists = false;
}
} catch (CmsException e) {
exists = false;
}
if (exists) {
conflictNames.addElement(m_importPath + destination);
}
}
}
} catch (Exception exc) {
throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
}
if (m_importZip != null) {
try {
m_importZip.close();
} catch (IOException exc) {
throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
}
}
return conflictNames;
}
/**
* 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 charsRead = 0;
int size = new Long(entry.getSize()).intValue();
byte[] buffer = new byte[size];
while (charsRead < size) {
charsRead += stream.read(buffer, charsRead, size - charsRead);
}
stream.close();
return buffer;
} else {
// no - use directory
File file = new File(m_importResource, filename);
FileInputStream fileStream = new FileInputStream(file);
int charsRead = 0;
int size = new Long(file.length()).intValue();
byte[] buffer = new byte[size];
while (charsRead < size) {
charsRead += fileStream.read(buffer, charsRead, size - charsRead);
}
fileStream.close();
return buffer;
}
} 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>
*/
protected void getImportResource() throws CmsException {
try {
// get the import resource
m_importResource = new File(CmsBase.getAbsolutePath(m_importFile));
// if it is a file it must be a zip-file
if(m_importResource.isFile()) {
m_importZip = new ZipFile(m_importResource);
}
} catch(Exception exc) {
m_report.println(exc);
throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
}
}
/**
* Returns a Vector 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 Vector of resource names that are needed to create a project for this import
*/
public Vector getResourcesForProject() throws CmsException {
NodeList fileNodes;
Element currentElement;
String destination;
Vector resources = new Vector();
try {
if (m_importingChannelData) m_cms.setContextToCos();
// get all file-nodes
fileNodes = m_docXml.getElementsByTagName(C_EXPORT_TAG_FILE);
// walk through all files in manifest
for (int i = 0; i < fileNodes.getLength(); i++) {
currentElement = (Element)fileNodes.item(i);
destination = getTextNodeValue(currentElement, C_EXPORT_TAG_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);
// this resource exists in the current project -> add it
resources.addElement(resource);
} catch (CmsException exc) {
// this resource is missing - we need the root-folder
resources.addElement(C_ROOT);
}
}
} catch (StringIndexOutOfBoundsException exc) {
// this is a resource in root-folder: ignore the excpetion
}
}
} catch (Exception exc) {
m_report.println(exc);
throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
} finally {
if (m_importingChannelData) m_cms.setContextToVfs();
}
if (m_importZip != null) {
try {
m_importZip.close();
} catch (IOException exc) {
m_report.println(exc);
throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
}
}
if (resources.contains(C_ROOT)) {
// we have to import root - forget the rest!
resources.removeAllElements();
resources.addElement(C_ROOT);
}
return resources;
}
/**
* Returns the text for a node.
*
* @param elem the parent element
* @param tag the tagname to get the value from
* @return the value of the tag
*/
protected String getTextNodeValue(Element elem, String tag) {
try {
return elem.getElementsByTagName(tag).item(0).getFirstChild().getNodeValue();
} catch(Exception exc) {
// ignore the exception and return null
return null;
}
}
/**
* Gets the xml-config file from the import resource and stores it in object-member.
* Checks whether the import is from a module file
*/
private void getXmlConfigFile() throws CmsException {
try {
InputStream in = new ByteArrayInputStream(getFileBytes(C_EXPORT_XMLFILENAME));
try {
m_docXml = A_CmsXmlContent.getXmlParser().parse(in);
} finally {
try {
in.close();
} catch (Exception e) {}
}
} catch (Exception exc) {
m_report.println(exc);
throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
}
}
/**
* Imports a resource (file or folder) into the cms.<p>
*
* @param source the path to the source-file
* @param destination the path to the destination-file in the cms
* @param type the resource-type of the file
* @param user the owner of the file
* @param group the group of the file
* @param access the access-flags of the file
* @param properties a hashtable with properties for this resource
* @param writtenFilenames filenames of the files and folder which have actually been successfully written
* not used when null
* @param fileCodes code of the written files (for the registry)
* not used when null
*/
private void importResource(
String source,
String destination,
String type,
String user,
String group,
String access,
long lastmodified,
Map properties,
String launcherStartClass,
Vector writtenFilenames,
Vector fileCodes
) {
boolean success = true;
byte[] content = null;
String fullname = null;
try {
if (m_importingChannelData) {
m_cms.setContextToCos();
// try to read an existing channel to get the channel id
String channelId = null;
try {
if ((type.equalsIgnoreCase(C_TYPE_FOLDER_NAME)) && (! destination.endsWith(C_FOLDER_SEPARATOR))) {
destination += C_FOLDER_SEPARATOR;
}
CmsResource res = m_cms.readFileHeader(C_ROOT + destination);
channelId = m_cms.readProperty(res.getAbsolutePath(), I_CmsConstants.C_PROPERTY_CHANNELID);
} catch (Exception e) {
// ignore the exception, a new channel id will be generated
}
if (channelId == null) {
// the channel id does not exist, so generate a new one
int newChannelId = com.opencms.dbpool.CmsIdGenerator.nextId(C_TABLE_CHANNELID);
channelId = "" + newChannelId;
}
properties.put(I_CmsConstants.C_PROPERTY_CHANNELID, channelId);
}
// get the file content
if (source != null) {
content = getFileBytes(source);
}
// check and convert old import files
if (m_importVersion < 2) {
// convert content from pre 5.x must be activated
if ("page".equals(type) || ("plain".equals(type)) || ("XMLTemplate".equals(type))) {
if (DEBUG > 0){
System.err.println("#########################");
System.err.println("["+this.getClass().getName()+".importResource()]: starting conversion of \""+type+"\" resource "+source+".");
}
// change the filecontent for encoding if necessary
content = convertFile(source, content);
}
// only check the file type if the version of the export is 0
if(m_importVersion == 0){
// ok, a (very) old system exported this, check if the file is ok
if(!(new CmsCompatibleCheck()).isTemplateCompatible(m_importPath + destination, content, type)){
type = C_TYPE_COMPATIBLEPLAIN_NAME;
m_report.print(m_report.key("report.must_set_to") + C_TYPE_COMPATIBLEPLAIN_NAME + " ", I_CmsReport.C_FORMAT_WARNING);
}
}
}
// version 2.0 import (since OpenCms 5.0), no content conversion required
CmsResource res = m_cms.importResource(source, destination, type, user, group, access, lastmodified,
properties, launcherStartClass, content, m_importPath);
if(res != null){
fullname = res.getAbsolutePath();
if(C_TYPE_PAGE_NAME.equals(type)){
m_importedPages.add(fullname);
}
}
m_report.println(m_report.key("report.ok"), I_CmsReport.C_FORMAT_OK);
} catch (Exception exc) {
// an error while importing the file
success = false;
m_report.println(exc);
try {
// Sleep some time after an error so that the report output has a chance to keep up
Thread.sleep(1000);
} catch (Exception e) {};
} finally {
if (m_importingChannelData) m_cms.setContextToVfs();
}
byte[] digestContent = {0};
if (content != null) {
digestContent = m_digest.digest(content);
}
if (success && (fullname != null)){
if (writtenFilenames != null){
writtenFilenames.addElement(fullname);
}
if (fileCodes != null){
fileCodes.addElement(new String(digestContent));
}
}
}
/**
* Imports the resources and writes them to the cms.<p>
*
* @param excludeList filenames of files and folders which should not
* be (over)written in the virtual file system (not used when null)
* @param writtenFilenames filenames of the files and folder which have actually been
* successfully written (not used when null)
* @param fileCodes code of the written files (for the registry)
* (not used when null)
* @param propertyName name of a property to be added to all resources
* @param propertyValue value of that property
* @throws CmsException if something goes wrong
*/
protected void importAllResources(
Vector excludeList,
Vector writtenFilenames,
Vector fileCodes,
String propertyName,
String propertyValue
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -