cmsresource.java
来自「找了很久才找到到源代码」· Java 代码 · 共 1,296 行 · 第 1/4 页
JAVA
1,296 行
* The parent resource of a folder is the parent folder.
* The parent resource of the root folder is <code>null</code>.<p>
*
* Example: <code>/system/workplace/</code> has the parent <code>/system/</code>.
*
* @param resource the resource to find the parent folder for
* @return the calculated parent absolute folder path, or <code>null</code> for the root folder
*/
public static String getParentFolder(String resource) {
if (CmsStringUtil.isEmptyOrWhitespaceOnly(resource) || "/".equals(resource)) {
return null;
}
// remove the last char, for a folder this will be "/", for a file it does not matter
String parent = (resource.substring(0, resource.length() - 1));
// now as the name does not end with "/", check for the last "/" which is the parent folder name
return parent.substring(0, parent.lastIndexOf('/') + 1);
}
/**
* Returns the directory level of a resource.<p>
*
* The root folder "/" has level 0,
* a folder "/foo/" would have level 1,
* a folfer "/foo/bar/" level 2 etc.<p>
*
* @param resource the resource to determine the directory level for
* @return the directory level of a resource
*/
public static int getPathLevel(String resource) {
int level = -1;
int pos = 0;
while (resource.indexOf('/', pos) >= 0) {
pos = resource.indexOf('/', pos) + 1;
level++;
}
return level;
}
/**
* Returns the name of a parent folder of the given resource,
* that is either minus levels up
* from the current folder, or that is plus levels down from the
* root folder.<p>
*
* @param resource the name of a resource
* @param level of levels to walk up or down
* @return the name of a parent folder of the given resource
*/
public static String getPathPart(String resource, int level) {
resource = getFolderPath(resource);
String result = null;
int pos = 0, count = 0;
if (level >= 0) {
// Walk down from the root folder /
while ((count < level) && (pos > -1)) {
count++;
pos = resource.indexOf('/', pos + 1);
}
} else {
// Walk up from the current folder
pos = resource.length();
while ((count > level) && (pos > -1)) {
count--;
pos = resource.lastIndexOf('/', pos - 1);
}
}
if (pos > -1) {
// To many levels walked
result = resource.substring(0, pos + 1);
} else {
// Add trailing slash
result = (level < 0) ? "/" : resource;
}
return result;
}
/**
* Returns true if the resource name certainly denotes a folder, that is ends with a "/".<p>
*
* @param resource the resource to check
* @return true if the resource name certainly denotes a folder, that is ends with a "/"
*/
public static boolean isFolder(String resource) {
return CmsStringUtil.isNotEmpty(resource) && (resource.charAt(resource.length() - 1) == '/');
}
/**
* Returns a clone of this Objects instance.<p>
*
* @return a clone of this instance
*/
public Object clone() {
CmsResource clone = new CmsResource(
m_structureId,
m_resourceId,
m_rootPath,
m_typeId,
m_isFolder,
m_flags,
m_projectLastModified,
m_state,
m_dateCreated,
m_userCreated,
m_dateLastModified,
m_userLastModified,
m_dateReleased,
m_dateExpired,
m_siblingCount,
m_length,
m_dateContent,
m_version);
if (isTouched()) {
clone.setDateLastModified(m_dateLastModified);
}
return clone;
}
/**
* Uses the resource root path to compare two resources.<p>
*
* Please note a number of additional comparators for resources exists as members of this class.<p>
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*
* @see #COMPARE_DATE_RELEASED
* @see #COMPARE_ROOT_PATH
* @see #COMPARE_ROOT_PATH_IGNORE_CASE
* @see #COMPARE_ROOT_PATH_IGNORE_CASE_FOLDERS_FIRST
*/
public int compareTo(Object obj) {
if (obj == this) {
return 0;
}
if (obj instanceof CmsResource) {
return m_rootPath.compareTo(((CmsResource)obj).m_rootPath);
}
return 0;
}
/**
* Two resources are considered equal in case their structure id is equal.<p>
*
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof CmsResource) {
return ((CmsResource)obj).m_structureId.equals(m_structureId);
}
return false;
}
/**
* Returns the date of the last modification of the content of this resource.<p>
*
* This applies only to resources of type {@link CmsFile}, since a {@link CmsFolder} has no content.
* In case of a folder, <code>-1</code> is always returned as content date.<p>
*
* Any modification of a resource, including changes to the resource properties,
* will increase the "date of last modification" which is returned by {@link #getDateLastModified()}.
* The "date of the content" as returned by this method only changes when the
* file content as returned by {@link CmsFile#getContents()} is changed.<p>
*
* @return the date of the last modification of the content of this resource
*
* @since 7.0.0
*/
public long getDateContent() {
return m_dateContent;
}
/**
* Returns the date of the creation of this resource.<p>
*
* @return the date of the creation of this resource
*/
public long getDateCreated() {
return m_dateCreated;
}
/**
* Returns the expiration date this resource.<p>
*
* If the expiration date has not been set, {@link #DATE_EXPIRED_DEFAULT} is returned.
* This means: The resource does never expire.<p>
*
* @return the expiration date of this resource
*/
public long getDateExpired() {
return m_dateExpired;
}
/**
* Returns the date of the last modification of this resource.<p>
*
* @return the date of the last modification of this resource
*/
public long getDateLastModified() {
return m_dateLastModified;
}
/**
* Returns the release date this resource.<p>
*
* If the release date has not been set, {@link #DATE_RELEASED_DEFAULT} is returned.
* This means: The resource has always been released.<p>
*
* @return the release date of this resource
*/
public long getDateReleased() {
return m_dateReleased;
}
/**
* Returns the flags of this resource.<p>
*
* @return the flags of this resource
*
* @see #setFlags(int) for an explanation of the resource flags
*/
public int getFlags() {
return m_flags;
}
/**
* Returns the length of the resource.<p>
*
* If the resource is a file, then this is the byte size of the file content.
* If the resource is a folder, then the size is always -1.<p>
*
* @return the length of the content
*/
public int getLength() {
// make sure folders always have a -1 size
return m_isFolder ? -1 : m_length;
}
/**
* Returns the name of this resource without parent folders, for example <code>index.html</code>.<p>
*
* @return the name of this resource without parent folders
*/
public String getName() {
String name = getName(m_rootPath);
if (name.charAt(name.length() - 1) == '/') {
return name.substring(0, name.length() - 1);
} else {
return name;
}
}
/**
* Returns the id of the project where the resource has been last modified.<p>
*
* @return the id of the project where the resource has been last modified, or <code>null</code>
*/
public CmsUUID getProjectLastModified() {
return m_projectLastModified;
}
/**
* Returns the id of the resource database entry of this resource.<p>
*
* @return the id of the resource database entry
*/
public CmsUUID getResourceId() {
return m_resourceId;
}
/**
* Returns the name of a resource with it's full path from the root folder
* including the current site root,
* for example <code>/sites/default/myfolder/index.html</code>.<p>
*
* In a presentation level application usually the current site root must be
* cut of from the root path. Use {@link CmsObject#getSitePath(CmsResource)}
* to get the "absolute" path of a resource in the current site.<p>
*
* @return the name of a resource with it's full path from the root folder
* including the current site root
*
* @see CmsObject#getSitePath(CmsResource)
* @see CmsRequestContext#getSitePath(CmsResource)
* @see CmsRequestContext#removeSiteRoot(String)
*/
public String getRootPath() {
return m_rootPath;
}
/**
* Returns the number of siblings of this resource, also counting this resource.<p>
*
* If a resource has no sibling, the total sibling count for this resource is <code>1</code>,
* if a resource has <code>n</code> siblings, the sibling count is <code>n + 1</code>.<p>
*
* @return the number of siblings of this resource, also counting this resource
*/
public int getSiblingCount() {
return m_siblingCount;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?