KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > WordDiff


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar;
27
28 public class WordDiff
29 {
30     private String JavaDoc dumpDelText(DelimitedString str, int start, int end)
31     {
32         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
33
34         sb.append("<strike>");
35         sb.append(str.getSegmentsBetween(start, end));
36         sb.append("</strike>");
37
38         return sb.toString();
39     }
40
41     private String JavaDoc dumpAddText(DelimitedString str, int start, int end)
42     {
43         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
44
45         sb.append("<b>");
46         sb.append(str.getSegmentsBetween(start, end));
47         sb.append("</b>");
48
49         return sb.toString();
50     }
51
52     public String JavaDoc compareStrings(String JavaDoc source, String JavaDoc dest)
53     {
54         DelimitedString str1 = new DelimitedString(source);
55         DelimitedString str2 = new DelimitedString(dest);
56         int str1Idx = 0;
57         int str2Idx = 0;
58         int delTextStart = -1;
59         int addTextStart = -1;
60
61         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
62
63         while(true)
64         {
65             // Get the next available tokens from each string.
66
String JavaDoc s1 = str1.getSegment(str1Idx);
67             String JavaDoc s2 = str2.getSegment(str2Idx);
68
69
70             // If either of the strings are null, we're at the end.
71
if(s1 == null || s2 == null)
72             {
73
74                 // Check to see if there's any remaining text to mark as deleted. If
75
// so, go ahead and mark it now.
76
if(delTextStart != -1)
77                 result.append(dumpDelText(str1, delTextStart, DelimitedString.END));
78                 else if(s1 != null)
79                 result.append(dumpDelText(str1, str1Idx, DelimitedString.END));
80
81                 // Check to see if there's any remaining text to mark as added. If so,
82
// mark it now.
83
if(addTextStart != -1)
84                 result.append(dumpAddText(str2, addTextStart, DelimitedString.END));
85                 else if(s2 != null)
86                 result.append(dumpAddText(str2, str2Idx, DelimitedString.END));
87
88                 // Break out of the while loop, we're all finished.
89
break;
90             }
91
92             // Check to see if the two words are the same.
93
if(s1.equals(s2))
94             {
95
96                 // Mark any pending deleted text as such.
97
if(delTextStart != -1 && delTextStart != str1Idx)
98                 result.append(dumpDelText(str1, delTextStart, str1Idx));
99
100                 // Mark any pending added text as such.
101
if(addTextStart != -1 && addTextStart != str2Idx)
102                 result.append(dumpAddText(str2, addTextStart, str2Idx));
103
104                 // Reset the starting points for the deletes/adds
105
delTextStart = addTextStart = -1;
106
107                 // Append the current token to the result string.
108
result.append(s1);
109                 result.append(" ");
110
111                 // Move to the next tokens.
112
str1Idx++;
113                 str2Idx++;
114
115                 continue;
116             }
117             else
118             {
119                 // If we get here, we've found two tokens which do not match. Start the runs
120
// of deleted and added tokens.
121
if(delTextStart == -1) delTextStart = str1Idx;
122                 if(addTextStart == -1) addTextStart = str2Idx;
123
124                 boolean found = false;
125
126                 // Loop through the first string looking for the current token from the second
127
// string.
128
for(int i = str1Idx + 1; str1.getSegment(i) != null; i++)
129                 {
130
131                     // Does this first-string token match the one from the second string?
132
if(str1.getSegment(i).equals(s2))
133                     {
134
135                         // Yes it does, mark the end point and exit the for loop.
136
found = true;
137                         str1Idx = i;
138                         break;
139                     }
140                 }
141
142                 // Check to see if we found a match. If not, move on to the next word in the
143
// second string.
144
if(!found)
145                 {
146                     str2Idx++;
147                 }
148             }
149
150         }
151
152         // All done. return the string with the strike and bold markup.
153
return result.toString();
154     }
155
156     public static void main(String JavaDoc args[])
157     {
158         System.out.println(new WordDiff().compareStrings("This is a really long sentence", "This is a short sentence"));
159         System.out.println(new WordDiff().compareStrings("This is a test", "This was a test"));
160         System.out.println(new WordDiff().compareStrings("If this was a test of the emergency broadcast system", "this was a test of the emergncy broadcast system"));
161         System.out.println(new WordDiff().compareStrings("This is new stuff only found once", "All stuff is new only once"));
162     }
163 }
Popular Tags