View Javadoc
1   package org.apache.maven.shared.utils.xml;
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.HashMap;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  
27  public class Xpp3DomUtils
28  {
29      public static Xpp3Dom mergeXpp3Dom( Xpp3Dom dominant, Xpp3Dom recessive, Boolean childMergeOverride )
30      {
31          return dominant != null ? merge( dominant, recessive, childMergeOverride ) : recessive;
32      }
33  
34      public static Xpp3Dom mergeXpp3Dom( Xpp3Dom dominant, Xpp3Dom recessive )
35      {
36          return dominant != null ? merge( dominant, recessive, null ) : recessive;
37      }
38  
39      public static Xpp3Dom merge( Xpp3Dom dominant, Xpp3Dom recessive, Boolean childMergeOverride )
40      {
41          if ( recessive == null || isCombineSelfOverride( dominant ) )
42          {
43              return dominant;
44          }
45  
46          if ( isEmpty( dominant.getValue() ) )
47          {
48              dominant.setValue( recessive.getValue() );
49          }
50  
51          for ( String attr : recessive.getAttributeNames() )
52          {
53              if ( isEmpty( dominant.getAttribute( attr ) ) )
54              {
55                  dominant.setAttribute( attr, recessive.getAttribute( attr ) );
56              }
57          }
58  
59          if ( recessive.getChildCount() > 0 )
60          {
61              boolean mergeChildren = isMergeChildren( dominant, childMergeOverride );
62  
63              if ( mergeChildren )
64              {
65                  Map<String, Iterator<Xpp3Dom>> commonChildren = getCommonChildren( dominant, recessive );
66                  for ( Xpp3Dom recessiveChild : recessive )
67                  {
68                      Iterator<Xpp3Dom> it = commonChildren.get( recessiveChild.getName() );
69                      if ( it == null )
70                      {
71                          dominant.addChild( new Xpp3Dom( recessiveChild ) );
72                      }
73                      else if ( it.hasNext() )
74                      {
75                          Xpp3Dom dominantChild = it.next();
76                          merge( dominantChild, recessiveChild, childMergeOverride );
77                      }
78                  }
79              }
80              else
81              {
82                  Xpp3Dom[] dominantChildren = dominant.getChildren();
83                  dominant.childList.clear();
84                  for ( Xpp3Dom child : recessive )
85                  {
86                      dominant.addChild( new Xpp3Dom( child ) );
87                  }
88  
89                  for ( Xpp3Dom aDominantChildren : dominantChildren )
90                  {
91                      dominant.addChild( aDominantChildren );
92                  }
93              }
94          }
95          return dominant;
96      }
97  
98      private static Map<String, Iterator<Xpp3Dom>> getCommonChildren( Xpp3Dom dominant, Xpp3Dom recessive )
99      {
100         Map<String, Iterator<Xpp3Dom>> commonChildren = new HashMap<String, Iterator<Xpp3Dom>>();
101 
102         for ( String childName : recessive.childMap.keySet() )
103         {
104             List<Xpp3Dom> dominantChildren = dominant.getChildrenList( childName );
105             if ( dominantChildren.size() > 0 )
106             {
107                 commonChildren.put( childName, dominantChildren.iterator() );
108             }
109         }
110         return commonChildren;
111     }
112 
113     private static boolean isCombineSelfOverride( Xpp3Dom xpp3Dom )
114     {
115         String selfMergeMode = xpp3Dom.getAttribute( Xpp3Dom.SELF_COMBINATION_MODE_ATTRIBUTE );
116         return Xpp3Dom.SELF_COMBINATION_OVERRIDE.equals( selfMergeMode );
117     }
118 
119     private static boolean isMergeChildren( Xpp3Dom dominant, Boolean override )
120     {
121         return override != null ? override : !isMergeChildren( dominant );
122     }
123 
124     private static boolean isMergeChildren( Xpp3Dom dominant )
125     {
126         return Xpp3Dom.CHILDREN_COMBINATION_APPEND.equals(
127             dominant.getAttribute( Xpp3Dom.CHILDREN_COMBINATION_MODE_ATTRIBUTE ) );
128     }
129 
130     public static boolean isEmpty( String str )
131     {
132         return str == null || str.trim().length() == 0;
133     }
134 
135 
136 
137 
138 }