PathUtils
Utility class with static helper methods for working with OpenKM repository paths (e.g. /okm:root/folder/file.pdf). All methods are static.
Path navigation
getParent
Description:
| Method | Return values | Description |
|---|---|---|
|
getParent(String path) |
String |
Returns the parent path. Returns |
Example:
System.out.println(PathUtils.getParent("/okm:root/folder/file.pdf")); // "/okm:root/folder"
System.out.println(PathUtils.getParent("/okm:root")); // "/"
getName
Description:
| Method | Return values | Description |
|---|---|---|
|
getName(String path) |
String |
Returns the last segment of a path (the node name). |
Example:
System.out.println(PathUtils.getName("/okm:root/folder/file.pdf")); // "file.pdf"
getContext
Description:
| Method | Return values | Description |
|---|---|---|
|
getContext(String path) |
String |
Returns the root context segment of a path. For example, |
Example:
System.out.println(PathUtils.getContext("/okm:root/folder/file.pdf")); // "/okm:root"
getDepth
Description:
| Method | Return values | Description |
|---|---|---|
|
getDepth(String path) |
int |
Returns the number of path segments (depth). For example, |
getElements
Description:
| Method | Return values | Description |
|---|---|---|
|
getElements(String path) |
String[] |
Returns all path segments as a string array. For example, |
Example:
package com.openkm;
import com.openkm.util.PathUtils;
public class Test {
public static void main(String[] args) {
try {
String path = "/okm:root/folder/file.pdf";
System.out.println(PathUtils.getParent(path)); // "/okm:root/folder"
System.out.println(PathUtils.getName(path)); // "file.pdf"
System.out.println(PathUtils.getContext(path)); // "/okm:root"
System.out.println(PathUtils.getDepth(path)); // 3
} catch (Exception e) {
e.printStackTrace();
}
}
}
Path and name sanitization
escape
Description:
| Method | Return values | Description |
|---|---|---|
|
escape(String name) |
String |
Removes characters that are invalid in both Windows file names and OpenKM node names (slashes, asterisks, and other reserved characters). Use this when creating node names from arbitrary user input. |
cleanup
Description:
| Method | Return values | Description |
|---|---|---|
|
cleanup(String name) |
String |
Removes slashes and asterisks from a node name, collapses multiple consecutive spaces into one, and trims leading and trailing whitespace. |
toValidPathName
Description:
| Method | Return values | Description |
|---|---|---|
|
toValidPathName(String path) |
String |
Applies |
shortenName
Description:
| Method | Return values | Description |
|---|---|---|
|
shortenName(String name, int maxLength) |
String |
Truncates a file name to at most |
shortenPath
Description:
| Method | Return values | Description |
|---|---|---|
|
shortenPath(String path, int maxLength) |
String |
Shortens a repository path to at most |
Example:
String name = PathUtils.escape("file: version 1.0/final.pdf"); // "file version 1.0final.pdf"
String short1 = PathUtils.shortenName("very_long_document_name.pdf", 20); // "very_long_docume.pdf"
String short2 = PathUtils.shortenPath("/okm:root/very/deep/nested/path/file.pdf", 30);
// "/okm:root/ver.../path/file.pdf"
Entity encoding
encodeEntities
Description:
| Method | Return values | Description |
|---|---|---|
|
encodeEntities(String path) |
String |
Encodes HTML entities ( |
decodeEntities
Description:
| Method | Return values | Description |
|---|---|---|
|
decodeEntities(String path) |
String |
Decodes HTML entity references back to their original characters. Reverse of |
Path validation and inspection
isPath
Description:
| Method | Return values | Description |
|---|---|---|
|
isPath(String nodeId) |
boolean |
Returns |
checkPath
Description:
| Method | Return values | Description |
|---|---|---|
|
checkPath(String path) |
boolean |
Returns |
isChild
Description:
| Method | Return values | Description |
|---|---|---|
|
isChild(String parentPath, String childPath) |
boolean |
Returns |
|
Examples:
|
||
fixContext
Description:
| Method | Return values | Description |
|---|---|---|
|
fixContext(String context) |
String |
Converts a context path to a file-system-safe identifier by removing the leading |
Example:
package com.openkm;
import com.openkm.util.PathUtils;
public class Test {
public static void main(String[] args) {
try {
System.out.println(PathUtils.isPath("/okm:root/file.pdf")); // true
System.out.println(PathUtils.isPath("d50133e3-dbfa-4d01-a109-28785cd48f40")); // false
System.out.println(PathUtils.checkPath("/okm:root/file.pdf")); // true
System.out.println(PathUtils.isChild("/okm:root/folder", "/okm:root/folder/sub/file.pdf")); // true
System.out.println(PathUtils.fixContext("/okm:root")); // "okm_root"
} catch (Exception e) {
e.printStackTrace();
}
}
}