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

File XWikiPatchUtils.java

 

Coverage histogram

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

Code metrics

10
29
5
1
132
69
11
0.38
5.8
5
2.2

Classes

Class Line # Actions
XWikiPatchUtils 41 29 0% 11 6
0.863636486.4%
 

Contributing tests

This file is covered by 6 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.doc.rcs;
21   
22    import java.util.List;
23    import java.util.StringTokenizer;
24   
25    import org.suigeneris.jrcs.diff.Diff;
26    import org.suigeneris.jrcs.diff.DifferentiationFailedException;
27    import org.suigeneris.jrcs.diff.PatchFailedException;
28    import org.suigeneris.jrcs.diff.Revision;
29    import org.suigeneris.jrcs.diff.delta.AddDelta;
30    import org.suigeneris.jrcs.diff.delta.Chunk;
31    import org.suigeneris.jrcs.diff.delta.DeleteDelta;
32    import org.suigeneris.jrcs.rcs.InvalidFileFormatException;
33    import org.suigeneris.jrcs.util.ToString;
34   
35    /**
36    * Diff and patch utility.
37    *
38    * @version $Id: 72e3426860aaf2383f3ecf446ceda2338ddede2d $
39    * @since 1.2M1
40    */
 
41    public class XWikiPatchUtils
42    {
43    /** prevent to create utility class. */
 
44  0 toggle private XWikiPatchUtils()
45    {
46    }
47   
48    /**
49    * @param orig - original text
50    * @param rev - new text
51    * @return diff in JRCS format
52    * @throws DifferentiationFailedException if error when creating diff
53    */
 
54  380 toggle public static String getDiff(Object[] orig, Object[] rev) throws DifferentiationFailedException
55    {
56  380 return Diff.diff(orig, rev).toRCSString();
57    }
58   
59    /**
60    * @param orig - original text
61    * @param rev - new text
62    * @return diff in JRCS format
63    * @throws DifferentiationFailedException if error when creating diff
64    */
 
65  380 toggle public static String getDiff(String orig, String rev) throws DifferentiationFailedException
66    {
67  380 return getDiff(ToString.stringToArray(orig), ToString.stringToArray(rev));
68    }
69   
70    /**
71    * From {@link org.suigeneris.jrcs.rcs.impl.Node#patch(List, boolean)}.
72    *
73    * @param orig - text to patch, List<String> of lines.
74    * @param diff - diff to patch, {@link Diff} format
75    * @throws InvalidFileFormatException if diff is incorrect
76    * @throws PatchFailedException if error in patching
77    */
 
78  37 toggle public static void patch(List<String> orig, String diff)
79    throws InvalidFileFormatException, PatchFailedException
80    {
81  37 Revision revision = new Revision();
82  37 Object[] lines = ToString.stringToArray(diff);
83  198 for (int it = 0; it < lines.length; it++) {
84  161 String cmd = lines[it].toString();
85  161 if (cmd.length() == 0) {
86  0 break;
87    }
88   
89  161 java.util.StringTokenizer t = new StringTokenizer(cmd, "ad ", true);
90  161 char action;
91  161 int n;
92  161 int count;
93   
94  161 try {
95  161 action = t.nextToken().charAt(0);
96  161 n = Integer.parseInt(t.nextToken());
97    // skip the space
98  161 t.nextToken();
99  161 count = Integer.parseInt(t.nextToken());
100    } catch (Exception e) {
101  0 throw new InvalidFileFormatException("line:" + ":" + e.getClass().getName(),
102    e);
103    }
104   
105  161 if (action == 'd') {
106  83 revision.addDelta(new DeleteDelta(new Chunk(n - 1, count)));
107  78 } else if (action == 'a') {
108  78 revision.addDelta(new AddDelta(n, new Chunk(getTextLines(lines,
109    it + 1, it + 1 + count), 0, count, n - 1)));
110  78 it += count;
111    } else {
112  0 throw new InvalidFileFormatException();
113    }
114    }
115  37 revision.applyTo(orig);
116    }
117   
118    /**
119    * @param lines - some text
120    * @param from - from that line
121    * @param to - to that line
122    * @return selected lines of text
123    */
 
124  78 toggle private static Object[] getTextLines(Object[] lines, int from, int to)
125    {
126  78 Object[] ret = new Object[to - from + 1];
127  164 for (int i = from; i < to; i++) {
128  86 ret[i - from] = lines[i];
129    }
130  78 return ret;
131    }
132    }