FormatUtil
Utility class with static helper methods for formatting sizes, dates, time intervals, and strings; sanitizing and escaping HTML; and validating common data types. All methods are static.
Size formatting
Section titled “Size formatting”formatSize
Section titled “formatSize”Description:
| Method | Return values | Description |
|---|---|---|
| formatSize(long bytes) | String | Formats a byte count as a human-readable size string (e.g. 1.5 MB). Units: B, KB, MB, GB, TB, PB, EB. |
- bytes: The size in bytes.
Example:
package com.openkm;
import java.io.File;import com.openkm.util.FormatUtil;
public class Test {
public static void main(String[] args) { try { File file = new File("/home/openkm/test.png"); System.out.println(FormatUtil.formatSize(file.length())); // e.g. "2.3 MB" } catch (Exception e) { e.printStackTrace(); } }}parseSize
Section titled “parseSize”Description:
| Method | Return values | Description |
|---|---|---|
| parseSize(String text) | long | Parses a human-readable size string (e.g. "10MB", "2GB") into bytes. Supports KB, MB, GB suffixes. |
- text: The size string to parse.
Example:
long bytes = FormatUtil.parseSize("10MB");System.out.println(bytes); // 10485760Date formatting
Section titled “Date formatting”formatDate (Calendar)
Section titled “formatDate (Calendar)”Description:
| Method | Return values | Description |
|---|---|---|
| formatDate(Calendar cal) | String | Formats a Calendar value as dd-MM-yyyy HH:mm:ss in the calendar’s own time zone. Returns null if cal is null. |
Example:
import java.util.Calendar;import com.openkm.util.FormatUtil;
System.out.println(FormatUtil.formatDate(Calendar.getInstance())); // "15-03-2024 14:30:00"formatDate (Date)
Section titled “formatDate (Date)”Description:
| Method | Return values | Description |
|---|---|---|
| formatDate(Date date) | String | Formats a Date value as dd-MM-yyyy HH:mm:ss. |
formatDate (Unix epoch)
Section titled “formatDate (Unix epoch)”Description:
| Method | Return values | Description |
|---|---|---|
| formatDate(long unixEpoch) | String | Formats a Unix epoch value (milliseconds since 1970-01-01) as dd-MM-yyyy HH:mm:ss. |
Example:
System.out.println(FormatUtil.formatDate(System.currentTimeMillis())); // "15-03-2024 14:30:00"Time interval formatting
Section titled “Time interval formatting”formatSeconds
Section titled “formatSeconds”Description:
| Method | Return values | Description |
|---|---|---|
| formatSeconds(long time) | String | Formats a duration in milliseconds as HH:MM:SS. |
formatSecondsSince
Section titled “formatSecondsSince”Description:
| Method | Return values | Description |
|---|---|---|
| formatSecondsSince(long time) | String | Formats the elapsed time since the given start timestamp (in milliseconds) as HH:MM:SS. Pass the result of System.currentTimeMillis() at the start of the operation. |
formatMilliSeconds
Section titled “formatMilliSeconds”Description:
| Method | Return values | Description |
|---|---|---|
| formatMilliSeconds(long time) | String | Formats a duration in milliseconds as HH:MM:SS.mmm. |
formatMilliSecondsSince
Section titled “formatMilliSecondsSince”Description:
| Method | Return values | Description |
|---|---|---|
| formatMilliSecondsSince(long time) | String | Formats the elapsed time since the given start timestamp (in milliseconds) as HH:MM:SS.mmm. Pass the result of System.currentTimeMillis() at the start of the operation. |
Example:
long start = System.currentTimeMillis();// ... long operation ...System.out.println("Elapsed: " + FormatUtil.formatMilliSecondsSince(start)); // "00:00:01.234"String formatting
Section titled “String formatting”formatArray
Section titled “formatArray”Description:
| Method | Return values | Description |
|---|---|---|
| formatArray(String[] values) | String | Converts a string array to a readable string. Returns the single element directly for one-element arrays, and the Apache Commons ArrayUtils.toString representation for multi-element arrays. Returns null for null input. |
formatObject
Section titled “formatObject”Description:
| Method | Return values | Description |
|---|---|---|
| formatObject(Object value) | String | Converts an object to a string. For Object[] uses ArrayUtils.toString; otherwise calls toString(). Returns null for null input. |
HTML and input sanitization
Section titled “HTML and input sanitization”escapeHtml
Section titled “escapeHtml”Description:
| Method | Return values | Description |
|---|---|---|
| escapeHtml(String str) | String | Escapes < and > as HTML entities, and converts newlines to <br/>. Returns null for null input. |
sanitizeInput
Section titled “sanitizeInput”Description:
| Method | Return values | Description |
|---|---|---|
| sanitizeInput(String string) | String | Sanitizes an HTML string to remove potentially dangerous content (XSS). Uses either the AntiSamy library or a legacy regex approach depending on the Config.SECURITY_INPUT_SANITIZER setting. Returns null for null input. |
cleanXSS
Section titled “cleanXSS”Description:
| Method | Return values | Description |
|---|---|---|
| cleanXSS(String value) | String | Cleans a string by escaping HTML angle brackets and parentheses, removing eval() expressions and inline JavaScript, and stripping script keywords. |
sanitizeEmailString
Section titled “sanitizeEmailString”Description:
| Method | Return values | Description |
|---|---|---|
| sanitizeEmailString(String value) | String | Removes any characters that are not letters, digits, punctuation, or spaces from the given string. Useful for sanitizing email address strings. |
Example:
String safe = FormatUtil.escapeHtml("<b>Hello</b>\nWorld");// "<b>Hello</b><br/>World"
String sanitized = FormatUtil.sanitizeInput("<script>alert('xss')</script>Hello");System.out.println(sanitized); // "Hello"String and encoding validation
Section titled “String and encoding validation”isValidUUID
Section titled “isValidUUID”Description:
| Method | Return values | Description |
|---|---|---|
| isValidUUID(String str) | boolean | Returns true if the given string is a valid UUID. |
Example:
boolean valid = FormatUtil.isValidUUID("d50133e3-dbfa-4d01-a109-28785cd48f40"); // trueboolean invalid = FormatUtil.isValidUUID("not-a-uuid"); // falsevalidUTF8
Section titled “validUTF8”Description:
| Method | Return values | Description |
|---|---|---|
| validUTF8(byte[] input) | boolean | Returns true if the given byte array is a valid UTF-8 sequence. |
fixUTF8 (byte array)
Section titled “fixUTF8 (byte array)”Description:
| Method | Return values | Description |
|---|---|---|
| fixUTF8(byte[] input) | byte[] | Replaces null bytes (0x00) with spaces (0x20) in a byte array. |
fixUTF8 (String)
Section titled “fixUTF8 (String)”Description:
| Method | Return values | Description |
|---|---|---|
| fixUTF8(String input) | String | Replaces null Unicode characters (\u0000) with spaces and trims the result. Returns null for null input. |
trimUnicodeSurrogates
Section titled “trimUnicodeSurrogates”Description:
| Method | Return values | Description |
|---|---|---|
| trimUnicodeSurrogates(String text) | String | Removes Unicode surrogate characters from the given string and trims it. Returns null for null input. Useful for preparing text before indexing or XML serialization. |
stripNonValidXMLCharacters
Section titled “stripNonValidXMLCharacters”Description:
| Method | Return values | Description |
|---|---|---|
| stripNonValidXMLCharacters(String in) | String | Removes all characters that are not valid in XML 1.0. Returns an empty string for null or empty input. Use this before inserting arbitrary text into an XML document. |
Example:
String cleaned = FormatUtil.stripNonValidXMLCharacters("Hello\u0008World");System.out.println(cleaned); // "HelloWorld"