Search in sources :

Example 6 with Chunk

use of org.suigeneris.jrcs.diff.delta.Chunk in project xwiki-platform by xwiki.

the class DiffTest method testSimpleLineDiff2.

@Test
public void testSimpleLineDiff2() throws XWikiException {
    String text1 = "A\nB\nC";
    String text2 = "A\nB B\nC";
    List diffs = this.plugin.getDifferencesAsList(text1, text2);
    assertEquals("There should be one difference", 1, diffs.size());
    Delta delta = (Delta) diffs.get(0);
    Chunk orig = delta.getOriginal();
    Chunk revised = delta.getRevised();
    assertEquals("Original should be", "B", orig.toString());
    assertEquals("Revised should be", "B B", revised.toString());
}
Also used : Delta(org.suigeneris.jrcs.diff.delta.Delta) List(java.util.List) Chunk(org.suigeneris.jrcs.diff.delta.Chunk) Test(org.junit.Test)

Example 7 with Chunk

use of org.suigeneris.jrcs.diff.delta.Chunk in project xwiki-platform by xwiki.

the class XWikiPatchUtils method patch.

/**
 * From {@link org.suigeneris.jrcs.rcs.impl.Node#patch(List, boolean)}.
 *
 * @param orig - text to patch, List<String> of lines.
 * @param diff - diff to patch, {@link Diff} format
 * @throws InvalidFileFormatException if diff is incorrect
 * @throws PatchFailedException if error in patching
 */
public static void patch(List<String> orig, String diff) throws InvalidFileFormatException, PatchFailedException {
    Revision revision = new Revision();
    Object[] lines = ToString.stringToArray(diff);
    for (int it = 0; it < lines.length; it++) {
        String cmd = lines[it].toString();
        if (cmd.length() == 0) {
            break;
        }
        java.util.StringTokenizer t = new StringTokenizer(cmd, "ad ", true);
        char action;
        int n;
        int count;
        try {
            action = t.nextToken().charAt(0);
            n = Integer.parseInt(t.nextToken());
            // skip the space
            t.nextToken();
            count = Integer.parseInt(t.nextToken());
        } catch (Exception e) {
            throw new InvalidFileFormatException("line:" + ":" + e.getClass().getName(), e);
        }
        if (action == 'd') {
            revision.addDelta(new DeleteDelta(new Chunk(n - 1, count)));
        } else if (action == 'a') {
            revision.addDelta(new AddDelta(n, new Chunk(getTextLines(lines, it + 1, it + 1 + count), 0, count, n - 1)));
            it += count;
        } else {
            throw new InvalidFileFormatException();
        }
    }
    revision.applyTo(orig);
}
Also used : DeleteDelta(org.suigeneris.jrcs.diff.delta.DeleteDelta) ToString(org.suigeneris.jrcs.util.ToString) Chunk(org.suigeneris.jrcs.diff.delta.Chunk) InvalidFileFormatException(org.suigeneris.jrcs.rcs.InvalidFileFormatException) PatchFailedException(org.suigeneris.jrcs.diff.PatchFailedException) DifferentiationFailedException(org.suigeneris.jrcs.diff.DifferentiationFailedException) InvalidFileFormatException(org.suigeneris.jrcs.rcs.InvalidFileFormatException) StringTokenizer(java.util.StringTokenizer) Revision(org.suigeneris.jrcs.diff.Revision) StringTokenizer(java.util.StringTokenizer) AddDelta(org.suigeneris.jrcs.diff.delta.AddDelta)

Example 8 with Chunk

use of org.suigeneris.jrcs.diff.delta.Chunk in project xwiki-platform by xwiki.

the class DiffTest method testSimpleLineDiff.

@Test
public void testSimpleLineDiff() throws XWikiException {
    String text1 = "A";
    String text2 = "A B";
    List diffs = this.plugin.getDifferencesAsList(text1, text2);
    assertEquals("There should be one difference", 1, diffs.size());
    Delta delta = (Delta) diffs.get(0);
    Chunk orig = delta.getOriginal();
    Chunk revised = delta.getRevised();
    assertEquals("Original should be", "A", orig.toString());
    assertEquals("Revised should be", "A B", revised.toString());
}
Also used : Delta(org.suigeneris.jrcs.diff.delta.Delta) List(java.util.List) Chunk(org.suigeneris.jrcs.diff.delta.Chunk) Test(org.junit.Test)

Example 9 with Chunk

use of org.suigeneris.jrcs.diff.delta.Chunk in project xwiki-platform by xwiki.

the class DiffTest method testSimpleWordDiff4.

@Test
public void testSimpleWordDiff4() throws XWikiException {
    String text1 = "I love Paris and I like London";
    String text2 = "I love London and I like Paris";
    List diffs = this.plugin.getWordDifferencesAsList(text1, text2);
    assertEquals("There should be two differences", 2, diffs.size());
    Delta delta1 = (Delta) diffs.get(0);
    Chunk orig1 = delta1.getOriginal();
    Chunk revised1 = delta1.getRevised();
    Delta delta2 = (Delta) diffs.get(1);
    Chunk orig2 = delta2.getOriginal();
    Chunk revised2 = delta2.getRevised();
    assertEquals("Original 1 should be", "Paris", orig1.toString());
    assertEquals("Revised 1 should be", "London", revised1.toString());
    assertEquals("Original 2 should be", "London", orig2.toString());
    assertEquals("Revised 2 should be", "Paris", revised2.toString());
}
Also used : Delta(org.suigeneris.jrcs.diff.delta.Delta) List(java.util.List) Chunk(org.suigeneris.jrcs.diff.delta.Chunk) Test(org.junit.Test)

Aggregations

Chunk (org.suigeneris.jrcs.diff.delta.Chunk)9 List (java.util.List)8 Delta (org.suigeneris.jrcs.diff.delta.Delta)8 Test (org.junit.Test)6 ToString (org.suigeneris.jrcs.util.ToString)3 ArrayList (java.util.ArrayList)2 StringTokenizer (java.util.StringTokenizer)1 DifferentiationFailedException (org.suigeneris.jrcs.diff.DifferentiationFailedException)1 PatchFailedException (org.suigeneris.jrcs.diff.PatchFailedException)1 Revision (org.suigeneris.jrcs.diff.Revision)1 AddDelta (org.suigeneris.jrcs.diff.delta.AddDelta)1 DeleteDelta (org.suigeneris.jrcs.diff.delta.DeleteDelta)1 InvalidFileFormatException (org.suigeneris.jrcs.rcs.InvalidFileFormatException)1