Search in sources :

Example 1 with Delta

use of org.suigeneris.jrcs.diff.delta.Delta in project xwiki-platform by xwiki.

the class DiffPlugin method getDifferencesAsHTML.

/**
 * Return an html blocks representing line diffs between text1 and text2
 *
 * @param text1 original content
 * @param text2 revised content
 * @param allDoc show all document
 * @return list of Delta objects
 */
public String getDifferencesAsHTML(String text1, String text2, boolean allDoc) throws XWikiException {
    StringBuilder html = new StringBuilder("<div class=\"diff\">");
    if (text1 == null) {
        text1 = "";
    }
    if (text2 == null) {
        text2 = "";
    }
    List list = getDifferencesAsList(text1, text2);
    String[] lines = ToString.stringToArray(text1);
    int cursor = 0;
    boolean addBR = false;
    for (int i = 0; i < list.size(); i++) {
        if (addBR) {
            addBR = false;
        }
        Delta delta = (Delta) list.get(i);
        int position = delta.getOriginal().anchor();
        // First we fill in all text that has not been changed
        while (cursor < position) {
            if (allDoc) {
                html.append("<div class=\"diffunmodifiedline\">");
                String text = escape(lines[cursor]);
                if (text.equals("")) {
                    text = "&nbsp;";
                }
                html.append(text);
                html.append("</div>");
            }
            cursor++;
        }
        // Then we fill in what has been removed
        Chunk orig = delta.getOriginal();
        Chunk rev = delta.getRevised();
        int j1 = 0;
        if (orig.size() > 0) {
            List chunks = orig.chunk();
            int j2 = 0;
            for (int j = 0; j < chunks.size(); j++) {
                String origline = (String) chunks.get(j);
                if (origline.equals("")) {
                    cursor++;
                    continue;
                }
                // if (j>0)
                // html.append("<br/>");
                List revchunks = rev.chunk();
                String revline = "";
                while ("".equals(revline)) {
                    revline = (j2 >= revchunks.size()) ? null : (String) revchunks.get(j2);
                    j2++;
                    j1++;
                }
                if (revline != null) {
                    html.append(getWordDifferencesAsHTML(origline, revline));
                } else {
                    html.append("<div class=\"diffmodifiedline\">");
                    html.append("<span class=\"diffremoveword\">");
                    html.append(escape(origline));
                    html.append("</span></div>");
                }
                addBR = true;
                cursor++;
            }
        }
        // Then we fill in what has been added
        if (rev.size() > 0) {
            List chunks = rev.chunk();
            for (int j = j1; j < chunks.size(); j++) {
                // if (j>0)
                // html.append("<br/>");
                html.append("<div class=\"diffmodifiedline\">");
                html.append("<span class=\"diffaddword\">");
                html.append(escape((String) chunks.get(j)));
                html.append("</span></div>");
            }
            addBR = true;
        }
    }
    // First we fill in all text that has not been changed
    if (allDoc) {
        while (cursor < lines.length) {
            html.append("<div class=\"diffunmodifiedline\">");
            String text = escape(lines[cursor]);
            if (text.equals("")) {
                text = "&nbsp;";
            }
            html.append(text);
            html.append("</div>");
            cursor++;
        }
    }
    html.append("</div>");
    return html.toString();
}
Also used : Delta(org.suigeneris.jrcs.diff.delta.Delta) ArrayList(java.util.ArrayList) List(java.util.List) ToString(org.suigeneris.jrcs.util.ToString) Chunk(org.suigeneris.jrcs.diff.delta.Chunk)

Example 2 with Delta

use of org.suigeneris.jrcs.diff.delta.Delta in project xwiki-platform by xwiki.

the class DiffPlugin method getWordDifferencesAsHTML.

/**
 * Return an html blocks representing word diffs between text1 and text2
 *
 * @param text1 original content
 * @param text2 revised content
 * @return list of Delta objects
 */
public String getWordDifferencesAsHTML(String text1, String text2) throws XWikiException {
    text1 = "~~PLACEHOLDER~~" + text1 + "~~PLACEHOLDER~~";
    text2 = "~~PLACEHOLDER~~" + text2 + "~~PLACEHOLDER~~";
    StringBuilder html = new StringBuilder("<div class=\"diffmodifiedline\">");
    List list = getWordDifferencesAsList(text1, text2);
    String[] words = StringUtils.splitPreserveAllTokens(text1, ' ');
    int cursor = 0;
    boolean addSpace = false;
    for (int i = 0; i < list.size(); i++) {
        if (addSpace) {
            html.append(" ");
            addSpace = false;
        }
        Delta delta = (Delta) list.get(i);
        int position = delta.getOriginal().anchor();
        // First we fill in all text that has not been changed
        while (cursor < position) {
            html.append(escape(words[cursor]));
            html.append(" ");
            cursor++;
        }
        // Then we fill in what has been removed
        Chunk orig = delta.getOriginal();
        if (orig.size() > 0) {
            html.append("<span class=\"diffremoveword\">");
            List chunks = orig.chunk();
            for (int j = 0; j < chunks.size(); j++) {
                if (j > 0) {
                    html.append(" ");
                }
                html.append(escape((String) chunks.get(j)));
                cursor++;
            }
            html.append("</span>");
            addSpace = true;
        }
        // Then we fill in what has been added
        Chunk rev = delta.getRevised();
        if (rev.size() > 0) {
            html.append("<span class=\"diffaddword\">");
            List chunks = rev.chunk();
            for (int j = 0; j < chunks.size(); j++) {
                if (j > 0) {
                    html.append(" ");
                }
                html.append(escape((String) chunks.get(j)));
            }
            html.append("</span>");
            addSpace = true;
        }
    }
    // First we fill in all text that has not been changed
    while (cursor < words.length) {
        if (addSpace) {
            html.append(" ");
        }
        html.append(escape(words[cursor]));
        addSpace = true;
        cursor++;
    }
    html.append("</div>");
    return html.toString().replaceAll("~~PLACEHOLDER~~", "");
}
Also used : Delta(org.suigeneris.jrcs.diff.delta.Delta) ArrayList(java.util.ArrayList) List(java.util.List) ToString(org.suigeneris.jrcs.util.ToString) Chunk(org.suigeneris.jrcs.diff.delta.Chunk)

Example 3 with Delta

use of org.suigeneris.jrcs.diff.delta.Delta in project xwiki-platform by xwiki.

the class DiffTest method testSimpleWordDiff2.

@Test
public void testSimpleWordDiff2() throws XWikiException {
    String text1 = "I love Paris and London";
    String text2 = "I live in Paris and London";
    List diffs = this.plugin.getWordDifferencesAsList(text1, text2);
    assertEquals("There should be two differences", 1, diffs.size());
    Delta delta1 = (Delta) diffs.get(0);
    Chunk orig1 = delta1.getOriginal();
    Chunk revised1 = delta1.getRevised();
    assertEquals("Original 1 should be", "love", orig1.toString());
    assertEquals("Revised 1 should be", "livein", revised1.toString());
}
Also used : Delta(org.suigeneris.jrcs.diff.delta.Delta) List(java.util.List) Chunk(org.suigeneris.jrcs.diff.delta.Chunk) Test(org.junit.Test)

Example 4 with Delta

use of org.suigeneris.jrcs.diff.delta.Delta in project xwiki-platform by xwiki.

the class DiffTest method testSimpleWordDiff.

@Test
public void testSimpleWordDiff() throws XWikiException {
    String text1 = "I love Paris";
    String text2 = "I live in Paris";
    List diffs = this.plugin.getWordDifferencesAsList(text1, text2);
    assertEquals("There should be two differences", 1, diffs.size());
    Delta delta1 = (Delta) diffs.get(0);
    Chunk orig1 = delta1.getOriginal();
    Chunk revised1 = delta1.getRevised();
    Delta delta2 = (Delta) diffs.get(0);
    Chunk orig2 = delta2.getOriginal();
    Chunk revised2 = delta2.getRevised();
    assertEquals("Original 1 should be", "love", orig1.toString());
    assertEquals("Revised 1 should be", "livein", revised1.toString());
}
Also used : Delta(org.suigeneris.jrcs.diff.delta.Delta) List(java.util.List) Chunk(org.suigeneris.jrcs.diff.delta.Chunk) Test(org.junit.Test)

Example 5 with Delta

use of org.suigeneris.jrcs.diff.delta.Delta in project xwiki-platform by xwiki.

the class DiffTest method testSimpleWordDiff3.

@Test
public void testSimpleWordDiff3() throws XWikiException {
    String text1 = "I love Paris and London";
    String text2 = "I love London and Paris";
    List diffs = this.plugin.getWordDifferencesAsList(text1, text2);
    assertEquals("There should be two differences", 2, diffs.size());
    Delta delta1 = (Delta) diffs.get(0);
    Chunk orig1 = delta1.getOriginal();
    Chunk revised1 = delta1.getRevised();
    Delta delta2 = (Delta) diffs.get(1);
    Chunk orig2 = delta2.getOriginal();
    Chunk revised2 = delta2.getRevised();
    assertEquals("Original 1 should be", "Parisand", orig1.toString());
    assertEquals("Revised 1 should be", "", revised1.toString());
    assertEquals("Original 2 should be", "", orig2.toString());
    assertEquals("Revised 2 should be", "andParis", revised2.toString());
}
Also used : Delta(org.suigeneris.jrcs.diff.delta.Delta) List(java.util.List) Chunk(org.suigeneris.jrcs.diff.delta.Chunk) Test(org.junit.Test)

Aggregations

Delta (org.suigeneris.jrcs.diff.delta.Delta)9 List (java.util.List)8 Chunk (org.suigeneris.jrcs.diff.delta.Chunk)8 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)2 ToString (org.suigeneris.jrcs.util.ToString)2 WikiEngine (org.apache.wiki.WikiEngine)1 WikiPage (org.apache.wiki.WikiPage)1 DifferentiationFailedException (org.suigeneris.jrcs.diff.DifferentiationFailedException)1 Revision (org.suigeneris.jrcs.diff.Revision)1 AddDelta (org.suigeneris.jrcs.diff.delta.AddDelta)1 ChangeDelta (org.suigeneris.jrcs.diff.delta.ChangeDelta)1 DeleteDelta (org.suigeneris.jrcs.diff.delta.DeleteDelta)1 MyersDiff (org.suigeneris.jrcs.diff.myers.MyersDiff)1