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