Skip to content

Commit

Permalink
[MNG-7596] Upgrade to plexus 3.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Nov 10, 2022
1 parent fa15fcf commit df6ffaf
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 36 deletions.
11 changes: 11 additions & 0 deletions api/maven-api-xml/src/main/java/org/apache/maven/api/xml/Dom.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ public interface Dom

String SELF_COMBINATION_REMOVE = "remove";

/**
* In case of complex XML structures, combining can be done based on id.
*/
String ID_COMBINATION_MODE_ATTRIBUTE = "combine.id";

/**
* In case of complex XML structures, combining can be done based on keys.
* This is a comma separated list of attribute names.
*/
String KEYS_COMBINATION_MODE_ATTRIBUTE = "combine.keys";

/**
* 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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.ListIterator;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -211,8 +212,6 @@ public void writeToSerializer( String namespace, XmlSerializer serializer )
* </ol></li>
* <li> If mergeSelf == true
* <ol type="A">
* <li> if the dominant root node's value is empty, set it to the recessive root node's value</li>
* <li> For each attribute in the recessive root node which is not set in the dominant root node, set it.</li>
* <li> Determine whether children from the recessive DOM will be merged or appended to the dominant DOM as
* siblings (flag=mergeChildren).
* <ol type="i">
Expand Down Expand Up @@ -258,17 +257,11 @@ public static Dom merge( Dom dominant, Dom recessive, Boolean childMergeOverride
if ( mergeSelf )
{

String value = null;
Object location = null;
String value = dominant.getValue();
Object location = dominant.getInputLocation();
Map<String, String> attrs = null;
List<Dom> children = null;

if ( isEmpty( dominant.getValue() ) && !isEmpty( recessive.getValue() ) )
{
value = recessive.getValue();
location = recessive.getInputLocation();
}

for ( Map.Entry<String, String> attr : recessive.getAttributes().entrySet() )
{
String key = attr.getKey();
Expand Down Expand Up @@ -298,35 +291,72 @@ public static Dom merge( Dom dominant, Dom recessive, Boolean childMergeOverride
}
}

if ( !mergeChildren )
{
children = new ArrayList<>( recessive.getChildren().size() + dominant.getChildren().size() );
children.addAll( recessive.getChildren() );
children.addAll( dominant.getChildren() );
}
else
String keysValue = recessive.getAttribute( KEYS_COMBINATION_MODE_ATTRIBUTE );

for ( Dom recessiveChild : recessive.getChildren() )
{
Map<String, Iterator<Dom>> commonChildren = new HashMap<>();
Set<String> names = recessive.getChildren().stream()
.map( Dom::getName ).collect( Collectors.toSet() );
for ( String name : names )
String idValue = recessiveChild.getAttribute( ID_COMBINATION_MODE_ATTRIBUTE );

Dom childDom = null;
if ( isNotEmpty( idValue ) )
{
List<Dom> dominantChildren = dominant.getChildren().stream()
.filter( n -> n.getName().equals( name ) )
.collect( Collectors.toList() );
if ( dominantChildren.size() > 0 )
for ( Dom dominantChild : dominant.getChildren() )
{
commonChildren.put( name, dominantChildren.iterator() );
if ( idValue.equals( dominantChild.getAttribute( ID_COMBINATION_MODE_ATTRIBUTE ) ) )
{
childDom = dominantChild;
// we have a match, so don't append but merge
mergeChildren = true;
}
}
}
else if ( isNotEmpty( keysValue ) )
{
String[] keys = keysValue.split( "," );
Map<String, Optional<String>> recessiveKeyValues = Stream.of( keys )
.collect( Collectors.toMap(
k -> k, k -> Optional.ofNullable( recessiveChild.getAttribute( k ) ) ) );

for ( Dom recessiveChild : recessive.getChildren() )
for ( Dom dominantChild : dominant.getChildren() )
{
Map<String, Optional<String>> dominantKeyValues = Stream.of( keys )
.collect( Collectors.toMap(
k -> k, k -> Optional.ofNullable( dominantChild.getAttribute( k ) ) ) );

if ( recessiveKeyValues.equals( dominantKeyValues ) )
{
childDom = dominantChild;
// we have a match, so don't append but merge
mergeChildren = true;
}
}
}
else
{
childDom = dominant.getChild( recessiveChild.getName() );
}

if ( mergeChildren && childDom != null )
{
Map<String, Iterator<Dom>> commonChildren = new HashMap<>();
Set<String> names = recessive.getChildren().stream()
.map( Dom::getName ).collect( Collectors.toSet() );
for ( String name : names )
{
List<Dom> dominantChildren = dominant.getChildren().stream()
.filter( n -> n.getName().equals( name ) )
.collect( Collectors.toList() );
if ( dominantChildren.size() > 0 )
{
commonChildren.put( name, dominantChildren.iterator() );
}
}

String name = recessiveChild.getName();
Iterator<Dom> it = commonChildren.computeIfAbsent( name,
n1 -> Stream.of( dominant.getChildren().stream()
.filter( n2 -> n2.getName().equals( n1 ) )
.collect( Collectors.toList() ) )
.filter( n2 -> n2.getName().equals( n1 ) )
.collect( Collectors.toList() ) )
.filter( l -> !l.isEmpty() )
.map( List::iterator )
.findFirst()
Expand Down Expand Up @@ -355,8 +385,7 @@ else if ( it.hasNext() )
}
else
{
int idx = ( children != null ? children : dominant.getChildren() )
.indexOf( dominantChild );
int idx = dominant.getChildren().indexOf( dominantChild );
Dom merged = merge( dominantChild, recessiveChild, childMergeOverride );
if ( merged != dominantChild )
{
Expand All @@ -369,6 +398,15 @@ else if ( it.hasNext() )
}
}
}
else
{
if ( children == null )
{
children = new ArrayList<>( dominant.getChildren() );
}
int idx = mergeChildren ? children.size() : recessive.getChildren().indexOf( recessiveChild );
children.add( idx, recessiveChild );
}
}
}

Expand Down Expand Up @@ -453,14 +491,14 @@ public String toUnescapedString()
return writer.toString();
}

public static boolean isNotEmpty( String str )
private static boolean isNotEmpty( String str )
{
return ( ( str != null ) && ( str.length() > 0 ) );
}

public static boolean isEmpty( String str )
private static boolean isEmpty( String str )
{
return ( ( str == null ) || ( str.trim().length() == 0 ) );
return ( ( str == null ) || ( str.length() == 0 ) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,12 @@ public static Xpp3Dom build( XmlPullParser parser, boolean trim, InputLocationBu
Map<String, String> attrs = null;
List<Dom> children = null;
int eventType = parser.getEventType();
boolean emptyTag = false;
while ( eventType != XmlPullParser.END_DOCUMENT )
{
if ( eventType == XmlPullParser.START_TAG )
{
emptyTag = parser.isEmptyElementTag();
if ( name == null )
{
name = parser.getName();
Expand Down Expand Up @@ -195,7 +197,8 @@ else if ( eventType == XmlPullParser.TEXT )
}
else if ( eventType == XmlPullParser.END_TAG )
{
return new Xpp3Dom( name, children == null ? value : null,
return new Xpp3Dom( name,
children == null ? ( value != null ? value : emptyTag ? null : "" ) : null,
attrs, children, location );
}
eventType = parser.next();
Expand Down
Loading

0 comments on commit df6ffaf

Please sign in to comment.