1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.watchlist.internal; |
21 |
|
|
22 |
|
import java.util.List; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Provider; |
26 |
|
import javax.inject.Singleton; |
27 |
|
|
28 |
|
import org.apache.commons.lang3.StringUtils; |
29 |
|
import org.apache.ecs.html.Div; |
30 |
|
import org.apache.ecs.html.Span; |
31 |
|
import org.slf4j.Logger; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.watchlist.internal.api.WatchListEvent; |
34 |
|
import org.xwiki.watchlist.internal.api.WatchListEventType; |
35 |
|
import org.xwiki.watchlist.internal.api.WatchListException; |
36 |
|
|
37 |
|
import com.xpn.xwiki.XWikiContext; |
38 |
|
import com.xpn.xwiki.XWikiException; |
39 |
|
import com.xpn.xwiki.doc.AttachmentDiff; |
40 |
|
import com.xpn.xwiki.doc.MetaDataDiff; |
41 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
42 |
|
import com.xpn.xwiki.objects.ObjectDiff; |
43 |
|
import com.xpn.xwiki.plugin.diff.DiffPluginApi; |
44 |
|
|
45 |
|
|
46 |
|
@link |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
@version |
51 |
|
|
52 |
|
@Component |
53 |
|
@Singleton |
|
|
| 44.3% |
Uncovered Elements: 54 (97) |
Complexity: 18 |
Complexity Density: 0.24 |
|
54 |
|
public class DefaultWatchListEventHTMLDiffExtractor implements WatchListEventHTMLDiffExtractor |
55 |
|
{ |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
private static final String HTML_STYLE_PLACEHOLDER_PREFIX = "WATCHLIST_STYLE_DIFF_"; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
private static final String HTML_IMG_PLACEHOLDER_SUFFIX = "_WATCHLIST_IMG_PLACEHOLDER"; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
private static final String HTML_IMG_METADATA_PREFIX = "metadata"; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
private static final String HTML_IMG_ATTACHMENT_PREFIX = "attach"; |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
private static final String HIDDEN_PROPERTIES_OBFUSCATED_VALUE = "******************"; |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
private static final String PASSWORD_CLASS_NAME = "Password"; |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
private static final String EMAIL_PROPERTY_NAME = "email"; |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
private static final String INITIAL_DOCUMENT_VERSION = "1.1"; |
95 |
|
|
96 |
|
@Inject |
97 |
|
private Logger logger; |
98 |
|
|
99 |
|
@Inject |
100 |
|
private Provider<XWikiContext> contextProvider; |
101 |
|
|
|
|
| 80% |
Uncovered Elements: 7 (35) |
Complexity: 4 |
Complexity Density: 0.13 |
|
102 |
2 |
@Override... |
103 |
|
public String getHTMLDiff(WatchListEvent event) throws WatchListException |
104 |
|
{ |
105 |
2 |
XWikiContext context = contextProvider.get(); |
106 |
|
|
107 |
2 |
StringBuffer result = new StringBuffer(); |
108 |
|
|
109 |
2 |
try { |
110 |
2 |
DiffPluginApi diff = (DiffPluginApi) context.getWiki().getPluginApi("diff", context); |
111 |
|
|
112 |
2 |
XWikiDocument d2 = context.getWiki().getDocument(event.getDocumentReference(), context); |
113 |
|
|
114 |
2 |
if (event.getType().equals(WatchListEventType.CREATE)) { |
115 |
2 |
d2 = context.getWiki().getDocument(d2, INITIAL_DOCUMENT_VERSION, context); |
116 |
|
} |
117 |
|
|
118 |
2 |
XWikiDocument d1 = context.getWiki().getDocument(d2, event.getPreviousVersion(), context); |
119 |
2 |
List<AttachmentDiff> attachDiffs = d2.getAttachmentDiff(d1, d2, context); |
120 |
2 |
List<List<ObjectDiff>> objectDiffs = d2.getObjectDiff(d1, d2, context); |
121 |
2 |
List<List<ObjectDiff>> classDiffs = d2.getClassDiff(d1, d2, context); |
122 |
2 |
List<MetaDataDiff> metaDiffs = d2.getMetaDataDiff(d1, d2, context); |
123 |
|
|
124 |
2 |
if (!d1.getContent().equals(d2.getContent())) { |
125 |
2 |
Div contentDiv = createDiffDiv("contentDiff"); |
126 |
2 |
String contentDiff = diff.getDifferencesAsHTML(d1.getContent(), d2.getContent(), false); |
127 |
2 |
contentDiv.addElement(contentDiff); |
128 |
2 |
result.append(contentDiv); |
129 |
|
} |
130 |
|
|
131 |
2 |
for (AttachmentDiff aDiff : attachDiffs) { |
132 |
0 |
Div attachmentDiv = createDiffDiv("attachmentDiff"); |
133 |
0 |
attachmentDiv.addElement(HTML_IMG_ATTACHMENT_PREFIX + HTML_IMG_PLACEHOLDER_SUFFIX); |
134 |
0 |
attachmentDiv.addElement(aDiff.toString()); |
135 |
0 |
result.append(attachmentDiv); |
136 |
|
} |
137 |
|
|
138 |
2 |
result.append(getObjectsHTMLDiff(objectDiffs, false, event.getFullName(), diff)); |
139 |
2 |
result.append(getObjectsHTMLDiff(classDiffs, true, event.getFullName(), diff)); |
140 |
|
|
141 |
2 |
for (MetaDataDiff mDiff : metaDiffs) { |
142 |
4 |
Div metaDiv = createDiffDiv("metaDiff"); |
143 |
4 |
metaDiv.addElement(HTML_IMG_METADATA_PREFIX + HTML_IMG_PLACEHOLDER_SUFFIX); |
144 |
4 |
metaDiv.addElement(mDiff.toString()); |
145 |
4 |
result.append(metaDiv); |
146 |
|
} |
147 |
|
|
148 |
2 |
return result.toString(); |
149 |
|
} catch (Exception e) { |
150 |
0 |
throw new WatchListException(String.format("Failed to compute HTML diff for event type [%s] on [%s]", |
151 |
|
event.getType(), event.getPrefixedFullName()), e); |
152 |
|
} |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
|
@param |
157 |
|
@return |
158 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
159 |
6 |
private static Div createDiffDiv(String classAttr)... |
160 |
|
{ |
161 |
6 |
Div div = new Div(); |
162 |
6 |
div.setClass(classAttr); |
163 |
6 |
div.setStyle(HTML_STYLE_PLACEHOLDER_PREFIX + classAttr); |
164 |
|
|
165 |
6 |
return div; |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
|
@param |
170 |
|
@return |
171 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
172 |
0 |
private static Span createDiffSpan(String classAttr)... |
173 |
|
{ |
174 |
0 |
Span span = new Span(); |
175 |
0 |
span.setClass(classAttr); |
176 |
0 |
span.setStyle(HTML_STYLE_PLACEHOLDER_PREFIX + classAttr); |
177 |
|
|
178 |
0 |
return span; |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
@param |
185 |
|
@param |
186 |
|
@return |
187 |
|
@throws |
188 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 4 |
Complexity Density: 1 |
|
189 |
0 |
private static String getPropertyHTMLDiff(ObjectDiff objectDiff, DiffPluginApi diff) throws XWikiException... |
190 |
|
{ |
191 |
0 |
String propDiff = |
192 |
|
diff.getDifferencesAsHTML(objectDiff.getPrevValue().toString(), objectDiff.getNewValue().toString(), false); |
193 |
|
|
194 |
|
|
195 |
0 |
if ((objectDiff.getPropType().equals(PASSWORD_CLASS_NAME) || objectDiff.getPropName().equals( |
196 |
|
EMAIL_PROPERTY_NAME)) |
197 |
|
&& !StringUtils.isBlank(propDiff)) { |
198 |
0 |
propDiff = HIDDEN_PROPERTIES_OBFUSCATED_VALUE; |
199 |
|
} |
200 |
|
|
201 |
0 |
return propDiff; |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
|
@param |
206 |
|
@param |
207 |
|
@param |
208 |
|
@param |
209 |
|
@return |
210 |
|
|
|
|
| 18.6% |
Uncovered Elements: 35 (43) |
Complexity: 8 |
Complexity Density: 0.24 |
|
211 |
4 |
private String getObjectsHTMLDiff(List<List<ObjectDiff>> objectDiffs, boolean isXWikiClass,... |
212 |
|
String documentFullName, DiffPluginApi diff) |
213 |
|
{ |
214 |
4 |
StringBuffer result = new StringBuffer(); |
215 |
4 |
String propSeparator = ": "; |
216 |
4 |
String prefix = (isXWikiClass) ? "class" : "object"; |
217 |
|
|
218 |
4 |
try { |
219 |
4 |
for (List<ObjectDiff> oList : objectDiffs) { |
220 |
0 |
if (oList.isEmpty()) { |
221 |
0 |
continue; |
222 |
|
} |
223 |
|
|
224 |
|
|
225 |
0 |
Div mainDiv = createDiffDiv(prefix + "Diff"); |
226 |
|
|
227 |
|
|
228 |
0 |
Span objectName = createDiffSpan(prefix + "ClassName"); |
229 |
0 |
if (isXWikiClass) { |
230 |
0 |
objectName.addElement(documentFullName); |
231 |
|
} else { |
232 |
0 |
objectName.addElement(oList.get(0).getClassName()); |
233 |
|
} |
234 |
0 |
mainDiv.addElement(prefix + HTML_IMG_PLACEHOLDER_SUFFIX); |
235 |
0 |
mainDiv.addElement(objectName); |
236 |
|
|
237 |
|
|
238 |
0 |
for (ObjectDiff oDiff : oList) { |
239 |
0 |
String propDiff = getPropertyHTMLDiff(oDiff, diff); |
240 |
0 |
if (StringUtils.isBlank(oDiff.getPropName()) || StringUtils.isBlank(propDiff)) { |
241 |
|
|
242 |
0 |
continue; |
243 |
|
} |
244 |
|
|
245 |
0 |
Div propDiv = createDiffDiv("propDiffContainer"); |
246 |
|
|
247 |
|
|
248 |
0 |
Span propNameSpan = createDiffSpan("propName"); |
249 |
0 |
propNameSpan.addElement(oDiff.getPropName() + propSeparator); |
250 |
0 |
String shortPropType = StringUtils.removeEnd(oDiff.getPropType(), "Class").toLowerCase(); |
251 |
0 |
if (StringUtils.isBlank(shortPropType)) { |
252 |
|
|
253 |
0 |
shortPropType = HTML_IMG_METADATA_PREFIX; |
254 |
|
} |
255 |
0 |
propDiv.addElement(shortPropType + HTML_IMG_PLACEHOLDER_SUFFIX); |
256 |
0 |
propDiv.addElement(propNameSpan); |
257 |
|
|
258 |
|
|
259 |
0 |
Div propDiffDiv = createDiffDiv("propDiff"); |
260 |
0 |
propDiffDiv.addElement(propDiff); |
261 |
0 |
propDiv.addElement(propDiffDiv); |
262 |
|
|
263 |
|
|
264 |
0 |
mainDiv.addElement(propDiv); |
265 |
|
} |
266 |
|
|
267 |
0 |
result.append(mainDiv); |
268 |
|
} |
269 |
|
} catch (XWikiException e) { |
270 |
|
|
271 |
0 |
logger.error("Failed to compute HTML objects or class diff", e); |
272 |
|
} |
273 |
|
|
274 |
4 |
return result.toString(); |
275 |
|
} |
276 |
|
} |