1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.annotation.maintainer.internal

File CharacterDiffService.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

12
24
2
1
106
53
9
0.38
12
2
4.5

Classes

Class Line # Actions
CharacterDiffService 43 24 0% 9 3
0.9210526392.1%
 

Contributing tests

This file is covered by 49 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   
21    package org.xwiki.annotation.maintainer.internal;
22   
23    import java.util.ArrayList;
24    import java.util.Collection;
25    import java.util.List;
26   
27    import javax.inject.Singleton;
28   
29    import org.incava.util.diff.Diff;
30    import org.incava.util.diff.Difference;
31    import org.xwiki.annotation.maintainer.DiffService;
32    import org.xwiki.annotation.maintainer.XDelta;
33    import org.xwiki.component.annotation.Component;
34   
35    /**
36    * DiffService implementation providing character level differences between content.
37    *
38    * @version $Id: d4150af39972c8c3f8eed604747c6f82d853b90e $
39    * @since 2.3M1
40    */
41    @Component(hints = {"default", "character" })
42    @Singleton
 
43    public class CharacterDiffService implements DiffService
44    {
 
45  49 toggle @Override
46    public Collection<XDelta> getDifferences(String previous, String current)
47    {
48    // get differences at character level
49    // FIXME: do we want at character level or we'd better get word level, to have it working faster
50  49 Collection<XDelta> deltas = new ArrayList<XDelta>();
51  49 List<Character> previousContent = new ArrayList<Character>();
52  2324 for (int i = 0; i < previous.length(); ++i) {
53  2275 previousContent.add(previous.charAt(i));
54    }
55  49 List<Character> currentContent = new ArrayList<Character>();
56  2386 for (int i = 0; i < current.length(); ++i) {
57  2337 currentContent.add(current.charAt(i));
58    }
59  49 Diff<Character> diff = new Diff<Character>(previousContent, currentContent);
60    // prepare the XDeltas for all diffs
61  49 for (Difference it : diff.diff()) {
62  62 XDelta delta = getDelta(previous, current, it);
63  62 if (delta != null) {
64  62 deltas.add(delta);
65    }
66    }
67  49 return deltas;
68    }
69   
70    /**
71    * Helper function to prepare an {@link XDelta} object for the passed content.
72    *
73    * @param previous the previous content
74    * @param current the current content
75    * @param diff the difference to prepare the XDelta object for
76    * @return an {@link XDelta} object corresponding to {@code diff}
77    */
 
78  62 toggle private XDelta getDelta(String previous, String current, Difference diff)
79    {
80  62 int position;
81  62 String original = "";
82  62 String modified = "";
83   
84    // deleted is from previous, added is in current
85   
86  62 if (diff.getDeletedStart() == Difference.NONE || diff.getAddedStart() == Difference.NONE) {
87    // this diff doesn't make sense, ignore it
88  0 return null;
89    }
90   
91  62 position = diff.getDeletedStart();
92   
93    // the content that was deleted
94  62 if (diff.getDeletedEnd() != Difference.NONE) {
95  46 original = previous.substring(diff.getDeletedStart(), diff.getDeletedEnd() + 1);
96    }
97   
98    // the content that was added
99  62 if (diff.getAddedEnd() != Difference.NONE) {
100  43 modified = current.substring(diff.getAddedStart(), diff.getAddedEnd() + 1);
101    }
102   
103    // else return the built chunk
104  62 return new ChunksXDelta(position, original, modified);
105    }
106    }