Skip to content

PathUtils

Utility class with static helper methods for working with OpenKM repository paths (e.g. /okm:root/folder/file.pdf). All methods are static.

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")); // "/"

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"

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"

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.

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();
}
}
}

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.

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.

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.

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.

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"

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.

Description:

Method Return values Description
decodeEntities(String path) String Decodes HTML entity references back to their original characters. Reverse of encodeEntities.

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.

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:).

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:

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();
}
}
}