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:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

getDepth(String path)

int

Returns the number of path segments (depth). For example, "/okm:root/folder/file.pdf" returns 3.

getElements

Description:

MethodReturn valuesDescription

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

escape

Description:

MethodReturn valuesDescription

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:

MethodReturn valuesDescription

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:

MethodReturn valuesDescription

toValidPathName(String path)

String

Applies cleanup and FileUtils.toValidFilename to every path segment below the root context, returning a sanitized full path.

shortenName

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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

encodeEntities

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

decodeEntities(String path)

String

Decodes HTML entity references back to their original characters. Reverse of encodeEntities.

Path validation and inspection

isPath

Description:

MethodReturn valuesDescription

isPath(String nodeId)

boolean

Returns true if the given string is a repository path (starts with /). Returns false for UUIDs.

checkPath

Description:

MethodReturn valuesDescription

checkPath(String path)

boolean

Returns true if the given path is a valid OpenKM repository path (non-null and starts with /okm:).

isChild

Description:

MethodReturn valuesDescription

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:

  • isChild("/okm:root", "/okm:root/Folder") ? true
  • isChild("/okm:root/Folder", "/okm:root/Folder2") ? false
  • isChild("/okm:root/Folder2", "/okm:root/Folder/Folder2") ? false

fixContext

Description:

MethodReturn valuesDescription

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