📄 cmsresourcetypepage.java
字号:
/**
* Copies a Resource.
*
* @param source the complete path of the sourcefile.
* @param destination the complete path of the destinationfolder.
* @param keepFlags <code>true</code> if the copy should keep the source file's flags,
* <code>false</code> if the copy should get the user's default flags.
*
* @exception CmsException if the file couldn't be copied, or the user
* has not the appropriate rights to copy the file.
*/
public void copyResource(CmsObject cms, String source, String destination, boolean keepFlags) throws CmsException{
// Read and parse the source page file
CmsFile file = cms.readFile(source);
CmsXmlControlFile hXml=new CmsXmlControlFile(cms, file);
// Check the path of the body file.
// Don't use the checkBodyPath method here to avaoid overhead.
String bodyPath=(C_CONTENTBODYPATH.substring(0, C_CONTENTBODYPATH.lastIndexOf("/")))+(source);
if (bodyPath.equals(hXml.getElementTemplate("body"))){
// Evaluate some path information
String destinationFolder = destination.substring(0,destination.lastIndexOf("/")+1);
checkFolders(cms, destinationFolder);
String newbodyPath=(C_CONTENTBODYPATH.substring(0, C_CONTENTBODYPATH.lastIndexOf("/")))+ destination;
// we don't want to use the changeContent method here
// to avoid overhead by copying, readig, parsing, setting XML and writing again.
// Instead, we re-use the already parsed XML content of the source
hXml.setElementTemplate("body", newbodyPath);
cms.doCopyFile(source, destination);
CmsFile newPageFile = cms.readFile(destination);
newPageFile.setContents(hXml.getXmlText().getBytes());
cms.writeFile(newPageFile);
// Now the new page file is created. Copy the body file
cms.doCopyFile(bodyPath, newbodyPath);
// set access flags, if neccessary
} else {
// The body part of the source was not found at
// the default place. Leave it there, don't make
// a copy and simply make a copy of the page file.
// So the new page links to the old body.
cms.doCopyFile(source, destination);
// set access flags, if neccessary
}
if(!keepFlags) {
setDefaultFlags(cms, destination);
}
}
/**
* Copies a resource from the online project to a new, specified project.
* <br>
* Copying a resource will copy the file header or folder into the specified
* offline project and set its state to UNCHANGED.
*
* @param resource the name of the resource.
* @exception CmsException if operation was not successful.
*/
public void copyResourceToProject(CmsObject cms, String resourceName) throws CmsException {
//String resourceName = linkManager.getResourceName(resourceId);
CmsFile file = cms.readFile(resourceName, true);
cms.doCopyResourceToProject(resourceName);
//check if the file type name is page
String bodyPath = checkBodyPath(cms, (CmsFile)file);
if (bodyPath != null){
cms.doCopyResourceToProject(bodyPath);
}
}
/**
* Creates a new resource
*
* @param cms The CmsObject
* @param folder The name of the parent folder
* @param name The name of the file
* @param properties The properties of the file
* @param contents The file content
*
* @exception CmsException if operation was not successful.
*/
public CmsResource createResource(CmsObject cms, String folder, String name, Hashtable properties, byte[] contents) throws CmsException{
// Scan for mastertemplates
Vector allMasterTemplates = cms.getFilesInFolder(C_CONTENTTEMPLATEPATH);
// Select the first mastertemplate as default
String masterTemplate = "";
if(allMasterTemplates.size() > 0) {
masterTemplate = ((CmsFile)allMasterTemplates.elementAt(0)).getAbsolutePath();
}
// Evaluate the absolute path to the new body file
String bodyFolder =(C_CONTENTBODYPATH.substring(0, C_CONTENTBODYPATH.lastIndexOf("/"))) + folder;
// Create the new page file
//CmsFile file = cms.doCreateFile(folder, name, "".getBytes(), I_CmsConstants.C_TYPE_PAGE_NAME, properties);
CmsFile file = cms.doCreateFile(folder, name, "".getBytes(), m_resourceTypeName, properties);
cms.doLockResource(folder + name, true);
CmsXmlControlFile pageXml = new CmsXmlControlFile(cms, file);
pageXml.setTemplateClass(C_CLASSNAME);
pageXml.setMasterTemplate(masterTemplate);
pageXml.setElementClass("body", C_CLASSNAME);
pageXml.setElementTemplate("body", bodyFolder + name);
pageXml.write();
// Check, if the body path exists and create missing folders, if neccessary
checkFolders(cms, folder);
// Create the new body file
//CmsFile bodyFile = cms.doCreateFile(bodyFolder, name, (C_DEFAULTBODY_START + new String(contents) + C_DEFAULTBODY_END).getBytes(), I_CmsConstants.C_TYPE_BODY_NAME, new Hashtable());
CmsFile bodyFile = cms.doCreateFile(bodyFolder, name, (C_DEFAULTBODY_START + new String(contents) + C_DEFAULTBODY_END).getBytes(), I_CmsConstants.C_TYPE_PLAIN_NAME, new Hashtable());
cms.doLockResource(bodyFolder + name, true);
int flags = bodyFile.getAccessFlags();
if ((flags & C_ACCESS_INTERNAL_READ) ==0 ) {
flags += C_ACCESS_INTERNAL_READ;
}
cms.chmod(bodyFile.getAbsolutePath(), flags);
return file;
}
public CmsResource createResource(CmsObject cms, String folder, String name, Hashtable properties, byte[] contents, String masterTemplate) throws CmsException{
CmsFile resource = (CmsFile)createResource(cms, folder, name, properties, contents);
CmsXmlControlFile pageXml = new CmsXmlControlFile(cms, resource);
pageXml.setMasterTemplate(masterTemplate);
pageXml.write();
return resource;
}
/**
* Deletes a resource.
*
* @param filename the complete path of the file.
*
* @exception CmsException if the file couldn't be deleted, or if the user
* has not the appropriate rights to delete the file.
*/
public void deleteResource(CmsObject cms, String filename) throws CmsException{
CmsFile file = cms.readFile(filename);
cms.doDeleteFile(filename);
String bodyPath = checkBodyPath(cms, (CmsFile)file);
if (bodyPath != null){
try{
cms.doDeleteFile(bodyPath);
} catch (CmsException e){
if(e.getType() != CmsException.C_NOT_FOUND){
throw e;
}
}
}
// The page file contains XML.
// So there could be some data in the parser's cache.
// Clear it!
String currentProject = cms.getRequestContext().currentProject().getName();
CmsXmlControlFile.clearFileCache(currentProject + ":" + filename);
}
/**
* Undeletes a resource.
*
* @param filename the complete path of the file.
*
* @exception CmsException if the file couldn't be undeleted, or if the user
* has not the appropriate rights to undelete the file.
*/
public void undeleteResource(CmsObject cms, String filename) throws CmsException{
CmsFile file = cms.readFile(filename, true);
cms.doUndeleteFile(filename);
String bodyPath = checkBodyPath(cms, (CmsFile)file);
if (bodyPath != null){
try{
cms.doUndeleteFile(bodyPath);
} catch (CmsException e){
if(e.getType() != CmsException.C_NOT_FOUND){
throw e;
}
}
}
// The page file contains XML.
// So there could be some data in the parser's cache.
// Clear it!
String currentProject = cms.getRequestContext().currentProject().getName();
CmsXmlControlFile.clearFileCache(currentProject + ":" + filename);
}
/**
* When a resource has to be exported, the ID磗 inside the
* Linkmanagement-Tags have to be changed to the corresponding URL磗
*
* @param file is the file that has to be changed
*/
public CmsFile exportResource(CmsObject cms, CmsFile file) throws CmsException {
//nothing to do here, because there couldn磘 be any Linkmanagement-Tags in a page-file (control-file)
return file;
}
/**
* When a resource has to be imported, the URL磗 of the
* Links inside the resources have to be saved and changed to the corresponding ID磗
*
* @param file is the file that has to be changed
*/
public CmsResource importResource(CmsObject cms, String source, String destination, String type, String user, String group, String access, Hashtable properties, String launcherStartClass, byte[] content, String importPath) throws CmsException {
CmsFile file = null;
String path = importPath + destination.substring(0, destination.lastIndexOf("/") + 1);
String name = destination.substring((destination.lastIndexOf("/") + 1), destination.length());
int state = C_STATE_NEW;
// this is a file
// first delete the file, so it can be overwritten
try {
cms.doLockResource(path + name, true);
cms.doDeleteFile(path + name);
state = C_STATE_CHANGED;
} catch (CmsException exc) {
state = C_STATE_NEW;
// ignore the exception, the file dosen't exist
}
// now create the file
// do not use createResource because then there will the body-file be created too.
// that would cause an exception while importing because of trying to
// duplicate an entry
file = (CmsFile)cms.doCreateFile(path, name, content, type, properties);
String fullname = file.getAbsolutePath();
lockResource(cms, fullname, true);
try{
cms.doChmod(fullname, Integer.parseInt(access));
}catch(CmsException e){
System.out.println("chmod(" + access + ") failed ");
}
try{
cms.doChgrp(fullname, group);
}catch(CmsException e){
System.out.println("chgrp(" + group + ") failed ");
}
try{
cms.doChown(fullname, user);
}catch(CmsException e){
System.out.println("chown((" + user + ") failed ");
}
if(launcherStartClass != null){
file.setLauncherClassname(launcherStartClass);
cms.writeFile(file);
}
return file;
}
/**
* Locks a given resource.
* <br>
* A user can lock a resource, so he is the only one who can write this
* resource.
*
* @param resource the complete path to the resource to lock.
* @param force if force is <code>true</code>, a existing locking will be overwritten.
*
* @exception CmsException if the user has not the rights to lock this resource.
* It will also be thrown, if there is a existing lock and force was set to false.
*/
public void lockResource(CmsObject cms, String resource, boolean force) throws CmsException{
// First read the page file.
CmsFile pageFile = cms.readFile(resource);
CmsUser pageLocker = null;
CmsUser bodyLocker = null;
// Check any locks on th page file
pageLocker = getLockedBy(cms, resource);
CmsUser currentUser = cms.getRequestContext().currentUser();
boolean pageLockedAndSelf = pageLocker != null && currentUser.equals(pageLocker);
CmsResource bodyFile = null;
String bodyPath = null;
// Try to fetch the body file.
try {
bodyPath = readBodyPath(cms, pageFile);
bodyFile = cms.readFileHeader(bodyPath);
} catch(Exception e) {
bodyPath = null;
bodyFile = null;
}
// first lock the page file
cms.doLockResource(resource, force);
if(bodyFile != null) {
// Everything with the page file is ok. We have write access. XML is valid.
// Body file could be determined and fetched.
// Now check further body file details (is it locked already, WHO has locked it, etc.)
bodyLocker = getLockedBy(cms, bodyPath);
// Lock the body, if neccessary
//if((bodyLocker == null && (pageLocker == null || pageLockedAndSelf || force))
// || (bodyLocker != null && !currentUser.equals(bodyLocker)
// && !(pageLocker != null && !currentUser.equals(pageLocker) && !force))) {
cms.doLockResource(bodyPath, force);
//}
}
/*
// Lock the page file, if neccessary
if(!(pageLockedAndSelf && (bodyFile != null && ((bodyLocker == null)
|| !currentUser.equals(bodyLocker))))) {
cms.doLockResource(resource, force);
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -