1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.diff

File DiffPlugin.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

64
134
10
1
325
223
44
0.33
13.4
10
4.4

Classes

Class Line # Actions
DiffPlugin 44 134 0% 44 28
0.8653846486.5%
 

Contributing tests

This file is covered by 13 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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 $Id: a206ffd630ea7f24f52bd37ec3dc39610711991d $
41    * @deprecated since 4.1 use diff service
42    */
43    @Deprecated
 
44    public class DiffPlugin extends XWikiDefaultPlugin
45    {
46    /**
47    * @param name the plugin name, usually ignored, since plugins have a fixed name
48    * @param className the name of this class, ignored
49    * @param context the current request context
50    */
 
51  14 toggle public DiffPlugin(String name, String className, XWikiContext context)
52    {
53  14 super(name, className, context);
54    }
55   
 
56  2 toggle @Override
57    public String getName()
58    {
59  2 return "diff";
60    }
61   
 
62  2 toggle @Override
63    public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context)
64    {
65  2 return new DiffPluginApi((DiffPlugin) plugin, context);
66    }
67   
68    /**
69    * Return a list of Delta objects representing line differences in text1 and text2
70    *
71    * @param text1 original content
72    * @param text2 revised content
73    * @return list of Delta objects
74    */
 
75  8 toggle 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   
 
91  18 toggle 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  35 toggle protected String escape(String text)
101    {
102  35 return XMLUtils.escape(text);
103    }
104   
105    /**
106    * Return a list of Delta objects representing word differences in text1 and text2
107    *
108    * @param text1 original content
109    * @param text2 revised content
110    * @return list of Delta objects
111    */
 
112  10 toggle 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    * Return an html blocks representing word diffs between text1 and text2
126    *
127    * @param text1 original content
128    * @param text2 revised content
129    * @return list of Delta objects
130    */
 
131  6 toggle 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    // First we fill in all text that has not been changed
151  20 while (cursor < position) {
152  10 html.append(escape(words[cursor]));
153  10 html.append(" ");
154  10 cursor++;
155    }
156    // Then we fill in what has been removed
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    // Then we fill in what has been added
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    // First we fill in all text that has not been changed
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    * Return an html blocks representing line diffs between text1 and text2
204    *
205    * @param text1 original content
206    * @param text2 revised content
207    * @return list of Delta objects
208    */
 
209  4 toggle public String getDifferencesAsHTML(String text1, String text2) throws XWikiException
210    {
211  4 return getDifferencesAsHTML(text1, text2, true);
212    }
213   
214    /**
215    * Return an html blocks representing line diffs between text1 and text2
216    *
217    * @param text1 original content
218    * @param text2 revised content
219    * @param allDoc show all document
220    * @return list of Delta objects
221    */
 
222  6 toggle 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    // First we fill in all text that has not been changed
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 = "&nbsp;";
250    }
251  2 html.append(text);
252  2 html.append("</div>");
253    }
254  2 cursor++;
255    }
256   
257    // Then we fill in what has been removed
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    // if (j>0)
272    // html.append("<br/>");
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    // Then we fill in what has been added
294  7 if (rev.size() > 0) {
295  6 List chunks = rev.chunk();
296  10 for (int j = j1; j < chunks.size(); j++) {
297    // if (j>0)
298    // html.append("<br/>");
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    // First we fill in all text that has not been changed
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 = "&nbsp;";
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    }