Skip to content

Commit

Permalink
codehaus-plexus#74 Support combine.self="remove"
Browse files Browse the repository at this point in the history
  • Loading branch information
belingueres committed Dec 6, 2019
1 parent ecdbeeb commit 3ee6703
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/main/java/org/codehaus/plexus/util/xml/Xpp3Dom.java
Expand Up @@ -76,6 +76,8 @@ public class Xpp3Dom

public static final String SELF_COMBINATION_MERGE = "merge";

public static final String SELF_COMBINATION_REMOVE = "remove";

/**
* This default mode for combining a DOM node during merge means that where element names match, the process will
* try to merge the element attributes and values, rather than overriding the recessive element completely with the
Expand Down Expand Up @@ -301,6 +303,13 @@ public void removeChild( int i )
child.setParent( null );
}

public void removeChild( Xpp3Dom child )
{
childList.remove( child );
// In case of any dangling references
child.setParent( null );
}

// ----------------------------------------------------------------------
// Parent handling
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -489,7 +498,17 @@ private static void mergeIntoXpp3Dom( Xpp3Dom dominant, Xpp3Dom recessive, Boole
else if ( it.hasNext() )
{
Xpp3Dom dominantChild = it.next();
mergeIntoXpp3Dom( dominantChild, recessiveChild, childMergeOverride );

String dominantChildCombinationMode =
dominantChild.getAttribute( SELF_COMBINATION_MODE_ATTRIBUTE );
if ( SELF_COMBINATION_REMOVE.equals( dominantChildCombinationMode ) )
{
dominant.removeChild( dominantChild );
}
else
{
mergeIntoXpp3Dom( dominantChild, recessiveChild, childMergeOverride );
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/codehaus/plexus/util/xml/Xpp3DomTest.java
Expand Up @@ -329,6 +329,21 @@ public void testDupeChildren()
assertEquals( "y", dom.getChild( "foo" ).getValue() );
}

@Test
public void testShouldRemoveEntireElementWithAttributesAndChildren()
throws Exception
{
String dominantStr = "<config><service combine.self=\"remove\"/></config>";
String recessiveStr = "<config><service><parameter>parameter</parameter></service></config>";
Xpp3Dom dominantConfig = Xpp3DomBuilder.build( new StringReader( dominantStr ) );
Xpp3Dom recessiveConfig = Xpp3DomBuilder.build( new StringReader( recessiveStr ) );

Xpp3Dom result = Xpp3Dom.mergeXpp3Dom( dominantConfig, recessiveConfig );

assertEquals( 0, result.getChildCount() );
assertEquals( "config", result.getName() );
}

private static class FixedInputLocationBuilder
implements Xpp3DomBuilder.InputLocationBuilder
{
Expand Down

0 comments on commit 3ee6703

Please sign in to comment.