1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.extension.xar.internal.job.diff; |
21 |
|
|
22 |
|
import java.util.List; |
23 |
|
import java.util.Map; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Singleton; |
27 |
|
|
28 |
|
import org.xwiki.component.annotation.Component; |
29 |
|
import org.xwiki.diff.display.UnifiedDiffBlock; |
30 |
|
import org.xwiki.extension.xar.job.diff.DocumentUnifiedDiff; |
31 |
|
import org.xwiki.extension.xar.job.diff.DocumentVersionReference; |
32 |
|
import org.xwiki.extension.xar.job.diff.EntityUnifiedDiff; |
33 |
|
import org.xwiki.model.reference.ClassPropertyReference; |
34 |
|
import org.xwiki.model.reference.DocumentReference; |
35 |
|
import org.xwiki.model.reference.ObjectReference; |
36 |
|
|
37 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
38 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
39 |
|
import com.xpn.xwiki.objects.BaseCollection; |
40 |
|
import com.xpn.xwiki.objects.BaseObject; |
41 |
|
import com.xpn.xwiki.objects.BaseObjectReference; |
42 |
|
import com.xpn.xwiki.objects.BaseProperty; |
43 |
|
import com.xpn.xwiki.objects.classes.BaseClass; |
44 |
|
import com.xpn.xwiki.objects.classes.PropertyClass; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
52 |
|
@Component(roles = DocumentUnifiedDiffBuilder.class) |
53 |
|
@Singleton |
|
|
| 0% |
Uncovered Elements: 177 (177) |
Complexity: 50 |
Complexity Density: 0.52 |
|
54 |
|
public class DocumentUnifiedDiffBuilder extends AbstractUnifiedDiffBuilder |
55 |
|
{ |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
private static final DocumentReference EMPTY_DOCUMENT_REFERENCE = new DocumentReference("wiki", "Space", |
60 |
|
"EmptyDocument"); |
61 |
|
|
62 |
|
@Inject |
63 |
|
private AttachmentUnifiedDiffBuilder attachmentDiffBuilder; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
@param |
70 |
|
@param |
71 |
|
@return |
72 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
73 |
0 |
public DocumentUnifiedDiff diff(XWikiDocument previousDocument, XWikiDocument nextDocument)... |
74 |
|
{ |
75 |
0 |
DocumentUnifiedDiff diff = |
76 |
|
new DocumentUnifiedDiff(getDocumentVersionReference(previousDocument), |
77 |
|
getDocumentVersionReference(nextDocument)); |
78 |
|
|
79 |
0 |
if (previousDocument != nextDocument) { |
80 |
0 |
XWikiDocument nonNullPreviousDocument = emptyDocumentIfNull(previousDocument); |
81 |
0 |
XWikiDocument nonNullNextDocument = emptyDocumentIfNull(nextDocument); |
82 |
|
|
83 |
0 |
addDocumentFieldDiffs(nonNullPreviousDocument, nonNullNextDocument, diff); |
84 |
0 |
addAttachmentDiffs(nonNullPreviousDocument, nonNullNextDocument, diff); |
85 |
0 |
addObjectDiffs(nonNullPreviousDocument, nonNullNextDocument, diff); |
86 |
0 |
addClassPropertyDiffs(nonNullPreviousDocument.getXClass(), nonNullNextDocument.getXClass(), diff); |
87 |
|
} |
88 |
|
|
89 |
0 |
return diff; |
90 |
|
} |
91 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
92 |
0 |
private DocumentVersionReference getDocumentVersionReference(XWikiDocument document)... |
93 |
|
{ |
94 |
0 |
if (document == null) { |
95 |
0 |
return null; |
96 |
|
} else { |
97 |
0 |
DocumentVersionReference documentVersionReference = |
98 |
|
new DocumentVersionReference(document.getDocumentReferenceWithLocale()); |
99 |
0 |
if (documentVersionReference.getVersion() == null) { |
100 |
0 |
return new DocumentVersionReference(documentVersionReference, document.getVersion()); |
101 |
|
} |
102 |
0 |
return documentVersionReference; |
103 |
|
} |
104 |
|
} |
105 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
106 |
0 |
private XWikiDocument emptyDocumentIfNull(XWikiDocument document)... |
107 |
|
{ |
108 |
0 |
if (document == null) { |
109 |
0 |
XWikiDocument emptyDocument = new XWikiDocument(EMPTY_DOCUMENT_REFERENCE); |
110 |
0 |
emptyDocument.setSyntax(null); |
111 |
0 |
return emptyDocument; |
112 |
|
} else { |
113 |
0 |
return document; |
114 |
|
} |
115 |
|
} |
116 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
117 |
0 |
private boolean isNull(XWikiDocument document)... |
118 |
|
{ |
119 |
0 |
return document.getDocumentReference() == EMPTY_DOCUMENT_REFERENCE; |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
@param |
127 |
|
@param |
128 |
|
@param |
129 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
130 |
0 |
private void addDocumentFieldDiffs(XWikiDocument previousDocument, XWikiDocument nextDocument,... |
131 |
|
DocumentUnifiedDiff documentDiff) |
132 |
|
{ |
133 |
0 |
maybeAddDiff(documentDiff, "title", previousDocument.getTitle(), nextDocument.getTitle()); |
134 |
0 |
maybeAddDiff(documentDiff, "parent", previousDocument.getParentReference(), nextDocument.getParentReference()); |
135 |
0 |
maybeAddDiff(documentDiff, "hidden", isNull(previousDocument) ? null : previousDocument.isHidden(), |
136 |
0 |
isNull(nextDocument) ? null : nextDocument.isHidden()); |
137 |
0 |
maybeAddDiff(documentDiff, "defaultLocale", previousDocument.getDefaultLocale(), |
138 |
|
nextDocument.getDefaultLocale()); |
139 |
0 |
maybeAddDiff(documentDiff, "syntax", previousDocument.getSyntax(), nextDocument.getSyntax()); |
140 |
0 |
maybeAddDiff(documentDiff, CONTENT, previousDocument.getContent(), nextDocument.getContent()); |
141 |
|
} |
142 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
143 |
0 |
private void addAttachmentDiffs(XWikiDocument previousDocument, XWikiDocument nextDocument,... |
144 |
|
DocumentUnifiedDiff documentDiff) |
145 |
|
{ |
146 |
|
|
147 |
0 |
for (XWikiAttachment previousAttachment : previousDocument.getAttachmentList()) { |
148 |
0 |
XWikiAttachment nextAttachment = nextDocument.getAttachment(previousAttachment.getFilename()); |
149 |
0 |
if (previousAttachment != nextAttachment) { |
150 |
0 |
this.attachmentDiffBuilder.addAttachmentDiff(previousAttachment, nextAttachment, documentDiff); |
151 |
|
} |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
0 |
for (XWikiAttachment nextAttachment : nextDocument.getAttachmentList()) { |
156 |
0 |
XWikiAttachment previousAttachment = previousDocument.getAttachment(nextAttachment.getFilename()); |
157 |
0 |
if (previousAttachment == null) { |
158 |
0 |
this.attachmentDiffBuilder.addAttachmentDiff(previousAttachment, nextAttachment, documentDiff); |
159 |
|
} |
160 |
|
} |
161 |
|
} |
162 |
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 5 |
Complexity Density: 0.42 |
|
163 |
0 |
private void addObjectDiffs(XWikiDocument previousDocument, XWikiDocument nextDocument,... |
164 |
|
DocumentUnifiedDiff documentDiff) |
165 |
|
{ |
166 |
|
|
167 |
0 |
for (List<BaseObject> previousObjects : previousDocument.getXObjects().values()) { |
168 |
0 |
for (BaseObject previousObject : previousObjects) { |
169 |
|
|
170 |
0 |
if (previousObject != null) { |
171 |
0 |
BaseObject nextObject = |
172 |
|
nextDocument.getXObject(previousObject.getXClassReference(), previousObject.getNumber()); |
173 |
0 |
if (previousObject != nextObject) { |
174 |
0 |
addObjectDiff(previousObject, nextObject, documentDiff); |
175 |
|
} |
176 |
|
} |
177 |
|
} |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
0 |
for (List<BaseObject> nextObjects : nextDocument.getXObjects().values()) { |
182 |
0 |
for (BaseObject nextObject : nextObjects) { |
183 |
|
|
184 |
0 |
if (nextObject != null) { |
185 |
0 |
BaseObject previousObject = |
186 |
|
previousDocument.getXObject(nextObject.getXClassReference(), nextObject.getNumber()); |
187 |
0 |
if (previousObject == null) { |
188 |
0 |
addObjectDiff(previousObject, nextObject, documentDiff); |
189 |
|
} |
190 |
|
} |
191 |
|
} |
192 |
|
} |
193 |
|
} |
194 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0.67 |
|
195 |
0 |
private void addObjectDiff(BaseObject previousObject, BaseObject nextObject, DocumentUnifiedDiff documentDiff)... |
196 |
|
{ |
197 |
0 |
ObjectReference previousReference = |
198 |
|
getObjectVersionReference(previousObject, documentDiff.getPreviousReference()); |
199 |
0 |
ObjectReference nextReference = getObjectVersionReference(nextObject, documentDiff.getNextReference()); |
200 |
0 |
EntityUnifiedDiff<ObjectReference> objectDiff = new EntityUnifiedDiff<>(previousReference, nextReference); |
201 |
|
|
202 |
0 |
addObjectDiff(previousObject == null ? new BaseObject() : previousObject, nextObject == null ? new BaseObject() |
203 |
|
: nextObject, objectDiff); |
204 |
|
|
205 |
0 |
if (objectDiff.size() > 0) { |
206 |
0 |
documentDiff.getObjectDiffs().add(objectDiff); |
207 |
|
} |
208 |
|
} |
209 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
210 |
0 |
private ObjectReference getObjectVersionReference(BaseObject object,... |
211 |
|
DocumentVersionReference documentVersionReference) |
212 |
|
{ |
213 |
0 |
return object == null ? null : new BaseObjectReference(object.getXClassReference(), object.getNumber(), |
214 |
|
documentVersionReference); |
215 |
|
} |
216 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
217 |
0 |
private void addObjectDiff(BaseCollection<?> previousObject, BaseCollection<?> nextObject,... |
218 |
|
Map<String, List<UnifiedDiffBlock<String, Character>>> objectDiff) |
219 |
|
{ |
220 |
|
|
221 |
0 |
for (String propertyName : previousObject.getPropertyList()) { |
222 |
0 |
BaseProperty<?> previousProperty = (BaseProperty<?>) previousObject.getField(propertyName); |
223 |
0 |
BaseProperty<?> nextProperty = (BaseProperty<?>) nextObject.getField(propertyName); |
224 |
0 |
if (previousProperty != nextProperty) { |
225 |
0 |
addObjectPropertyDiff(previousProperty, nextProperty, objectDiff); |
226 |
|
} |
227 |
|
} |
228 |
|
|
229 |
|
|
230 |
0 |
for (String propertyName : nextObject.getPropertyList()) { |
231 |
0 |
BaseProperty<?> previousProperty = (BaseProperty<?>) previousObject.getField(propertyName); |
232 |
0 |
BaseProperty<?> nextProperty = (BaseProperty<?>) nextObject.getField(propertyName); |
233 |
0 |
if (previousProperty == null) { |
234 |
0 |
addObjectPropertyDiff(previousProperty, nextProperty, objectDiff); |
235 |
|
} |
236 |
|
} |
237 |
|
} |
238 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 7 |
Complexity Density: 1.4 |
|
239 |
0 |
private void addObjectPropertyDiff(BaseProperty<?> previousProperty, BaseProperty<?> nextProperty,... |
240 |
|
Map<String, List<UnifiedDiffBlock<String, Character>>> objectDiff) |
241 |
|
{ |
242 |
0 |
String key = previousProperty == null ? nextProperty.getName() : previousProperty.getName(); |
243 |
0 |
Object previousValue = previousProperty == null ? null : previousProperty.getValue(); |
244 |
0 |
Object nextValue = nextProperty == null ? null : nextProperty.getValue(); |
245 |
0 |
if (maybeAddDiff(objectDiff, key, previousValue, nextValue) |
246 |
|
&& (isPrivateProperty(previousProperty) || isPrivateProperty(nextProperty))) { |
247 |
|
|
248 |
0 |
objectDiff.get(key).clear(); |
249 |
|
} |
250 |
|
} |
251 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 5 |
Complexity Density: 0.62 |
|
252 |
0 |
private boolean isPrivateProperty(BaseProperty<?> property)... |
253 |
|
{ |
254 |
0 |
BaseCollection<?> object = property == null ? null : property.getObject(); |
255 |
0 |
if (object != null) { |
256 |
0 |
BaseClass xclass = object.getXClass(this.xcontextProvider.get()); |
257 |
0 |
if (xclass != null) { |
258 |
0 |
PropertyClass propertyClass = (PropertyClass) xclass.get(property.getName()); |
259 |
0 |
String propertyType = propertyClass == null ? null : propertyClass.getClassType(); |
260 |
0 |
return "Password".equals(propertyType) || "Email".equals(propertyClass); |
261 |
|
} |
262 |
|
} |
263 |
0 |
return false; |
264 |
|
} |
265 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
266 |
0 |
private void addClassPropertyDiffs(BaseClass previousClass, BaseClass nextClass, DocumentUnifiedDiff documentDiff)... |
267 |
|
{ |
268 |
|
|
269 |
0 |
for (String propertyName : previousClass.getPropertyList()) { |
270 |
0 |
PropertyClass previousProperty = (PropertyClass) previousClass.get(propertyName); |
271 |
0 |
PropertyClass nextProperty = (PropertyClass) nextClass.get(propertyName); |
272 |
0 |
addClassPropertyDiff(previousProperty, nextProperty, documentDiff); |
273 |
|
} |
274 |
|
|
275 |
|
|
276 |
0 |
for (String propertyName : nextClass.getPropertyList()) { |
277 |
0 |
PropertyClass previousProperty = (PropertyClass) previousClass.get(propertyName); |
278 |
0 |
PropertyClass nextProperty = (PropertyClass) nextClass.get(propertyName); |
279 |
0 |
if (previousProperty == null) { |
280 |
0 |
addClassPropertyDiff(previousProperty, nextProperty, documentDiff); |
281 |
|
} |
282 |
|
} |
283 |
|
} |
284 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 6 |
Complexity Density: 0.67 |
|
285 |
0 |
private void addClassPropertyDiff(PropertyClass previousProperty, PropertyClass nextProperty,... |
286 |
|
DocumentUnifiedDiff documentDiff) |
287 |
|
{ |
288 |
0 |
ClassPropertyReference previousReference = |
289 |
|
getClassPropertyVersionReference(previousProperty, documentDiff.getPreviousReference()); |
290 |
0 |
ClassPropertyReference nextReference = |
291 |
|
getClassPropertyVersionReference(nextProperty, documentDiff.getNextReference()); |
292 |
0 |
EntityUnifiedDiff<ClassPropertyReference> classPropertyDiff = |
293 |
|
new EntityUnifiedDiff<>(previousReference, nextReference); |
294 |
|
|
295 |
|
|
296 |
0 |
maybeAddDiff(classPropertyDiff, "type", previousProperty == null ? null : previousProperty.getClassType(), |
297 |
0 |
nextProperty == null ? null : nextProperty.getClassType()); |
298 |
|
|
299 |
0 |
addObjectDiff(previousProperty == null ? new PropertyClass() : previousProperty, nextProperty == null |
300 |
|
? new PropertyClass() : nextProperty, classPropertyDiff); |
301 |
|
|
302 |
|
|
303 |
0 |
classPropertyDiff.remove("name"); |
304 |
|
|
305 |
0 |
classPropertyDiff.remove("unmodifiable"); |
306 |
|
|
307 |
0 |
if (classPropertyDiff.size() > 0) { |
308 |
0 |
documentDiff.getClassPropertyDiffs().add(classPropertyDiff); |
309 |
|
} |
310 |
|
} |
311 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
312 |
0 |
private ClassPropertyReference getClassPropertyVersionReference(PropertyClass property,... |
313 |
|
DocumentVersionReference documentVersionReference) |
314 |
|
{ |
315 |
0 |
return property == null ? null : new ClassPropertyReference(property.getName(), documentVersionReference); |
316 |
|
} |
317 |
|
} |