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