1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.plugin.diff; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.List; |
24 |
|
|
25 |
|
import org.apache.commons.lang3.StringUtils; |
26 |
|
import org.suigeneris.jrcs.diff.Diff; |
27 |
|
import org.suigeneris.jrcs.diff.Revision; |
28 |
|
import org.suigeneris.jrcs.diff.delta.Chunk; |
29 |
|
import org.suigeneris.jrcs.diff.delta.Delta; |
30 |
|
import org.suigeneris.jrcs.util.ToString; |
31 |
|
import org.xwiki.xml.XMLUtils; |
32 |
|
|
33 |
|
import com.xpn.xwiki.XWikiContext; |
34 |
|
import com.xpn.xwiki.XWikiException; |
35 |
|
import com.xpn.xwiki.api.Api; |
36 |
|
import com.xpn.xwiki.plugin.XWikiDefaultPlugin; |
37 |
|
import com.xpn.xwiki.plugin.XWikiPluginInterface; |
38 |
|
|
39 |
|
|
40 |
|
@version |
41 |
|
@deprecated |
42 |
|
|
43 |
|
@Deprecated |
|
|
| 86.5% |
Uncovered Elements: 28 (208) |
Complexity: 44 |
Complexity Density: 0.33 |
|
44 |
|
public class DiffPlugin extends XWikiDefaultPlugin |
45 |
|
{ |
46 |
|
|
47 |
|
@param |
48 |
|
@param |
49 |
|
@param |
50 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
51 |
14 |
public DiffPlugin(String name, String className, XWikiContext context)... |
52 |
|
{ |
53 |
14 |
super(name, className, context); |
54 |
|
} |
55 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
56 |
2 |
@Override... |
57 |
|
public String getName() |
58 |
|
{ |
59 |
2 |
return "diff"; |
60 |
|
} |
61 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
62 |
2 |
@Override... |
63 |
|
public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) |
64 |
|
{ |
65 |
2 |
return new DiffPluginApi((DiffPlugin) plugin, context); |
66 |
|
} |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
@param |
72 |
|
@param |
73 |
|
@return |
74 |
|
|
|
|
| 54.5% |
Uncovered Elements: 5 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
75 |
8 |
public List getDifferencesAsList(String text1, String text2) throws XWikiException... |
76 |
|
{ |
77 |
8 |
try { |
78 |
8 |
if (text1 == null) { |
79 |
0 |
text1 = ""; |
80 |
|
} |
81 |
8 |
if (text2 == null) { |
82 |
0 |
text2 = ""; |
83 |
|
} |
84 |
8 |
return getDeltas(Diff.diff(ToString.stringToArray(text1), ToString.stringToArray(text2))); |
85 |
|
} catch (Exception e) { |
86 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_DIFF, XWikiException.ERROR_XWIKI_DIFF_CONTENT_ERROR, |
87 |
|
"Diff of content generated an exception", e); |
88 |
|
} |
89 |
|
} |
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
91 |
18 |
protected List getDeltas(Revision rev)... |
92 |
|
{ |
93 |
18 |
ArrayList list = new ArrayList(); |
94 |
43 |
for (int i = 0; i < rev.size(); i++) { |
95 |
25 |
list.add(rev.getDelta(i)); |
96 |
|
} |
97 |
18 |
return list; |
98 |
|
} |
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
100 |
35 |
protected String escape(String text)... |
101 |
|
{ |
102 |
35 |
return XMLUtils.escape(text); |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
@param |
109 |
|
@param |
110 |
|
@return |
111 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
112 |
10 |
public List getWordDifferencesAsList(String text1, String text2) throws XWikiException... |
113 |
|
{ |
114 |
10 |
try { |
115 |
10 |
text1 = text1.replaceAll(" ", "\n"); |
116 |
10 |
text2 = text2.replaceAll(" ", "\n"); |
117 |
10 |
return getDeltas(Diff.diff(ToString.stringToArray(text1), ToString.stringToArray(text2))); |
118 |
|
} catch (Exception e) { |
119 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_DIFF, XWikiException.ERROR_XWIKI_DIFF_CONTENT_ERROR, |
120 |
|
"Diff of content generated an exception", e); |
121 |
|
} |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
@param |
128 |
|
@param |
129 |
|
@return |
130 |
|
|
|
|
| 85.3% |
Uncovered Elements: 10 (68) |
Complexity: 12 |
Complexity Density: 0.26 |
|
131 |
6 |
public String getWordDifferencesAsHTML(String text1, String text2) throws XWikiException... |
132 |
|
{ |
133 |
6 |
text1 = "~~PLACEHOLDER~~" + text1 + "~~PLACEHOLDER~~"; |
134 |
6 |
text2 = "~~PLACEHOLDER~~" + text2 + "~~PLACEHOLDER~~"; |
135 |
|
|
136 |
6 |
StringBuilder html = new StringBuilder("<div class=\"diffmodifiedline\">"); |
137 |
6 |
List list = getWordDifferencesAsList(text1, text2); |
138 |
6 |
String[] words = StringUtils.splitPreserveAllTokens(text1, ' '); |
139 |
6 |
int cursor = 0; |
140 |
6 |
boolean addSpace = false; |
141 |
|
|
142 |
16 |
for (int i = 0; i < list.size(); i++) { |
143 |
10 |
if (addSpace) { |
144 |
4 |
html.append(" "); |
145 |
4 |
addSpace = false; |
146 |
|
} |
147 |
|
|
148 |
10 |
Delta delta = (Delta) list.get(i); |
149 |
10 |
int position = delta.getOriginal().anchor(); |
150 |
|
|
151 |
20 |
while (cursor < position) { |
152 |
10 |
html.append(escape(words[cursor])); |
153 |
10 |
html.append(" "); |
154 |
10 |
cursor++; |
155 |
|
} |
156 |
|
|
157 |
10 |
Chunk orig = delta.getOriginal(); |
158 |
10 |
if (orig.size() > 0) { |
159 |
8 |
html.append("<span class=\"diffremoveword\">"); |
160 |
8 |
List chunks = orig.chunk(); |
161 |
16 |
for (int j = 0; j < chunks.size(); j++) { |
162 |
8 |
if (j > 0) { |
163 |
0 |
html.append(" "); |
164 |
|
} |
165 |
8 |
html.append(escape((String) chunks.get(j))); |
166 |
8 |
cursor++; |
167 |
|
} |
168 |
8 |
html.append("</span>"); |
169 |
8 |
addSpace = true; |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
10 |
Chunk rev = delta.getRevised(); |
174 |
10 |
if (rev.size() > 0) { |
175 |
8 |
html.append("<span class=\"diffaddword\">"); |
176 |
8 |
List chunks = rev.chunk(); |
177 |
17 |
for (int j = 0; j < chunks.size(); j++) { |
178 |
9 |
if (j > 0) { |
179 |
1 |
html.append(" "); |
180 |
|
} |
181 |
9 |
html.append(escape((String) chunks.get(j))); |
182 |
|
} |
183 |
8 |
html.append("</span>"); |
184 |
8 |
addSpace = true; |
185 |
|
} |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
6 |
while (cursor < words.length) { |
190 |
0 |
if (addSpace) { |
191 |
0 |
html.append(" "); |
192 |
|
} |
193 |
0 |
html.append(escape(words[cursor])); |
194 |
0 |
addSpace = true; |
195 |
0 |
cursor++; |
196 |
|
} |
197 |
|
|
198 |
6 |
html.append("</div>"); |
199 |
6 |
return html.toString().replaceAll("~~PLACEHOLDER~~", ""); |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
@param |
206 |
|
@param |
207 |
|
@return |
208 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
209 |
4 |
public String getDifferencesAsHTML(String text1, String text2) throws XWikiException... |
210 |
|
{ |
211 |
4 |
return getDifferencesAsHTML(text1, text2, true); |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
@param |
218 |
|
@param |
219 |
|
@param |
220 |
|
@return |
221 |
|
|
|
|
| 88.3% |
Uncovered Elements: 12 (103) |
Complexity: 19 |
Complexity Density: 0.28 |
|
222 |
6 |
public String getDifferencesAsHTML(String text1, String text2, boolean allDoc) throws XWikiException... |
223 |
|
{ |
224 |
6 |
StringBuilder html = new StringBuilder("<div class=\"diff\">"); |
225 |
6 |
if (text1 == null) { |
226 |
0 |
text1 = ""; |
227 |
|
} |
228 |
6 |
if (text2 == null) { |
229 |
0 |
text2 = ""; |
230 |
|
} |
231 |
6 |
List list = getDifferencesAsList(text1, text2); |
232 |
6 |
String[] lines = ToString.stringToArray(text1); |
233 |
6 |
int cursor = 0; |
234 |
6 |
boolean addBR = false; |
235 |
|
|
236 |
13 |
for (int i = 0; i < list.size(); i++) { |
237 |
7 |
if (addBR) { |
238 |
1 |
addBR = false; |
239 |
|
} |
240 |
|
|
241 |
7 |
Delta delta = (Delta) list.get(i); |
242 |
7 |
int position = delta.getOriginal().anchor(); |
243 |
|
|
244 |
9 |
while (cursor < position) { |
245 |
2 |
if (allDoc) { |
246 |
2 |
html.append("<div class=\"diffunmodifiedline\">"); |
247 |
2 |
String text = escape(lines[cursor]); |
248 |
2 |
if (text.equals("")) { |
249 |
0 |
text = " "; |
250 |
|
} |
251 |
2 |
html.append(text); |
252 |
2 |
html.append("</div>"); |
253 |
|
} |
254 |
2 |
cursor++; |
255 |
|
} |
256 |
|
|
257 |
|
|
258 |
7 |
Chunk orig = delta.getOriginal(); |
259 |
7 |
Chunk rev = delta.getRevised(); |
260 |
7 |
int j1 = 0; |
261 |
|
|
262 |
7 |
if (orig.size() > 0) { |
263 |
4 |
List chunks = orig.chunk(); |
264 |
4 |
int j2 = 0; |
265 |
8 |
for (int j = 0; j < chunks.size(); j++) { |
266 |
4 |
String origline = (String) chunks.get(j); |
267 |
4 |
if (origline.equals("")) { |
268 |
0 |
cursor++; |
269 |
0 |
continue; |
270 |
|
} |
271 |
|
|
272 |
|
|
273 |
4 |
List revchunks = rev.chunk(); |
274 |
4 |
String revline = ""; |
275 |
8 |
while ("".equals(revline)) { |
276 |
4 |
revline = (j2 >= revchunks.size()) ? null : (String) revchunks.get(j2); |
277 |
4 |
j2++; |
278 |
4 |
j1++; |
279 |
|
} |
280 |
4 |
if (revline != null) { |
281 |
3 |
html.append(getWordDifferencesAsHTML(origline, revline)); |
282 |
|
} else { |
283 |
1 |
html.append("<div class=\"diffmodifiedline\">"); |
284 |
1 |
html.append("<span class=\"diffremoveword\">"); |
285 |
1 |
html.append(escape(origline)); |
286 |
1 |
html.append("</span></div>"); |
287 |
|
} |
288 |
4 |
addBR = true; |
289 |
4 |
cursor++; |
290 |
|
} |
291 |
|
} |
292 |
|
|
293 |
|
|
294 |
7 |
if (rev.size() > 0) { |
295 |
6 |
List chunks = rev.chunk(); |
296 |
10 |
for (int j = j1; j < chunks.size(); j++) { |
297 |
|
|
298 |
|
|
299 |
4 |
html.append("<div class=\"diffmodifiedline\">"); |
300 |
4 |
html.append("<span class=\"diffaddword\">"); |
301 |
4 |
html.append(escape((String) chunks.get(j))); |
302 |
4 |
html.append("</span></div>"); |
303 |
|
} |
304 |
6 |
addBR = true; |
305 |
|
} |
306 |
|
} |
307 |
|
|
308 |
|
|
309 |
6 |
if (allDoc) { |
310 |
5 |
while (cursor < lines.length) { |
311 |
1 |
html.append("<div class=\"diffunmodifiedline\">"); |
312 |
1 |
String text = escape(lines[cursor]); |
313 |
1 |
if (text.equals("")) { |
314 |
0 |
text = " "; |
315 |
|
} |
316 |
1 |
html.append(text); |
317 |
1 |
html.append("</div>"); |
318 |
1 |
cursor++; |
319 |
|
} |
320 |
|
} |
321 |
6 |
html.append("</div>"); |
322 |
6 |
return html.toString(); |
323 |
|
} |
324 |
|
|
325 |
|
} |