View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author Robert Scholte
32   * @since 3.2.4
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  }