Example usage for org.apache.commons.jrcs.diff Delta getOriginal

List of usage examples for org.apache.commons.jrcs.diff Delta getOriginal

Introduction

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

Prototype

public Chunk getOriginal() 

Source Link

Document

Accessor method to return the chunk representing the original sequence of items

Usage

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);
    }/*from  w  w w . j  a  v a  2  s.com*/

    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.qvcslib.CompareFilesWithApacheDiff.java

private int computeDeletedByteCount(Delta delta) throws UnsupportedEncodingException {
    // This should be the byte count of the original chunk.
    @SuppressWarnings("unchecked")
    List<CompareLineInfo> originalChunk = delta.getOriginal().chunk();
    int seekPosition = originalChunk.get(0).getLineSeekPosition();
    CompareLineInfo lastLine = originalChunk.get(originalChunk.size() - 1);
    int lastLineSeekStart = lastLine.getLineSeekPosition();
    byte[] lastLineAsByteArray = lastLine.getLineString().getBytes(UTF8);
    int lastLineEnd = lastLineSeekStart + lastLineAsByteArray.length;
    return lastLineEnd - seekPosition;
}

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

void moveToCurrentDifference() {
    // Recall that the changes vector is in reverse order, the LAST changes are first, the first
    // changes are last.
    Delta delta = compareFilesForGUI.getDelta(currentDifferenceIndex);

    int file1LineNumber = delta.getOriginal().first() + 1;
    int i;/*w  w w .j  av a 2s .  com*/
    int j = -1;
    int maximumRow = file1ContentsListModel.size();
    ContentRow row;
    for (i = 0; (i + file1LineNumber) < maximumRow; i++) {
        row = file1ContentsListModel.elementAt(file1LineNumber + i);
        if (row.getDelta() == delta) {
            j = i;
            break;
        }
    }

    file1ContentsListModel.setCurrentDifferenceIndex(file1LineNumber + j);
    file2ContentsListModel.setCurrentDifferenceIndex(file1LineNumber + j);
    positionViewPort(file1LineNumber + j);

    setNextPreviousActionStates(compareFilesForGUI.getNumberOfChanges());
}

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

private void formatEditScript(Delta delta, DataOutputStream outStream, CompareLineInfo[] fileA)
        throws QVCSOperationException {
    try {//  www .j  a  va 2s  .  c  o  m
        short editType;
        int seekPosition = -1;
        int deletedByteCount = 0;
        int insertedByteCount = 0;
        byte[] secondFileByteBuffer = null;
        if (delta instanceof ChangeDelta) {
            CompareLineInfo originalStartingLine = fileA[delta.getOriginal().anchor()];
            seekPosition = originalStartingLine.getLineSeekPosition();
            editType = CompareFilesEditInformation.QVCS_EDIT_REPLACE;
            deletedByteCount = computeDeletedByteCount(delta);
            insertedByteCount = computeInsertedByteCount(delta);
            secondFileByteBuffer = computeSecondFileByteBufferForInsert(delta, insertedByteCount);
        } else if (delta instanceof AddDelta) {
            int anchor = delta.getOriginal().anchor();
            if (anchor == 0) {
                CompareLineInfo originalStartingLine = fileA[delta.getOriginal().anchor()];
                seekPosition = originalStartingLine.getLineSeekPosition();
            } else {
                CompareLineInfo originalStartingLine = fileA[delta.getOriginal().anchor() - 1];
                byte[] lineAsByteArray = originalStartingLine.getLineString().getBytes(UTF8);
                seekPosition = originalStartingLine.getLineSeekPosition() + lineAsByteArray.length;
            }
            editType = CompareFilesEditInformation.QVCS_EDIT_INSERT;
            insertedByteCount = computeInsertedByteCount(delta);
            AddDelta insertDelta = (AddDelta) delta;
            secondFileByteBuffer = computeSecondFileByteBufferForInsert(insertDelta, insertedByteCount);
        } else if (delta instanceof DeleteDelta) {
            CompareLineInfo originalStartingLine = fileA[delta.getOriginal().anchor()];
            seekPosition = originalStartingLine.getLineSeekPosition();
            editType = CompareFilesEditInformation.QVCS_EDIT_DELETE;
            deletedByteCount = computeDeletedByteCount(delta);
        } else {
            throw new QVCSOperationException("Internal error -- invalid edit type");
        }
        CompareFilesEditInformation editInfo = new CompareFilesEditInformation(editType, seekPosition,
                deletedByteCount, insertedByteCount);

        switch (editType) {
        case CompareFilesEditInformation.QVCS_EDIT_DELETE:
            editInfo.write(outStream);
            break;
        case CompareFilesEditInformation.QVCS_EDIT_INSERT:
        case CompareFilesEditInformation.QVCS_EDIT_REPLACE:
            editInfo.write(outStream);
            outStream.write(secondFileByteBuffer, 0, insertedByteCount);
            break;
        default:
            throw new QVCSOperationException("Internal error -- invalid edit type");
        }
    } catch (IOException e) {
        throw new QVCSOperationException("IOException in formatEditScript() " + e.getLocalizedMessage());
    }
}

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);
    }// w w w. j a v a  2  s. c om
    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:org.jmeld.diff.MyersDiff.java

private JMRevision buildRevision(Revision revision, Object[] orig, Object[] rev) {
    JMRevision result;//  w  w  w  .  j a v a 2s . c om
    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;
}