Class XMLFormatPreserver

java.lang.Object
org.apache.catalina.storeconfig.XMLFormatPreserver

public final class XMLFormatPreserver extends Object
Best-effort preservation of the original layout (comments, blank lines and attribute order) of server.xml and context configuration files when StoreConfig rewrites them.

The freshly generated XML is always the source of truth for the content. Both the previous version and the freshly generated version are parsed, the elements of both documents are matched and the freshly generated document is re-serialized using the layout of the previous version.

The preservation is best-effort. Whenever the previous version is missing, cannot be parsed, or the result cannot be verified, the freshly generated XML is returned unchanged.

  • Method Details

    • preserve

      public static String preserve(File originalFile, String newXml, String encoding)
      Preserves the layout of the previous version of a configuration file for the freshly generated XML.
      Parameters:
      originalFile - The previous version of the configuration file, may be null
      newXml - The freshly generated XML
      encoding - The character encoding of the configuration file
      Returns:
      The freshly generated XML, re-laid-out using the previous version when possible, otherwise the freshly generated XML unchanged
    • preserve

      public static String preserve(String originalXml, String newXml, String encoding)
      Preserves the layout of the previous version of an XML document for the freshly generated XML.
      Parameters:
      originalXml - The previous version of the XML document, may be null
      newXml - The freshly generated XML
      encoding - The character encoding used in the XML declaration of the result
      Returns:
      The freshly generated XML, re-laid-out using the previous version when possible, otherwise the freshly generated XML unchanged