Skip to content
210 changes: 62 additions & 148 deletions api/maven-api-xml/src/main/java/org/apache/maven/api/xml/XmlNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,54 +135,19 @@ public interface XmlNode {
String DEFAULT_SELF_COMBINATION_MODE = XmlService.DEFAULT_SELF_COMBINATION_MODE;

/**
* Returns the local name of this XML node.
*
* @return the node name, never {@code null}
*/
@Nonnull
String name();

/**
* Returns the namespace URI of this XML node.
*
* @return the namespace URI, never {@code null} (empty string if no namespace)
*/
@Nonnull
String namespaceUri();

/**
* Returns the namespace prefix of this XML node.
*
* @return the namespace prefix, never {@code null} (empty string if no prefix)
*/
@Nonnull
String prefix();

/**
* Returns the text content of this XML node.
* Returns the input location information for this node, if available.
* This can be useful for error reporting and debugging.
*
* @return the node's text value, or {@code null} if none exists
* @return the input location object, or {@code null} if not available
*/
@Nullable
String value();
Object getInputLocation();

/**
* Returns an immutable map of all attributes defined on this XML node.
*
* @return map of attribute names to values, never {@code null}
*/
@Nonnull
Map<String, String> attributes();
String getName();

/**
* Returns the value of a specific attribute.
*
* @param name the name of the attribute to retrieve
* @return the attribute value, or {@code null} if the attribute doesn't exist
* @throws NullPointerException if name is null
*/
@Nullable
String attribute(@Nonnull String name);
@Nonnull
String getNamespaceUri();

/**
* Returns the namespace context for this node — a map of namespace prefix to URI
Expand All @@ -195,119 +160,28 @@ public interface XmlNode {
* the {@code mvn → http://maven.apache.org/POM/4.0.0} binding.
*
* @return map of namespace prefix to URI, never {@code null}
* @since 4.1.0
*/
@Nonnull
default Map<String, String> namespaces() {
return Map.of();
}

/**
* Returns an immutable list of all child nodes.
*
* @return list of child nodes, never {@code null}
*/
@Nonnull
List<XmlNode> children();

/**
* Returns the first child node with the specified name.
*
* @param name the name of the child node to find
* @return the first matching child node, or {@code null} if none found
*/
@Nullable
XmlNode child(String name);

/**
* Returns the input location information for this node, if available.
* This can be useful for error reporting and debugging.
*
* @return the input location object, or {@code null} if not available
*/
@Nullable
Object inputLocation();

/**
* @deprecated Use {@link #name()} instead.
*/
@Deprecated(since = "4.0.0", forRemoval = true)
@Nonnull
default String getName() {
return name();
}

/**
* @deprecated Use {@link #namespaceUri()} instead.
* @since 4.0.0
*/
@Deprecated(since = "4.0.0", forRemoval = true)
@Nonnull
default String getNamespaceUri() {
return namespaceUri();
}
Map<String, String> getNamespaces();

/**
* @deprecated Use {@link #prefix()} instead.
*/
@Deprecated(since = "4.0.0", forRemoval = true)
@Nonnull
default String getPrefix() {
return prefix();
}
String getPrefix();

/**
* @deprecated Use {@link #value()} instead.
*/
@Deprecated(since = "4.0.0", forRemoval = true)
@Nullable
default String getValue() {
return value();
}
String getValue();

/**
* @deprecated Use {@link #attributes()} instead.
*/
@Deprecated(since = "4.0.0", forRemoval = true)
@Nonnull
default Map<String, String> getAttributes() {
return attributes();
}
Map<String, String> getAttributes();

/**
* @deprecated Use {@link #attribute(String)} instead.
*/
@Deprecated(since = "4.0.0", forRemoval = true)
@Nullable
default String getAttribute(@Nonnull String name) {
return attribute(name);
}
String getAttribute(@Nonnull String name);

/**
* @deprecated Use {@link #children()} instead.
*/
@Deprecated(since = "4.0.0", forRemoval = true)
@Nonnull
default List<XmlNode> getChildren() {
return children();
}

/**
* @deprecated Use {@link #child(String)} instead.
*/
@Deprecated(since = "4.0.0", forRemoval = true)
@Nullable
default XmlNode getChild(String name) {
return child(name);
}
List<XmlNode> getChildren();

/**
* @deprecated Use {@link #inputLocation()} instead.
*/
@Deprecated(since = "4.0.0", forRemoval = true)
@Nullable
default Object getInputLocation() {
return inputLocation();
}
XmlNode getChild(String name);

/**
* @deprecated Use {@link XmlService#merge(XmlNode, XmlNode, Boolean)} instead.
Expand Down Expand Up @@ -565,32 +439,72 @@ private record Impl(
}

@Override
public String attribute(@Nonnull String name) {
public String getName() {
return name();
}

@Override
public String getNamespaceUri() {
return namespaceUri();
}

@Override
public String getPrefix() {
return prefix();
}

@Override
public Map<String, String> getNamespaces() {
return namespaces();
}

@Override
public String getValue() {
return value();
}

@Override
public Map<String, String> getAttributes() {
return attributes();
}

@Override
public String getAttribute(@Nonnull String name) {
return attributes.get(name);
}

@Override
public XmlNode child(String name) {
public List<XmlNode> getChildren() {
return children();
}

@Override
public XmlNode getChild(String name) {
if (name != null) {
ListIterator<XmlNode> it = children.listIterator(children.size());
while (it.hasPrevious()) {
XmlNode child = it.previous();
if (name.equals(child.name())) {
if (name.equals(child.getName())) {
return child;
}
}
}
return null;
}

@Override
public Object getInputLocation() {
return inputLocation();
}

@Override
public boolean equals(Object o) {
return this == o
|| o instanceof XmlNode that
&& Objects.equals(this.name, that.name())
&& Objects.equals(this.value, that.value())
&& Objects.equals(this.attributes, that.attributes())
&& Objects.equals(this.children, that.children());
&& Objects.equals(this.name, that.getName())
&& Objects.equals(this.value, that.getValue())
&& Objects.equals(this.attributes, that.getAttributes())
&& Objects.equals(this.children, that.getChildren());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class PlexusXmlBeanConverterTest {
void convertsXmlNode() {
XmlNode node = assertInstanceOf(XmlNode.class, newConverter().convert(TypeLiteral.get(XmlNode.class), XML));

assertEquals("configuration", node.name());
assertEquals("strict", node.attribute("mode"));
assertEquals("text", node.child("value").value());
assertNull(node.child("empty").value());
assertEquals("configuration", node.getName());
assertEquals("strict", node.getAttribute("mode"));
assertEquals("text", node.getChild("value").getValue());
assertNull(node.getChild("empty").getValue());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ public void testRecursiveExpressionCycleInPluginConfiguration() throws Exception
assertNotNull(out);
assertCollectorState(0, 1, 0, collector);
org.apache.maven.api.model.Plugin p = out.getBuild().getPlugins().get(0).getDelegate();
assertEquals("${prop}", p.getConfiguration().child("filter").attribute("value"));
assertEquals("${prop}", p.getConfiguration().getChild("filter").getAttribute("value"));
}

protected abstract ModelInterpolator createInterpolator() throws Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ void testNamespaceInXmlNode() throws XMLStreamException {
Plugin plugin = model.getBuild().getPlugins().get(0);
XmlNode node = plugin.getConfiguration();
assertNotNull(node);
assertEquals("http://maven.apache.org/POM/4.0.0", node.namespaceUri());
assertEquals("m", node.prefix());
assertEquals("configuration", node.name());
assertEquals(1, node.children().size());
XmlNode myConfig = node.children().get(0);
assertEquals("http://fabric8.io/fabric8-maven-plugin", myConfig.namespaceUri());
assertEquals("", myConfig.prefix());
assertEquals("myConfig", myConfig.name());
assertEquals("http://maven.apache.org/POM/4.0.0", node.getNamespaceUri());
assertEquals("m", node.getPrefix());
assertEquals("configuration", node.getName());
assertEquals(1, node.getChildren().size());
XmlNode myConfig = node.getChildren().get(0);
assertEquals("http://fabric8.io/fabric8-maven-plugin", myConfig.getNamespaceUri());
assertEquals("", myConfig.getPrefix());
assertEquals("myConfig", myConfig.getName());
String config = node.toString();
assertFalse(config.isEmpty(), "Expected collection to not be empty but was empty");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ private PluginInfo extractPluginInfo(Artifact artifact) {
}

private static String mayGetChild(XmlNode node, String child) {
XmlNode c = node.child(child);
XmlNode c = node.getChild(child);
if (c != null) {
return c.value();
return c.getValue();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ public SessionBuilder newRepositorySessionBuilder(MavenExecutionRequest request)

if (server.getConfiguration() != null) {
XmlNode dom = server.getDelegate().getConfiguration();
List<XmlNode> children = dom.children().stream()
.filter(c -> !"wagonProvider".equals(c.name()))
List<XmlNode> children = dom.getChildren().stream()
.filter(c -> !"wagonProvider".equals(c.getName()))
.collect(Collectors.toList());
dom = XmlNode.newInstance(dom.name(), children);
dom = XmlNode.newInstance(dom.getName(), children);
PlexusConfiguration config = XmlPlexusConfiguration.toPlexusConfiguration(dom);
configProps.put("aether.transport.wagon.config." + server.getId(), config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,13 @@ private void finalizeMojoConfiguration(MojoExecution mojoExecution) {
List<XmlNode> children = new ArrayList<>();
if (mojoDescriptor.getParameters() != null) {
for (Parameter parameter : mojoDescriptor.getParameters()) {
XmlNode parameterConfiguration = executionConfiguration.child(parameter.getName());
XmlNode parameterConfiguration = executionConfiguration.getChild(parameter.getName());

if (parameterConfiguration == null) {
parameterConfiguration = executionConfiguration.child(parameter.getAlias());
parameterConfiguration = executionConfiguration.getChild(parameter.getAlias());
}

XmlNode parameterDefaults = defaultConfiguration.child(parameter.getName());
XmlNode parameterDefaults = defaultConfiguration.getChild(parameter.getName());

if (parameterConfiguration != null) {
parameterConfiguration = XmlService.merge(parameterConfiguration, parameterDefaults, Boolean.TRUE);
Expand All @@ -310,9 +310,9 @@ private void finalizeMojoConfiguration(MojoExecution mojoExecution) {
}

if (parameterConfiguration != null) {
Map<String, String> attributes = new HashMap<>(parameterConfiguration.attributes());
Map<String, String> attributes = new HashMap<>(parameterConfiguration.getAttributes());

String attributeForImplementation = parameterConfiguration.attribute("implementation");
String attributeForImplementation = parameterConfiguration.getAttribute("implementation");
String parameterForImplementation = parameter.getImplementation();
if ((attributeForImplementation == null || attributeForImplementation.isEmpty())
&& ((parameterForImplementation != null) && !parameterForImplementation.isEmpty())) {
Expand All @@ -321,10 +321,10 @@ private void finalizeMojoConfiguration(MojoExecution mojoExecution) {

parameterConfiguration = XmlNode.newInstance(
parameter.getName(),
parameterConfiguration.value(),
parameterConfiguration.getValue(),
attributes,
parameterConfiguration.children(),
parameterConfiguration.inputLocation());
parameterConfiguration.getChildren(),
parameterConfiguration.getInputLocation());

children.add(parameterConfiguration);
}
Expand Down
Loading
Loading