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
Section titled “Path navigation”getParent
Section titled “getParent”Description:
| Method | Return values | Description |
|---|---|---|
| getParent(String path) | String | Returns the parent path. Returns "/" if the path has no parent segment. |
Example:
System.out.println(PathUtils.getParent("/okm:root/folder/file.pdf")); // "/okm:root/folder"System.out.println(PathUtils.getParent("/okm:root")); // "/"getName
Section titled “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
Section titled “getContext”Description:
| Method | Return values | Description |
|---|---|---|
| getContext(String path) | String | Returns the root context segment of a path. For example, "/okm:root/folder/file.pdf" returns "/okm:root". |
Example:
System.out.println(PathUtils.getContext("/okm:root/folder/file.pdf")); // "/okm:root"getDepth
Section titled “getDepth”Description:
| Method | Return values | Description |
|---|---|---|
| getDepth(String path) | int | Returns the number of path segments (depth). For example, "/okm:root/folder/file.pdf" returns 3. |
getElements
Section titled “getElements”Description:
| Method | Return values | Description |
|---|---|---|
| getElements(String path) | String[] | Returns all path segments as a string array. For example, "/okm:root/folder/file.pdf" returns ["okm:root", "folder", "file.pdf"]. |
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
Section titled “Path and name sanitization”escape
Section titled “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
Section titled “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
Section titled “toValidPathName”Description:
| Method | Return values | Description |
|---|---|---|
| toValidPathName(String path) | String | Applies cleanup and FileUtils.toValidFilename to every path segment below the root context, returning a sanitized full path. |
shortenName
Section titled “shortenName”Description:
| Method | Return values | Description |
|---|---|---|
| shortenName(String name, int maxLength) | String | Truncates a file name to at most maxLength characters, preserving the extension. Returns the name unchanged if it is already within the limit. Returns null for null input. |
shortenPath
Section titled “shortenPath”Description:
| Method | Return values | Description |
|---|---|---|
| shortenPath(String path, int maxLength) | String | Shortens a repository path to at most maxLength characters by replacing the middle of the parent path with ... while keeping the file name intact. Returns the path unchanged if it already fits. |
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
Section titled “Entity encoding”encodeEntities
Section titled “encodeEntities”Description:
| Method | Return values | Description |
|---|---|---|
| encodeEntities(String path) | String | Encodes HTML entities (&, <, >, ", ') in a path or string to their entity equivalents. Idempotent: already-encoded entities are not double-encoded. |
decodeEntities
Section titled “decodeEntities”Description:
| Method | Return values | Description |
|---|---|---|
| decodeEntities(String path) | String | Decodes HTML entity references back to their original characters. Reverse of encodeEntities. |
Path validation and inspection
Section titled “Path validation and inspection”isPath
Section titled “isPath”Description:
| Method | Return values | Description |
|---|---|---|
| isPath(String nodeId) | boolean | Returns true if the given string is a repository path (starts with /). Returns false for UUIDs. |
checkPath
Section titled “checkPath”Description:
| Method | Return values | Description |
|---|---|---|
| checkPath(String path) | boolean | Returns true if the given path is a valid OpenKM repository path (non-null and starts with /okm:). |
isChild
Section titled “isChild”Description:
| Method | Return values | Description |
|---|---|---|
| isChild(String parentPath, String childPath) | boolean | Returns true if childPath is a direct or indirect descendant of parentPath. Returns false if either argument is null. |
- Examples:
fixContext
Section titled “fixContext”Description:
| Method | Return values | Description |
|---|---|---|
| fixContext(String context) | String | Converts a context path to a file-system-safe identifier by removing the leading / and replacing : with _. For example, "/okm:root" becomes "okm_root". |
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(); } }}