1 package org.apache.maven.toolchain.merge;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.LinkedHashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.maven.toolchain.model.PersistedToolchains;
27 import org.apache.maven.toolchain.model.ToolchainModel;
28 import org.codehaus.plexus.util.xml.Xpp3Dom;
29
30
31
32
33
34
35 public class MavenToolchainMerger
36 {
37
38 public void merge( PersistedToolchains dominant, PersistedToolchains recessive, String recessiveSourceLevel )
39 {
40 if ( dominant == null || recessive == null )
41 {
42 return;
43 }
44
45 recessive.setSourceLevel( recessiveSourceLevel );
46
47 shallowMerge( dominant.getToolchains(), recessive.getToolchains(), recessiveSourceLevel );
48 }
49
50 private void shallowMerge( List<ToolchainModel> dominant, List<ToolchainModel> recessive,
51 String recessiveSourceLevel )
52 {
53 Map<Object, ToolchainModel> merged = new LinkedHashMap<>();
54
55 for ( ToolchainModel dominantModel : dominant )
56 {
57 Object key = getToolchainModelKey( dominantModel );
58
59 merged.put( key, dominantModel );
60 }
61
62 for ( ToolchainModel recessiveModel : recessive )
63 {
64 Object key = getToolchainModelKey( recessiveModel );
65
66 ToolchainModel dominantModel = merged.get( key );
67 if ( dominantModel == null )
68 {
69 recessiveModel.setSourceLevel( recessiveSourceLevel );
70 dominant.add( recessiveModel );
71 }
72 else
73 {
74 mergeToolchainModel_Configuration( dominantModel, recessiveModel );
75 }
76 }
77 }
78
79 protected void mergeToolchainModel_Configuration( ToolchainModel target,
80 ToolchainModel source )
81 {
82 Xpp3Dom src = (Xpp3Dom) source.getConfiguration();
83 if ( src != null )
84 {
85 Xpp3Dom tgt = (Xpp3Dom) target.getConfiguration();
86 if ( tgt == null )
87 {
88 tgt = Xpp3Dom.mergeXpp3Dom( new Xpp3Dom( src ), tgt );
89 }
90 else
91 {
92 tgt = Xpp3Dom.mergeXpp3Dom( tgt, src );
93 }
94 target.setConfiguration( tgt );
95 }
96 }
97
98 protected Object getToolchainModelKey( ToolchainModel model )
99 {
100 return model;
101 }
102
103 }