Class Utils

java.lang.Object
net.thauvin.erik.httpstatus.Utils

@NullMarked public final class Utils extends Object
A collection of utility methods used throughout the project. This class is not intended to be instantiated.

Provides helpers for XML escaping and safe writer output.

Since:
1.0.0
Author:
Erik C. Thauvin
  • Method Details

    • escapeXml

      public static String escapeXml(String value)
      Converts the characters <, >, &, ', and " to their corresponding XML entity codes.

      Returns the original value when empty. Only the predefined XML entities are escaped; all other characters are preserved as-is.

      Parameters:
      value - the string to escape
      Returns:
      the escaped string
      Throws:
      NullPointerException - if the value is null
    • outWrite

      public static void outWrite(Writer out, @Nullable String value, @Nullable String defaultValue, boolean escapeXml) throws IOException
      Writes a string value to the given writer, optionally escaping XML characters. When the value is null, the provided default value is used instead. If both are null, nothing is written.

      The writer must not be null. XML escaping is performed using escapeXml(String).

      Parameters:
      out - the writer to write to
      value - the string value to write, may be null
      defaultValue - the default value to use when value is null, may be null
      escapeXml - true to escape XML characters, false otherwise
      Throws:
      IOException - If an I/O error occurs while writing
      NullPointerException - If the writer is null