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