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

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

Introduction

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

Prototype

public int size() 

Source Link

Document

Returns the number of deltas in this revision.

Usage

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

/**
 *  Creates a simple text string describing the added content.
 *
 *  @param context/*  w w  w .  ja  va 2  s.  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.ecyrd.jspwiki.diff.TraditionalDiffProvider.java

/**
 * Makes a diff using the BMSI utility package. We use our own diff printer,
 * which makes things easier./*w  ww .j  a  v a  2s  . c  o  m*/
 * 
 * @param ctx The WikiContext in which the diff should be made.
 * @param p1 The first string
 * @param p2 The second string.
 * 
 * @return Full HTML diff.
 */
public String makeDiffHtml(WikiContext ctx, String p1, String p2) {
    String diffResult = "";

    try {
        String[] first = Diff.stringToArray(TextUtil.replaceEntities(p1));
        String[] second = Diff.stringToArray(TextUtil.replaceEntities(p2));
        Revision rev = Diff.diff(first, second, new MyersDiff());

        if (rev == null || rev.size() == 0) {
            // No difference

            return "";
        }

        StringBuffer ret = new StringBuffer(rev.size() * 20); // Guessing how big it will become...

        ret.append("<table class=\"diff\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");
        rev.accept(new RevisionPrint(ctx, ret));
        ret.append("</table>\n");

        return ret.toString();
    } catch (DifferentiationFailedException e) {
        diffResult = "makeDiff failed with DifferentiationFailedException";
        log.error(diffResult, e);
    }

    return diffResult;
}

From source file:cn.vlabs.duckling.vwb.service.diff.impl.TraditionalDiffProvider.java

/**
 * Makes a diff using the BMSI utility package. We use our own diff printer,
 * which makes things easier./* w ww .j a va  2  s.  c  o m*/
 */
public String makeDiffHtml(VWBContext ctx, String p1, String p2) {
    String diffResult = "";

    try {
        String[] first = Diff.stringToArray(TextUtil.replaceEntities(p1));
        String[] second = Diff.stringToArray(TextUtil.replaceEntities(p2));
        Revision rev = Diff.diff(first, second, new MyersDiff());

        if (rev == null || rev.size() == 0) {
            // No difference

            return "";
        }

        StringBuffer ret = new StringBuffer(rev.size() * 20); // Guessing
        // how big
        // it will
        // become...

        ret.append("<table class=\"diff\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");
        rev.accept(new RevisionPrint(ctx, ret));
        ret.append("</table>\n");

        return ret.toString();
    } catch (DifferentiationFailedException e) {
        diffResult = "makeDiff failed with DifferentiationFailedException";
        log.error(diffResult, e);
    }

    return diffResult;
}

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 a2  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);
    }//  w  w w  .  j  ava  2  s.  c  o m
    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

private boolean compareFiles() throws DifferentiationFailedException, IOException, QVCSOperationException {
    CompareLineInfo[] fileA = buildLinesFromFile(inFileA);
    setFile1LineCount(fileA.length);//from  w w w . j  a v  a 2  s  . c om
    CompareLineInfo[] fileB = buildLinesFromFile(inFileB);
    setFile2LineCount(fileB.length);

    Revision apacheRevision = Diff.diff(fileA, fileB);

    if (apacheRevision.size() == 0) {
        // The files are identical.
        comparisonResultFlag = true;
    }

    // Create the edit script that describes the changes we found.
    writeEditScript(apacheRevision, fileA, fileB);
    return true;
}

From source file:com.ecyrd.jspwiki.diff.ContextualDiffProvider.java

/**
 * Do a colored diff of the two regions. This. is. serious. fun. ;-)
 *
 * @see com.ecyrd.jspwiki.diff.DiffProvider#makeDiffHtml(WikiContext, String, String)
 * /*from  w ww  . ja v a  2 s .c o  m*/
 * {@inheritDoc}
 */
public synchronized String makeDiffHtml(WikiContext ctx, String wikiOld, String wikiNew) {
    //
    // Sequencing handles lineterminator to <br /> and every-other consequtive space to a &nbsp;
    //
    String[] alpha = sequence(TextUtil.replaceEntities(wikiOld));
    String[] beta = sequence(TextUtil.replaceEntities(wikiNew));

    Revision rev = null;
    try {
        rev = Diff.diff(alpha, beta, new MyersDiff());
    } catch (DifferentiationFailedException dfe) {
        log.error("Diff generation failed", dfe);
        return "Error while creating version diff.";
    }

    int revSize = rev.size();

    StringBuffer sb = new StringBuffer();

    sb.append(m_diffStart);

    //
    // The MyersDiff is a bit dumb by converting a single line multi-word diff into a series
    // of Changes. The ChangeMerger pulls them together again...
    //
    ChangeMerger cm = new ChangeMerger(sb, alpha, revSize);

    rev.accept(cm);

    cm.shutdown();

    sb.append(m_diffEnd);

    return sb.toString();
}

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

protected void writeEditScript(Revision apacheRevision, CompareLineInfo[] fileA, CompareLineInfo[] fileB)
        throws QVCSOperationException {
    DataOutputStream outStream = null;
    try {/* w  w  w.  j  av a  2  s  .com*/
        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.apache.wiki.diff.FlowchartDiffProvider.java

private String createTextDiff(WikiContext ctx, String p1, String p2, StringBuffer buffy)
        throws DifferentiationFailedException {
    String[] first = Diff.stringToArray(TextUtil.replaceEntities(p1));
    String[] second = Diff.stringToArray(TextUtil.replaceEntities(p2));
    Revision rev = Diff.diff(first, second, new MyersDiff());

    if (rev == null || rev.size() == 0) {
        // No difference

        return "";
    }// www  . j  a v  a  2 s .c  o m

    // first version is empty page. Diff is called when article is created
    if (p1.equals("")) {
        return "";
    }

    // how big
    // it will
    // become...

    buffy.append("<table class=\"diff\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");
    rev.accept(new RevisionPrint(ctx, buffy));
    buffy.append("</table>\n");

    return buffy.toString();
}

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

private JMRevision buildRevision(Revision revision, Object[] orig, Object[] rev) {
    JMRevision result;/* 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;
}