📄 cmsimport.java
字号:
// 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;
}
}
/**
* Returns a buffered reader for this resource using the importFile as root.
*
* @param filename The name of the file to read.
* @return BufferedReader The filereader for this file.
*/
private BufferedReader getFileReader(String filename)
throws Exception{
// is this a zip-file?
if(m_importZip != null) {
// yes
ZipEntry entry = m_importZip.getEntry(filename);
InputStream stream = m_importZip.getInputStream(entry);
return new BufferedReader( new InputStreamReader(stream));
} else {
// no - use directory
File xmlFile = new File(m_importResource, filename);
return new BufferedReader(new FileReader(xmlFile));
}
}
/**
* Gets the import resource and stores it in object-member.
*/
private 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) {
throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
}
}
/**
* This method returns the resource-names that are needed to create a project for this import.
* It calls the method getConflictingFileNames if needed, to calculate these resources.
*/
public Vector getResourcesForProject() throws CmsException {
NodeList fileNodes;
Element currentElement, currentProperty;
String source, destination, path;
Vector resources = new Vector();
try {
// 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);
source = getTextNodeValue(currentElement, C_EXPORT_TAG_SOURCE);
destination = getTextNodeValue(currentElement, C_EXPORT_TAG_DESTINATION);
path = m_importPath + 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) {
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);
}
}
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 this node.
*
* @param elem the parent-element.
* @param tag the tagname to get the value from.
* @return the value of the tag.
*/
private 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 {
BufferedReader xmlReader = getFileReader(C_EXPORT_XMLFILENAME);
m_docXml = A_CmsXmlContent.getXmlParser().parse(xmlReader);
xmlReader.close();
} catch(Exception exc) {
throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
}
}
/**
* Imports a resource (file or folder) into the cms.
* @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, Hashtable properties, String launcherStartClass, Vector writtenFilenames, Vector fileCodes) {
// print out the information for shell-users
System.out.print("Importing ");
System.out.print(destination + " ");
boolean success = true;
byte[] content = null;
String fullname = null;
try {
String path = m_importPath + destination.substring(0, destination.lastIndexOf("/") + 1);
String name = destination.substring((destination.lastIndexOf("/") + 1), destination.length());
if (source != null){
content = getFileBytes(source);
}
// set invalid files to type compatible_plain
type = fitFileType(path, name, content, type, properties);
CmsResource res = m_cms.importResource(source, destination, type, user, group, access,
properties, launcherStartClass, content, m_importPath);
if(res != null){
fullname = res.getAbsolutePath();
}
System.out.println("OK");
} catch (Exception exc) {
// an error while importing the file
success = false;
System.out.println("Error: "+exc.toString());
}
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 even if there already exist conflicting files
*/
public void importResources() throws CmsException {
if (m_cms.isAdmin()){
importGroups();
importUsers();
}
importResources(null, null, null, null, null);
if (m_importZip != null){
try{
m_importZip.close();
} catch (IOException exc) {
throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
}
}
}
/**
* Imports the resources and writes them to the cms.
* param excludeList filenames of files and folders which should not be (over) written in the virtual file system
* 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
*/
public void importResources(Vector excludeList, Vector writtenFilenames, Vector fileCodes, String propertyName, String propertyValue) throws CmsException {
NodeList fileNodes, propertyNodes;
Element currentElement, currentProperty;
String source, destination, type, user, group, access, launcherStartClass;
Hashtable properties;
Vector types = new Vector(); // stores the file types for which the property already exists
// first lock the resource to import
// m_cms.lockResource(m_importPath);
try {
// 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);
// get all information for a file-import
source = getTextNodeValue(currentElement, C_EXPORT_TAG_SOURCE);
destination = getTextNodeValue(currentElement, C_EXPORT_TAG_DESTINATION);
type = getTextNodeValue(currentElement, C_EXPORT_TAG_TYPE);
user = getTextNodeValue(currentElement, C_EXPORT_TAG_USER);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -