Search in sources :

Example 1 with AddDelta

use of org.suigeneris.jrcs.diff.delta.AddDelta 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 2 with AddDelta

use of org.suigeneris.jrcs.diff.delta.AddDelta in project jspwiki by apache.

the class SpamFilter method getChange.

/**
 *  Creates a simple text string describing the added content.
 *
 *  @param context
 *  @param newText
 *  @return Empty string, if there is no change.
 */
private static Change getChange(WikiContext context, String newText) {
    WikiPage page = context.getPage();
    StringBuffer change = new StringBuffer();
    WikiEngine engine = context.getEngine();
    // Get current page version
    Change ch = new Change();
    try {
        String oldText = engine.getPureText(page.getName(), WikiProvider.LATEST_VERSION);
        String[] first = Diff.stringToArray(oldText);
        String[] second = Diff.stringToArray(newText);
        Revision rev = Diff.diff(first, second, new MyersDiff());
        if (rev == null || rev.size() == 0) {
            return ch;
        }
        for (int i = 0; i < rev.size(); i++) {
            Delta d = rev.getDelta(i);
            if (d instanceof AddDelta) {
                d.getRevised().toString(change, "", "\r\n");
                ch.m_adds++;
            } else if (d instanceof ChangeDelta) {
                d.getRevised().toString(change, "", "\r\n");
                ch.m_adds++;
            } else if (d instanceof DeleteDelta) {
                ch.m_removals++;
            }
        }
    } catch (DifferentiationFailedException e) {
        log.error("Diff failed", e);
    }
    // 
    // Don't forget to include the change note, too
    // 
    String changeNote = (String) page.getAttribute(WikiPage.CHANGENOTE);
    if (changeNote != null) {
        change.append("\r\n");
        change.append(changeNote);
    }
    // 
    if (page.getAuthor() != null) {
        change.append("\r\n" + page.getAuthor());
    }
    ch.m_change = change.toString();
    return ch;
}
Also used : ChangeDelta(org.suigeneris.jrcs.diff.delta.ChangeDelta) DifferentiationFailedException(org.suigeneris.jrcs.diff.DifferentiationFailedException) DeleteDelta(org.suigeneris.jrcs.diff.delta.DeleteDelta) WikiPage(org.apache.wiki.WikiPage) MyersDiff(org.suigeneris.jrcs.diff.myers.MyersDiff) Revision(org.suigeneris.jrcs.diff.Revision) DeleteDelta(org.suigeneris.jrcs.diff.delta.DeleteDelta) Delta(org.suigeneris.jrcs.diff.delta.Delta) AddDelta(org.suigeneris.jrcs.diff.delta.AddDelta) ChangeDelta(org.suigeneris.jrcs.diff.delta.ChangeDelta) AddDelta(org.suigeneris.jrcs.diff.delta.AddDelta) WikiEngine(org.apache.wiki.WikiEngine)

Aggregations

DifferentiationFailedException (org.suigeneris.jrcs.diff.DifferentiationFailedException)2 Revision (org.suigeneris.jrcs.diff.Revision)2 AddDelta (org.suigeneris.jrcs.diff.delta.AddDelta)2 DeleteDelta (org.suigeneris.jrcs.diff.delta.DeleteDelta)2 StringTokenizer (java.util.StringTokenizer)1 WikiEngine (org.apache.wiki.WikiEngine)1 WikiPage (org.apache.wiki.WikiPage)1 PatchFailedException (org.suigeneris.jrcs.diff.PatchFailedException)1 ChangeDelta (org.suigeneris.jrcs.diff.delta.ChangeDelta)1 Chunk (org.suigeneris.jrcs.diff.delta.Chunk)1 Delta (org.suigeneris.jrcs.diff.delta.Delta)1 MyersDiff (org.suigeneris.jrcs.diff.myers.MyersDiff)1 InvalidFileFormatException (org.suigeneris.jrcs.rcs.InvalidFileFormatException)1 ToString (org.suigeneris.jrcs.util.ToString)1