Example usage for org.apache.commons.jrcs.diff.myers MyersDiff MyersDiff

List of usage examples for org.apache.commons.jrcs.diff.myers MyersDiff MyersDiff

Introduction

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

Prototype

public MyersDiff() 

Source Link

Document

Constructs an instance of the Myers differencing algorithm.

Usage

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.//from   w w  w  .j a v  a 2s  . c om
 */
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.ecyrd.jspwiki.diff.TraditionalDiffProvider.java

/**
 * Makes a diff using the BMSI utility package. We use our own diff printer,
 * which makes things easier.//from  w w w  .  j  av 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: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  ww w  .  j  a v  a  2s .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.ecyrd.jspwiki.filters.SpamFilter.java

/**
 *  Creates a simple text string describing the added content.
 *
 *  @param context//w w w .ja  v  a 2s  .  co  m
 *  @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: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 "";
    }/*from www  .  ja 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:uk.ac.cam.caret.sakai.rwiki.tool.bean.GenericDiffBean.java

/**
 * initialise the bean. To be called after the leftContent and rightContent
 * have been set.//from   w  ww  .j ava  2  s. co  m
 */
public void init() {
    MyersDiff diffAlgorithm = new MyersDiff();

    try {
        objectifiedLeft = leftContent.split("\n");

        objectifiedRight = rightContent.split("\n");

        difference = diffAlgorithm.diff(objectifiedLeft, objectifiedRight);

    } catch (DifferentiationFailedException e) {
        // Quite why JRCS has this I don't know!
        throw new RuntimeException("DifferentiationFailedException occured: " + "This should never happen!", e);
    }

}