KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > textdiff > HunkTextDiffVisitor


1 package org.jahia.utils.textdiff;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Vector JavaDoc;
5
6 import org.jahia.registries.ServicesRegistry;
7 import org.jahia.services.htmlparser.TidyHtmlParser;
8
9 import jlibdiff.Hunk;
10 import jlibdiff.HunkAdd;
11 import jlibdiff.HunkChange;
12 import jlibdiff.HunkDel;
13 import jlibdiff.HunkVisitor;
14 import jlibdiff.Diff;
15
16
17 /**
18  *
19  * <p>Title: Text Diff highlighter based on JLibDiff API </p>
20  * <p>Description: </p>
21  * <p>Copyright: Copyright (c) 2002</p>
22  * <p>Company: </p>
23  * @author Khue Nguyen
24  * @version 1.0
25  */

26 public class HunkTextDiffVisitor extends HunkVisitor {
27
28     private String JavaDoc oldText = "";
29     private String JavaDoc newText = "";
30
31     private ArrayList JavaDoc hunks = new ArrayList JavaDoc();
32
33     private StringBuffer JavaDoc highlightedOldText = new StringBuffer JavaDoc();
34     private StringBuffer JavaDoc highlightedNewText = new StringBuffer JavaDoc();
35     private StringBuffer JavaDoc mergedDiffText = new StringBuffer JavaDoc();
36
37
38     /**
39      *
40      * @param oldText
41      * @param newText
42      */

43     public HunkTextDiffVisitor(String JavaDoc oldText, String JavaDoc newText) {
44
45         if ( oldText != null ){
46             this.oldText = oldText;
47             this.highlightedOldText = new StringBuffer JavaDoc(oldText);
48             this.mergedDiffText = new StringBuffer JavaDoc(oldText);
49         }
50
51         if ( newText != null ){
52             this.newText = newText;
53             this.highlightedNewText = new StringBuffer JavaDoc(newText);
54         }
55
56         // Highlight text diff
57
try {
58             jlibdiff.Diff diff = new Diff();
59             diff.diffString(oldText,newText);
60             diff.accept(this);
61         } catch ( Throwable JavaDoc t ){
62             t.printStackTrace();
63         }
64     }
65
66     /**
67      *
68      * @param hunkadd
69      */

70     public void visitHunkAdd(HunkAdd hunkadd) {
71         this.hunks.add(hunkadd);
72     }
73
74     /**
75      *
76      * @param hunkchange
77      */

78     public void visitHunkChange(HunkChange hunkchange) {
79         this.hunks.add(hunkchange);
80     }
81
82     public void visitHunkDel(HunkDel hunkdel) {
83         this.hunks.add(hunkdel);
84     }
85
86     /**
87      * You must call this method to run text diff processing
88      *
89      */

90     public void highLightDiff(){
91
92         if ( this.hunks.size()==0 ){
93             return;
94         }
95
96         StringBuffer JavaDoc oldValueBuff = new StringBuffer JavaDoc();
97         StringBuffer JavaDoc newValueBuff = new StringBuffer JavaDoc();
98         StringBuffer JavaDoc mergedValueBuff = new StringBuffer JavaDoc();
99         String JavaDoc tmpOldValue = "";
100         String JavaDoc tmpNewValue = "";
101         String JavaDoc tmpMergedValue = "";
102
103         int oldValuePos = 0;
104         int newValuePos = 0;
105
106         int size = this.hunks.size();
107         Hunk hunk = null;
108         for ( int i=0; i<size ; i++ ){
109             tmpOldValue = oldValueBuff.toString();
110             tmpNewValue = newValueBuff.toString();
111             tmpMergedValue = mergedValueBuff.toString();
112             int tmpOldValuePos = oldValuePos;
113             int tmpNewValuePos = newValuePos;
114
115             hunk = (Hunk)this.hunks.get(i);
116             if ( hunk instanceof HunkAdd ){
117                 oldValueBuff.append(this.oldText.substring(oldValuePos,hunk.lowLine(0)));
118                 newValueBuff.append(this.newText.substring(newValuePos,hunk.lowLine(1)-1));
119                 mergedValueBuff.append(this.oldText.substring(oldValuePos,hunk.lowLine(0)));
120
121                 StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
122                 buff.append("<span style='color:aqua'>");
123                 buff.append(this.newText.substring(hunk.lowLine(1)-1,hunk.highLine(1)));
124                 buff.append("</span>");
125
126                 mergedValueBuff.append(buff.toString());
127                 newValueBuff.append(buff.toString());
128                 oldValuePos = hunk.highLine(0);
129                 newValuePos = hunk.highLine(1);
130
131             } else if ( hunk instanceof HunkChange ){
132                 oldValueBuff.append(this.oldText.substring(oldValuePos,hunk.lowLine(0)-1));
133                 newValueBuff.append(this.newText.substring(newValuePos,hunk.lowLine(1)-1));
134                 mergedValueBuff.append(this.oldText.substring(oldValuePos,hunk.lowLine(0)-1));
135
136                 // old
137
StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
138                 buff.append("<span style='color:lime'>");
139                 buff.append(this.oldText.substring(hunk.lowLine(0)-1,hunk.highLine(0)));
140                 buff.append("</span>");
141                 oldValueBuff.append(buff.toString());
142
143                 // new
144
buff = new StringBuffer JavaDoc();
145                 buff.append("<span style='color:lime'>");
146                 buff.append(this.newText.substring(hunk.lowLine(1)-1,hunk.highLine(1)));
147                 buff.append("</span>");
148                 mergedValueBuff.append(buff.toString());
149                 newValueBuff.append(buff.toString());
150
151                 oldValuePos = hunk.highLine(0);
152                 newValuePos = hunk.highLine(1);
153
154             } else {
155                 oldValueBuff.append(this.oldText.substring(oldValuePos,hunk.lowLine(0)-1));
156                 newValueBuff.append(this.newText.substring(newValuePos,hunk.lowLine(1)));
157                 mergedValueBuff.append(this.oldText.substring(oldValuePos,hunk.lowLine(0)-1));
158
159                 StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
160                 buff.append("<span style='color:red;text-decoration:line-through;'>");
161                 buff.append(this.oldText.substring(hunk.lowLine(0)-1,hunk.highLine(0)));
162                 buff.append("</span>");
163
164                 // old
165
oldValueBuff.append(buff.toString());
166                 mergedValueBuff.append(buff.toString());
167
168                 oldValuePos = hunk.highLine(0);
169                 newValuePos = hunk.highLine(1);
170             }
171         }
172         oldValueBuff.append(this.oldText.substring(oldValuePos));
173         newValueBuff.append(this.newText.substring(newValuePos));
174         mergedValueBuff.append(this.oldText.substring(oldValuePos));
175
176         //this.highlightedOldText = oldValueBuff;
177
//this.highlightedNewText = newValueBuff;
178
//this.mergedDiffText = mergedValueBuff;
179

180         String JavaDoc parsedHtml = ServicesRegistry.getInstance().getHtmlParserService()
181             .parse(oldValueBuff.toString(),new Vector JavaDoc());
182         if ( parsedHtml.indexOf(TidyHtmlParser.TIDYERRORS_TAG) == -1 ){
183             this.highlightedOldText = oldValueBuff;
184         }
185
186         parsedHtml = ServicesRegistry.getInstance().getHtmlParserService()
187             .parse(newValueBuff.toString(),new Vector JavaDoc());
188         if ( parsedHtml.indexOf(TidyHtmlParser.TIDYERRORS_TAG) == -1 ){
189             this.highlightedNewText = newValueBuff;
190         }
191
192         parsedHtml = ServicesRegistry.getInstance().getHtmlParserService()
193             .parse(mergedValueBuff.toString(),new Vector JavaDoc());
194         if ( parsedHtml.indexOf(TidyHtmlParser.TIDYERRORS_TAG) == -1 ){
195             this.mergedDiffText = mergedValueBuff;
196         } else {
197              this.mergedDiffText = new StringBuffer JavaDoc("<span style='color:lime'>"+this.highlightedNewText.toString()+"</span>");
198         }
199
200     }
201
202     /**
203      *
204      * @return
205      */

206     public String JavaDoc getHighlightedOldText(){
207         return this.highlightedOldText.toString();
208     }
209
210     /**
211      *
212      * @return
213      */

214     public String JavaDoc getHighlightedNewText(){
215         return this.highlightedNewText.toString();
216     }
217
218     /**
219      *
220      * @return
221      */

222     public String JavaDoc getMergedDiffText(){
223         return this.mergedDiffText.toString();
224     }
225
226     /**
227      * Returns a deleted text style
228      *
229      * @param value
230      * @return
231      */

232     public static String JavaDoc getDeletedText(String JavaDoc value){
233         // @todo : put in a service
234
StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
235         buff.append("<span style='color:red;text-decoration:line-through;'>");
236         buff.append(value);
237         buff.append("</span>");
238         return buff.toString();
239     }
240
241     /**
242      * Returns an added text style
243      *
244      * @param value
245      * @return
246      */

247     public static String JavaDoc getAddedText(String JavaDoc value){
248         // @todo : put in a service
249
StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
250         buff.append("<span style='color:aqua'>");
251         buff.append(value);
252         buff.append("</span>");
253         return buff.toString();
254     }
255
256     /**
257      * Returns a changed text style
258      *
259      * @param value
260      * @return
261      */

262     public static String JavaDoc getChangedText(String JavaDoc value){
263         // @todo : put in a service
264
StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
265         buff.append("<span style='color:lime'>");
266         buff.append(value);
267         buff.append("</span>");
268         return buff.toString();
269     }
270
271 }
272
Popular Tags