1 package org.codehaus.plexus.metadata.merge.support;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import java.util.ArrayList;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Set;
31
32 import org.codehaus.plexus.metadata.merge.MergeException;
33 import org.codehaus.plexus.metadata.merge.MergeStrategy;
34 import org.jdom2.Content;
35 import org.jdom2.Element;
36
37
38
39
40 public abstract class AbstractMergeableElement extends AbstractMergeableSupport {
41 public AbstractMergeableElement(Element element) {
42 super(element);
43 }
44
45
46
47
48
49
50
51
52
53
54
55
56 protected boolean isRecessiveElementInConflict(AbstractMergeableElement re, String eltName) {
57
58 List l = new ArrayList();
59 l.add(eltName);
60 return isRecessiveElementInConflict(re, l);
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 protected boolean isRecessiveElementInConflict(AbstractMergeableElement re, List eltNameList) {
77
78
79 eltNameList = getElementNamesForConflictResolution(eltNameList);
80
81 if (null == eltNameList || eltNameList.size() == 0) {
82 return false;
83 }
84
85
86 for (Object anEltNameList : eltNameList) {
87 String eltName = (String) anEltNameList;
88 String dEltValue = getChildTextTrim(eltName);
89 String rEltValue = re.getChildTextTrim(eltName);
90 if (null == dEltValue || null == rEltValue || !dEltValue.equals(rEltValue)) {
91 return false;
92 }
93 }
94 return true;
95 }
96
97
98
99
100
101
102
103
104
105 protected boolean mergeableElementComesFromRecessive(AbstractMergeableElement re, String eltName) {
106 return null == getChildText(eltName) && null != re.getChildText(eltName);
107 }
108
109
110
111
112
113
114 public void merge(Mergeable me, MergeStrategy strategy) throws MergeException {
115
116 strategy.apply(this, me);
117 }
118
119 public void merge(Mergeable me) throws MergeException {
120 if (!isExpectedElementType(me)) {
121
122
123
124 throw new MergeException("Cannot Merge dissimilar elements. " + "(Expected : '"
125 + getClass().getName() + "', found '" + me.getClass().getName() + "')");
126 }
127
128 AbstractMergeableElement rce = (AbstractMergeableElement) me;
129
130 Set allowedTags = new HashSet();
131
132 for (int i = 0; i < getAllowedTags().length; i++) {
133 String tagName = getAllowedTags()[i].getTagName();
134
135 allowedTags.add(tagName);
136
137 List defaultConflictChecklist = new ArrayList();
138 defaultConflictChecklist.add(tagName);
139
140 if (!isRecessiveElementInConflict(rce, defaultConflictChecklist)
141 && mergeableElementComesFromRecessive(rce, tagName)) {
142 this.addContent((Element) rce.getChild(tagName).clone());
143
144 } else if (getAllowedTags()[i].isMergeable()
145 && isRecessiveElementInConflict(rce, defaultConflictChecklist)) {
146
147 try {
148 getAllowedTags()[i]
149 .createMergeable(this.getChild(tagName))
150 .merge(
151 getAllowedTags()[i].createMergeable(rce.getChild(tagName)),
152 getDefaultMergeStrategy());
153 } catch (Exception e) {
154
155 throw new MergeException(
156 "Unable to create Mergeable instance for tag " + "'" + getAllowedTags()[i] + "'.", e);
157 }
158 }
159 }
160
161 for (Object o : me.getElement().getChildren()) {
162 Element child = (Element) o;
163
164 if (!allowedTags.contains(child.getName())) {
165
166 element.addContent((Content) child.clone());
167 }
168 }
169 }
170 }