1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.internal.merge; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.io.StringReader; |
24 |
|
import java.util.ArrayList; |
25 |
|
import java.util.Collections; |
26 |
|
import java.util.List; |
27 |
|
|
28 |
|
import org.apache.commons.io.IOUtils; |
29 |
|
import org.apache.commons.lang3.ObjectUtils; |
30 |
|
import org.apache.commons.lang3.StringUtils; |
31 |
|
import org.xwiki.diff.DiffManager; |
32 |
|
import org.xwiki.diff.MergeException; |
33 |
|
|
34 |
|
import com.xpn.xwiki.doc.merge.MergeResult; |
35 |
|
import com.xpn.xwiki.web.Utils; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
@version |
41 |
|
@since |
42 |
|
|
|
|
| 62% |
Uncovered Elements: 30 (79) |
Complexity: 23 |
Complexity Density: 0.43 |
|
43 |
|
public final class MergeUtils |
44 |
|
{ |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
private static final DiffManager DIFFMANAGER = Utils.getComponent(DiffManager.class); |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
53 |
0 |
private MergeUtils()... |
54 |
|
{ |
55 |
|
|
56 |
|
} |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
@param |
62 |
|
@param |
63 |
|
@param |
64 |
|
@param |
65 |
|
@return |
66 |
|
|
|
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
67 |
166 |
public static String mergeLines(String previousStr, String newStr, String currentStr, MergeResult mergeResult)... |
68 |
|
{ |
69 |
166 |
org.xwiki.diff.MergeResult<String> result; |
70 |
166 |
try { |
71 |
166 |
result = DIFFMANAGER.merge(toLines(previousStr), toLines(newStr), toLines(currentStr), null); |
72 |
|
|
73 |
166 |
mergeResult.getLog().addAll(result.getLog()); |
74 |
|
|
75 |
166 |
String resultStr = fromLines(result.getMerged()); |
76 |
|
|
77 |
166 |
if (!StringUtils.equals(resultStr, currentStr)) { |
78 |
25 |
mergeResult.setModified(true); |
79 |
|
} |
80 |
|
|
81 |
166 |
return resultStr; |
82 |
|
} catch (MergeException e) { |
83 |
0 |
mergeResult.getLog().error("Failed to execute merge lines", e); |
84 |
|
} |
85 |
|
|
86 |
0 |
return currentStr; |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
@param |
93 |
|
@param |
94 |
|
@param |
95 |
|
@param |
96 |
|
@param |
97 |
|
@return |
98 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
99 |
652 |
public static <T> T mergeOject(T previousObject, T newObject, T currentObject, MergeResult mergeResult)... |
100 |
|
{ |
101 |
652 |
if (ObjectUtils.notEqual(previousObject, newObject)) { |
102 |
35 |
if (ObjectUtils.equals(previousObject, currentObject)) { |
103 |
32 |
mergeResult.setModified(true); |
104 |
32 |
return newObject; |
105 |
3 |
} else if (ObjectUtils.equals(newObject, currentObject)) { |
106 |
1 |
return currentObject; |
107 |
|
} |
108 |
|
|
109 |
2 |
mergeResult.getLog().error("Failed to merge objects: previous=[{}] new=[{}] current=[{}]", previousObject, |
110 |
|
newObject, currentObject); |
111 |
|
} |
112 |
|
|
113 |
619 |
return currentObject; |
114 |
|
} |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
@param |
120 |
|
@param |
121 |
|
@param |
122 |
|
@param |
123 |
|
@return |
124 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
125 |
0 |
public static String mergeCharacters(String previousStr, String newStr, String currentStr, MergeResult mergeResult)... |
126 |
|
{ |
127 |
0 |
org.xwiki.diff.MergeResult<Character> result; |
128 |
0 |
try { |
129 |
0 |
result = DIFFMANAGER.merge(toCharacters(previousStr), toCharacters(newStr), toCharacters(currentStr), null); |
130 |
|
|
131 |
0 |
mergeResult.getLog().addAll(result.getLog()); |
132 |
|
|
133 |
0 |
String resultStr = fromCharacters(result.getMerged()); |
134 |
|
|
135 |
0 |
if (!StringUtils.equals(resultStr, currentStr)) { |
136 |
0 |
mergeResult.setModified(true); |
137 |
|
} |
138 |
|
|
139 |
0 |
return resultStr; |
140 |
|
} catch (MergeException e) { |
141 |
0 |
mergeResult.getLog().error("Failed to execute merge characters", e); |
142 |
|
} |
143 |
|
|
144 |
0 |
return currentStr; |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
|
@link |
149 |
|
|
150 |
|
@param |
151 |
|
@param |
152 |
|
@param |
153 |
|
@param |
154 |
|
@param |
155 |
|
|
|
|
| 91.7% |
Uncovered Elements: 1 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
156 |
3 |
public static <T> void mergeList(List<T> commonAncestor, List<T> next, List<T> current, MergeResult mergeResult)... |
157 |
|
{ |
158 |
3 |
org.xwiki.diff.MergeResult<T> result; |
159 |
3 |
try { |
160 |
3 |
result = DIFFMANAGER.merge(commonAncestor, next, current, null); |
161 |
|
|
162 |
3 |
mergeResult.getLog().addAll(result.getLog()); |
163 |
|
|
164 |
3 |
List<T> merged = result.getMerged(); |
165 |
|
|
166 |
3 |
if (!ObjectUtils.equals(merged, current)) { |
167 |
1 |
current.clear(); |
168 |
1 |
current.addAll(result.getMerged()); |
169 |
1 |
mergeResult.setModified(true); |
170 |
|
} |
171 |
|
} catch (MergeException e) { |
172 |
0 |
mergeResult.getLog().error("Failed to execute merge lists", e); |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
|
@param |
178 |
|
@return |
179 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
180 |
166 |
private static String fromLines(List<String> lines)... |
181 |
|
{ |
182 |
166 |
return StringUtils.join(lines, '\n'); |
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
|
@param |
187 |
|
@return |
188 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 5 |
Complexity Density: 0.71 |
|
189 |
498 |
private static List<String> toLines(String str)... |
190 |
|
{ |
191 |
498 |
List<String> result; |
192 |
498 |
try { |
193 |
498 |
result = IOUtils.readLines(new StringReader(str)); |
194 |
|
|
195 |
|
|
196 |
498 |
if (str.endsWith("\n") || str.endsWith("\r") || str.endsWith("\r\n")) { |
197 |
4 |
result.add(""); |
198 |
|
} |
199 |
|
|
200 |
|
} catch (IOException e) { |
201 |
|
|
202 |
0 |
result = null; |
203 |
|
} |
204 |
498 |
return result; |
205 |
|
} |
206 |
|
|
207 |
|
|
208 |
|
@param |
209 |
|
@return |
210 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
211 |
0 |
private static String fromCharacters(List<Character> characters)... |
212 |
|
{ |
213 |
0 |
return StringUtils.join(characters, null); |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
|
@param |
218 |
|
@return |
219 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
220 |
0 |
private static List<Character> toCharacters(String str)... |
221 |
|
{ |
222 |
0 |
List<Character> characters; |
223 |
|
|
224 |
0 |
if (str != null) { |
225 |
0 |
characters = new ArrayList<Character>(str.length()); |
226 |
|
|
227 |
0 |
for (char c : str.toCharArray()) { |
228 |
0 |
characters.add(c); |
229 |
|
} |
230 |
|
} else { |
231 |
0 |
characters = Collections.emptyList(); |
232 |
|
} |
233 |
|
|
234 |
0 |
return characters; |
235 |
|
} |
236 |
|
} |