diff-util

originalnew
1  This part of the
2  document has stayed the
3  same from version to
4  version.  It shouldn't
5  be shown if it doesn't
6  change.  Otherwise, that
7  would not be helping to
8  compress the size of the
9  changes.
10 
11 This paragraph contains
12 text that is outdated.
13 It will be deleted in the
14 near future.
15 
16 It is important to spell
17 check this dokument. On
18 the other hand, a
19 misspelled word isn't
20 the end of the world.
21 Nothing in the rest of
22 this paragraph needs to
23 be changed. Things can
24 be added after it.
1  This is an important
2  notice! It should
3  therefore be located at
4  the beginning of this
5  document!
6  
7  This part of the
8  document has stayed the
9  same from version to
10 version.  It shouldn't
11 be shown if it doesn't
12 change.  Otherwise, that
13 would not be helping to
14 compress anything.
15 
16 It is important to spell
17 check this document. On
18 the other hand, a
19 misspelled word isn't
20 the end of the world.
21 Nothing in the rest of
22 this paragraph needs to
23 be changed. Things can
24 be added after it.
25 
26 This paragraph contains
27 important new additions
28 to this document.

create unified diff

    	List<String> original = new LinkedList<String>();
    	original.add("1  This part of the");
    	original.add("2  document has stayed the");
    	original.add("3  same from version to");
    	original.add("4  version.  It shouldn't");
    	original.add("5  be shown if it doesn't");
    	original.add("6  change.  Otherwise, that");
    	original.add("7  would not be helping to");
    	original.add("8  compress the size of the");
    	original.add("9  changes.");
    	original.add("10 ");
    	original.add("11 This paragraph contains");
    	original.add("12 text that is outdated.");
    	original.add("13 It will be deleted in the");
    	original.add("14 near future.");
    	original.add("15 ");
    	original.add("16 It is important to spell");
    	original.add("17 check this dokument. On");
    	original.add("18 the other hand, a");
    	original.add("19 misspelled word isn't");
    	original.add("20 the end of the world.");
    	original.add("21 Nothing in the rest of");
    	original.add("22 this paragraph needs to");
    	original.add("23 be changed. Things can");
    	original.add("24 be added after it.");
    	
    	List<String> revised = new LinkedList<String>();
    	revised.add("1  This is an important");
    	revised.add("2  notice! It should");
    	revised.add("3  therefore be located at");
    	revised.add("4  the beginning of this");
    	revised.add("5  document!");
    	revised.add("6  ");
    	revised.add("7  This part of the");
    	revised.add("8  document has stayed the");
    	revised.add("9  same from version to");
    	revised.add("10 version.  It shouldn't");
    	revised.add("11 be shown if it doesn't");
    	revised.add("12 change.  Otherwise, that");
    	revised.add("13 would not be helping to");
    	revised.add("14 compress anything.");
    	revised.add("15 ");
    	revised.add("16 It is important to spell");
    	revised.add("17 check this document. On");
    	revised.add("18 the other hand, a");
    	revised.add("19 misspelled word isn't");
    	revised.add("20 the end of the world.");
    	revised.add("21 Nothing in the rest of");
    	revised.add("22 this paragraph needs to");
    	revised.add("23 be changed. Things can");
    	revised.add("24 be added after it.");
    	revised.add("25 ");
    	revised.add("26 This paragraph contains");
    	revised.add("27 important new additions");
    	revised.add("28 to this document.");
    	
    	Patch patch = DiffUtils.diff(original, revised);
    	List<String> unidiff = DiffUtils.generateUnifiedDiff("Before", "After", original, patch, 0);
    	for (String string : unidiff) {
			System.out.println(string);
	}

output:

--- Before
+++ After
@@ -1,14 +1,14 @@
-1  This part of the
-2  document has stayed the
-3  same from version to
-4  version.  It shouldn't
-5  be shown if it doesn't
-6  change.  Otherwise, that
-7  would not be helping to
-8  compress the size of the
-9  changes.
-10 
-11 This paragraph contains
-12 text that is outdated.
-13 It will be deleted in the
-14 near future.
+1  This is an important
+2  notice! It should
+3  therefore be located at
+4  the beginning of this
+5  document!
+6  
+7  This part of the
+8  document has stayed the
+9  same from version to
+10 version.  It shouldn't
+11 be shown if it doesn't
+12 change.  Otherwise, that
+13 would not be helping to
+14 compress anything.
@@ -17,1 +17,1 @@
-17 check this dokument. On
+17 check this document. On
@@ -25,0 +25,4 @@
+25 
+26 This paragraph contains
+27 important new additions
+28 to this document.

import unified diff

    	Patch patch_generated = DiffUtils.parseUnifiedDiff(unidiff);
    	try {
		List<String> revised_generated = (List<String>) DiffUtils.patch(original, patch_generated);
		for (String string : revised_generated) {
				System.out.println(string);
		}
	} catch (PatchFailedException e) {
			e.printStackTrace();
	}
	List<String> original_generated = (List<String>) DiffUtils.unpatch(revised, patch_generated);
    	for (String string : original_generated) {
    		System.out.println(string);
	}

output:

1  This is an important
2  notice! It should
3  therefore be located at
4  the beginning of this
5  document!
6  
7  This part of the
8  document has stayed the
9  same from version to
10 version.  It shouldn't
11 be shown if it doesn't
12 change.  Otherwise, that
13 would not be helping to
14 compress anything.
15 
16 It is important to spell
17 check this document. On
18 the other hand, a
19 misspelled word isn't
20 the end of the world.
21 Nothing in the rest of
22 this paragraph needs to
23 be changed. Things can
24 be added after it.
25 
26 This paragraph contains
27 important new additions
28 to this document.
1  This part of the
2  document has stayed the
3  same from version to
4  version.  It shouldn't
5  be shown if it doesn't
6  change.  Otherwise, that
7  would not be helping to
8  compress the size of the
9  changes.
10 
11 This paragraph contains
12 text that is outdated.
13 It will be deleted in the
14 near future.
15 
16 It is important to spell
17 check this dokument. On
18 the other hand, a
19 misspelled word isn't
20 the end of the world.
21 Nothing in the rest of
22 this paragraph needs to
23 be changed. Things can
24 be added after it.

ok.


  1. diff-util_row /raw/diff-util_row
	private class L2B{
		private StringBuilder sb = new StringBuilder();
		StringBuilder l2b(List<?> l){
			sb.delete(0, sb.length());
			for (Object object : l) {
				sb.append(object+"\n");
			}
			if(sb.length()>=1)sb.deleteCharAt(sb.length()-1); // last "\n"
			return sb;
		}
	}
    private void comparableDiff(){
    	
    	List<String> original = new LinkedList<String>();
    	original.add("This part of the");
    	original.add("document has stayed the");
    	original.add("same from version to");
    	original.add("version.  It shouldn't");
    	original.add("be shown if it doesn't");
    	original.add("change.  Otherwise, that");
    	original.add("would not be helping to");
    	original.add("compress the size of the");
    	original.add("changes.");
    	original.add("");
    	original.add("This paragraph contains");
    	original.add("text that is outdated.");
    	original.add("It will be deleted in the");
    	original.add("near future.");
    	original.add("");
    	original.add("It is important to spell");
    	original.add("check this dokument. On");
    	original.add("the other hand, a");
    	original.add("misspelled word isn't");
    	original.add("the end of the world.");
    	original.add("Nothing in the rest of");
    	original.add("this paragraph needs to");
    	original.add("be changed. Things can");
    	original.add("be added after it.");
    	original.add("25 ***********************");
    	original.add("26 ***********************");
    	original.add("27 ****###**#***#**###****");
    	original.add("28 ****#****##**#**#**#***");
    	original.add("29 ****###**#*#*#**#**#***");
    	original.add("30 ****#****#**##**#**#***");
    	original.add("31 ****###**#***#**###****");
    	original.add("32 ***********************");
    	original.add("33 ***********************");
    	original.add("25 ");
    	original.add("This paragraph contains");
    	original.add("important new additions");
    	original.add("to this document.");
    	
    	List<String> revised = new LinkedList<String>();
    	revised.add("This is an important");
    	revised.add("notice! It should");
    	revised.add("therefore be located at");
    	revised.add("the beginning of this");
    	revised.add("document!");
    	revised.add("");
    	revised.add("This part of the");
    	revised.add("document has stayed the");
    	revised.add("same from version to");
    	revised.add("version.  It shouldn't");
    	revised.add("be shown if it doesn't");
    	revised.add("change.  Otherwise, that");
    	revised.add("would not be helping to");
    	revised.add("compress anything.");
    	revised.add("");
    	revised.add("It is important to spell");
    	revised.add("check this document. On");
    	revised.add("the other hand, a");
    	revised.add("misspelled word isn't");
    	revised.add("the end of the world.");
    	revised.add("Nothing in the rest of");
    	revised.add("this paragraph needs to");
    	revised.add("be changed. Things can");
    	revised.add("be added after it.");
    	revised.add("");
    	revised.add("This paragraph contains");
    	revised.add("important new additions");
    	revised.add("to this document.");
    	
    	String[] cors = new String[]{"palegreen","khaki","pink","moccasin","lightskyblue","lightyellow","coral","aliceblue","yellowgreen","beige","lightpink"};
    	StringBuilder tl = new StringBuilder();
    	StringBuilder tr = new StringBuilder();
    	Patch patch = DiffUtils.diff(original, revised);
    	List<Delta> deltas = patch.getDeltas();
    	L2B l2B = new L2B();
    	int cori = 0;
    	int last = 0;
    	for (Delta delta : deltas) {
    		if(last + 1 < delta.getOriginal().getPosition()){ // not continuous
    			tl.append("<pre style='font-size:smaller;'>\n");
    			tr.append("<pre style='font-size:smaller;'>\n");
    			for(int i = last + 1; i < delta.getOriginal().getPosition(); i++){
    				tl.append(original.get(i)+"\n");
    				tr.append(original.get(i)+"\n");
    			}
    			tl.append("</pre>\n");
    			tr.append("</pre>\n");
    		}
		List<?> or = delta.getOriginal().getLines();
    		tl.append("<pre style='background-color:"+cors[cori]+";'>\n"+l2B.l2b(or)+"\n</pre>");
    		List<?> re = delta.getRevised().getLines();
    		tr.append("<pre style='background-color:"+cors[cori]+";'>\n"+l2B.l2b(re)+"\n</pre>");
			cori = (cori<cors.length)?cori+1:0;
			last = delta.getOriginal().last();
		}
		if(last + 1 < original.size()){ //last is not delta
			tl.append("<pre style='font-size:smaller;'>\n");
			tr.append("<pre style='font-size:smaller;'>\n");
			for(int i = last + 1; i < original.size(); i++){
				tl.append(original.get(i)+"\n");
				tr.append(original.get(i)+"\n");
			}
			tl.append("</pre>\n");
			tr.append("</pre>\n");
		}
		
    	System.out.println("<html><table><tr><td style='vertical-align:top;'>"+tl.toString()+"</td><td style='vertical-align:top;'>"+tr.toString()+"</td></tr></table></html>");
    }
    last modified: 28 October 2011 [ View wiki source Close ]
     
SEARCH DIFFERENCE ATTACHMENT RECENT POST