Class DomUtils

java.lang.Object
org.apache.maven.cling.invoker.mvnup.goals.DomUtils

public class DomUtils extends Object
Utility class for XML operations on Maven POM files.

This class provides convenience methods that delegate to:

  • PomEditor - DomTrip's PomEditor
  • Element - DomTrip's Element API

These methods are kept for convenience and backward compatibility. For more advanced operations, consider using ExtendedPomEditor or DomTrip directly.

Using DomTrip Directly

Many operations can be performed directly using DomTrip's Element API:

// Find child element
Element child = parent.child("version").orElse(null);

// Check if child exists
boolean hasVersion = parent.child("version").isPresent();

// Get child text content
String version = parent.child("version")
    .map(Element::textContent)
    .orElse(null);

// Get trimmed text content
String trimmedVersion = parent.child("version")
    .map(Element::textContentTrimmed)
    .orElse(null);

// Set text content (fluent API)
element.textContent("4.0.0");

When to Use DomUtils

Use DomUtils methods when you need:

  • Maven-specific element ordering (insertNewElement, insertContentElement)
  • High-level helpers (addGAVElements, createDependency, createPlugin)
  • Null-safe operations (updateElementContent, removeElement)
  • Update-or-create patterns (updateOrCreateChildElement)
See Also:
  • Element
  • Editor
  • PomEditor
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addGAVElements(eu.maveniverse.domtrip.Element parent, String groupId, String artifactId, String version)
    Convenience method to add GAV (groupId, artifactId, version) elements to a parent.
    static eu.maveniverse.domtrip.Element
    createDependency(eu.maveniverse.domtrip.Element dependenciesElement, String groupId, String artifactId, String version)
    Convenience method to create a dependency element with GAV.
    static eu.maveniverse.domtrip.Element
    createPlugin(eu.maveniverse.domtrip.Element pluginsElement, String groupId, String artifactId, String version)
    Convenience method to create a plugin element with GAV.
    static eu.maveniverse.domtrip.Element
    findChildElement(eu.maveniverse.domtrip.Element parent, String name)
    Finds a child element by name under the specified parent.
    static eu.maveniverse.domtrip.Element
    insertContentElement(eu.maveniverse.domtrip.Element parent, String name, String content)
    Inserts a new content element with the given name and text content.
    static eu.maveniverse.domtrip.Element
    insertNewElement(String name, eu.maveniverse.domtrip.Element parent)
    Inserts a new child element to the given parent element with proper Maven POM ordering.
    static void
    removeElement(eu.maveniverse.domtrip.Element element)
    Removes an element from its parent.
    static String
    toXml(eu.maveniverse.domtrip.Document document)
    Serializes a domtrip Document to XML string with preserved formatting.
    static eu.maveniverse.domtrip.Element
    updateOrCreateChildElement(eu.maveniverse.domtrip.Element parent, String childName, String content)
    Updates or creates a child element with the given content.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • insertNewElement

      public static eu.maveniverse.domtrip.Element insertNewElement(String name, eu.maveniverse.domtrip.Element parent)
      Inserts a new child element to the given parent element with proper Maven POM ordering.
      Parameters:
      name - the name of the new element
      parent - the parent element
      Returns:
      the new element
    • insertContentElement

      public static eu.maveniverse.domtrip.Element insertContentElement(eu.maveniverse.domtrip.Element parent, String name, String content)
      Inserts a new content element with the given name and text content.
      Parameters:
      parent - the parent element
      name - the name of the new element
      content - the text content
      Returns:
      the new element
    • findChildElement

      public static eu.maveniverse.domtrip.Element findChildElement(eu.maveniverse.domtrip.Element parent, String name)
      Finds a child element by name under the specified parent.
      Parameters:
      parent - the parent element
      name - the child element name to find
      Returns:
      the child element if found, null otherwise
    • toXml

      public static String toXml(eu.maveniverse.domtrip.Document document)
      Serializes a domtrip Document to XML string with preserved formatting.
      Parameters:
      document - the domtrip Document
      Returns:
      the XML string with preserved formatting
    • removeElement

      public static void removeElement(eu.maveniverse.domtrip.Element element)
      Removes an element from its parent.
      Parameters:
      element - the element to remove
    • addGAVElements

      public static void addGAVElements(eu.maveniverse.domtrip.Element parent, String groupId, String artifactId, String version)
      Convenience method to add GAV (groupId, artifactId, version) elements to a parent.
      Parameters:
      parent - the parent element (e.g., dependency or plugin)
      groupId - the groupId value
      artifactId - the artifactId value
      version - the version value (can be null to skip)
    • createDependency

      public static eu.maveniverse.domtrip.Element createDependency(eu.maveniverse.domtrip.Element dependenciesElement, String groupId, String artifactId, String version)
      Convenience method to create a dependency element with GAV.
      Parameters:
      dependenciesElement - the dependencies parent element
      groupId - the groupId value
      artifactId - the artifactId value
      version - the version value (can be null)
      Returns:
      the created dependency element
    • createPlugin

      public static eu.maveniverse.domtrip.Element createPlugin(eu.maveniverse.domtrip.Element pluginsElement, String groupId, String artifactId, String version)
      Convenience method to create a plugin element with GAV.
      Parameters:
      pluginsElement - the plugins parent element
      groupId - the groupId value
      artifactId - the artifactId value
      version - the version value (can be null)
      Returns:
      the created plugin element
    • updateOrCreateChildElement

      public static eu.maveniverse.domtrip.Element updateOrCreateChildElement(eu.maveniverse.domtrip.Element parent, String childName, String content)
      Updates or creates a child element with the given content.
      Parameters:
      parent - the parent element
      childName - the child element name
      content - the content to set
      Returns:
      the updated or created element