Example usage for org.apache.commons.jrcs.diff Revision getDelta

List of usage examples for org.apache.commons.jrcs.diff Revision getDelta

Introduction

In this page you can find the example usage for org.apache.commons.jrcs.diff Revision getDelta.

Prototype

public Delta getDelta(int i) 

Source Link

Document

Retrieves a delta from this revision by position.

Usage

From source file:com.ecyrd.jspwiki.filters.SpamFilter.java

/**
 *  Creates a simple text string describing the added content.
 *
 *  @param context/*  w w  w .j av  a 2s .c  om*/
 *  @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);
    }

    //
    //  And author as well
    //

    if (page.getAuthor() != null) {
        change.append("\r\n" + page.getAuthor());
    }

    ch.m_change = change.toString();
    return ch;
}

From source file:com.qumasoft.guitools.compare.CompareFilesForGUI.java

private void deduceRowTypes(Revision apacheRev, CompareLineInfo[] fileA, CompareLineInfo[] fileB)
        throws QVCSOperationException {
    // First get all the deltas...
    Delta[] deltas = new Delta[apacheRev.size()];
    for (int i = 0; i < apacheRev.size(); i++) {
        deltas[i] = apacheRev.getDelta(i);
    }/* w  w w  . j  a  v a  2  s . co  m*/

    fileARowTypeArray = new byte[fileA.length];
    fileBRowTypeArray = new byte[fileB.length];

    fileADeltaForRowArray = new Delta[fileA.length];
    fileBDeltaForRowArray = new Delta[fileB.length];

    // Set all the rows to default to NORMAL.
    for (int i = 0; i < fileA.length; i++) {
        fileARowTypeArray[i] = ContentRow.ROWTYPE_NORMAL;
    }
    for (int i = 0; i < fileB.length; i++) {
        fileBRowTypeArray[i] = ContentRow.ROWTYPE_NORMAL;
    }
    for (Delta delta : deltas) {
        byte rowType;
        if (delta instanceof AddDelta) {
            rowType = ContentRow.ROWTYPE_INSERT;
            // We need to set the row type and delta here for fileA because the
            // for loop doesn't traverse any rows on fileA.
            if (delta.getOriginal().anchor() > 0) {
                fileARowTypeArray[delta.getOriginal().anchor() - 1] = ContentRow.ROWTYPE_NORMAL;
                fileADeltaForRowArray[delta.getOriginal().anchor() - 1] = delta;
            }
        } else if (delta instanceof ChangeDelta) {
            rowType = ContentRow.ROWTYPE_REPLACE;
        } else if (delta instanceof DeleteDelta) {
            rowType = ContentRow.ROWTYPE_DELETE;
            // We need to set the row type and delta here for fileB because the
            // for loop doesn't traverse any rows on fileB.
            if (delta.getRevised().anchor() > 0) {
                fileBRowTypeArray[delta.getRevised().anchor() - 1] = ContentRow.ROWTYPE_NORMAL;
                fileBDeltaForRowArray[delta.getRevised().anchor() - 1] = delta;
            }
        } else {
            throw new QVCSOperationException("Unknown delta type.");
        }
        @SuppressWarnings("unchecked")
        int firstA = delta.getOriginal().first();
        int lastA = delta.getOriginal().last();
        for (int j = firstA; j <= lastA; j++) {
            fileARowTypeArray[j] = rowType;
            fileADeltaForRowArray[j] = delta;
        }
        int firstB = delta.getRevised().first();
        int lastB = delta.getRevised().last();
        for (int j = firstB; j <= lastB; j++) {
            fileBRowTypeArray[j] = rowType;
            fileBDeltaForRowArray[j] = delta;
        }
    }
}

From source file:com.qumasoft.guitools.compare.ContentRow.java

private void deduceCharacterAnnotations(Revision differences, Byte[] sBytes) {
    // First get all the deltas...
    Delta[] deltas = new Delta[differences.size()];
    for (int i = 0; i < differences.size(); i++) {
        deltas[i] = differences.getDelta(i);
    }/*from   ww w.j  a va  2  s. com*/
    fileACharacterTypeArray = new byte[sBytes.length];
    // Set all the rows to default to NORMAL.
    for (int i = 0; i < sBytes.length; i++) {
        fileACharacterTypeArray[i] = ContentRow.ROWTYPE_NORMAL;
    }
    for (Delta characterDelta : deltas) {
        byte rType;
        if (characterDelta instanceof AddDelta) {
            rType = ContentRow.ROWTYPE_INSERT;
            rowHadAnnotations = true;
        } else if (characterDelta instanceof ChangeDelta) {
            rType = ContentRow.ROWTYPE_REPLACE;
            rowHadAnnotations = true;
        } else if (characterDelta instanceof DeleteDelta) {
            rType = ContentRow.ROWTYPE_DELETE;
            rowHadAnnotations = true;
        } else {
            continue; // this is goofy... and should never happen. We'll ignore the problem.
        }
        @SuppressWarnings("unchecked")
        int firstA = characterDelta.getOriginal().first();
        int lastA = characterDelta.getOriginal().last();
        for (int j = firstA; j <= lastA; j++) {
            fileACharacterTypeArray[j] = rType;
        }
    }
}

From source file:com.qumasoft.qvcslib.CompareFilesWithApacheDiff.java

protected void writeEditScript(Revision apacheRevision, CompareLineInfo[] fileA, CompareLineInfo[] fileB)
        throws QVCSOperationException {
    DataOutputStream outStream = null;
    try {/*from  w w  w.  ja  va  2  s  .  co m*/
        outStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));

        // Write the header
        CompareFilesEditHeader editHeader = new CompareFilesEditHeader();
        editHeader.setBaseFileSize(new Common32Long((int) inFileA.length()));
        editHeader.setTimeOfTarget(new CommonTime());
        editHeader.write(outStream);

        int count = apacheRevision.size();
        for (int index = 0; index < count; index++) {
            Delta delta = apacheRevision.getDelta(index);
            formatEditScript(delta, outStream, fileA);
        }
    } catch (IOException e) {
        throw new QVCSOperationException("IO Exception in writeEditScript() " + e.getLocalizedMessage());
    } finally {
        if (outStream != null) {
            try {
                outStream.close();
            } catch (IOException ex) {
                Logger.getLogger(CompareFilesWithApacheDiff.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:org.jmeld.diff.MyersDiff.java

private JMRevision buildRevision(Revision revision, Object[] orig, Object[] rev) {
    JMRevision result;/*from  w  ww . j  a  v a2  s. c o  m*/
    Delta delta;
    Chunk original;
    Chunk revised;

    if (orig == null) {
        throw new IllegalArgumentException("original sequence is null");
    }

    if (rev == null) {
        throw new IllegalArgumentException("revised sequence is null");
    }

    result = new JMRevision(orig, rev);
    for (int i = 0; i < revision.size(); i++) {
        delta = revision.getDelta(i);
        original = delta.getOriginal();
        revised = delta.getRevised();

        result.add(new JMDelta(new JMChunk(original.anchor(), original.size()),
                new JMChunk(revised.anchor(), revised.size())));
    }

    return result;
}