1 package org.jahia.utils.textdiff; 2 3 import java.util.ArrayList ; 4 import java.util.Vector ; 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 26 public class HunkTextDiffVisitor extends HunkVisitor { 27 28 private String oldText = ""; 29 private String newText = ""; 30 31 private ArrayList hunks = new ArrayList (); 32 33 private StringBuffer highlightedOldText = new StringBuffer (); 34 private StringBuffer highlightedNewText = new StringBuffer (); 35 private StringBuffer mergedDiffText = new StringBuffer (); 36 37 38 43 public HunkTextDiffVisitor(String oldText, String newText) { 44 45 if ( oldText != null ){ 46 this.oldText = oldText; 47 this.highlightedOldText = new StringBuffer (oldText); 48 this.mergedDiffText = new StringBuffer (oldText); 49 } 50 51 if ( newText != null ){ 52 this.newText = newText; 53 this.highlightedNewText = new StringBuffer (newText); 54 } 55 56 try { 58 jlibdiff.Diff diff = new Diff(); 59 diff.diffString(oldText,newText); 60 diff.accept(this); 61 } catch ( Throwable t ){ 62 t.printStackTrace(); 63 } 64 } 65 66 70 public void visitHunkAdd(HunkAdd hunkadd) { 71 this.hunks.add(hunkadd); 72 } 73 74 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 90 public void highLightDiff(){ 91 92 if ( this.hunks.size()==0 ){ 93 return; 94 } 95 96 StringBuffer oldValueBuff = new StringBuffer (); 97 StringBuffer newValueBuff = new StringBuffer (); 98 StringBuffer mergedValueBuff = new StringBuffer (); 99 String tmpOldValue = ""; 100 String tmpNewValue = ""; 101 String 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 buff = new StringBuffer (); 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 StringBuffer buff = new StringBuffer (); 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 buff = new StringBuffer (); 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 buff = new StringBuffer (); 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 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 180 String parsedHtml = ServicesRegistry.getInstance().getHtmlParserService() 181 .parse(oldValueBuff.toString(),new Vector ()); 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 ()); 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 ()); 194 if ( parsedHtml.indexOf(TidyHtmlParser.TIDYERRORS_TAG) == -1 ){ 195 this.mergedDiffText = mergedValueBuff; 196 } else { 197 this.mergedDiffText = new StringBuffer ("<span style='color:lime'>"+this.highlightedNewText.toString()+"</span>"); 198 } 199 200 } 201 202 206 public String getHighlightedOldText(){ 207 return this.highlightedOldText.toString(); 208 } 209 210 214 public String getHighlightedNewText(){ 215 return this.highlightedNewText.toString(); 216 } 217 218 222 public String getMergedDiffText(){ 223 return this.mergedDiffText.toString(); 224 } 225 226 232 public static String getDeletedText(String value){ 233 StringBuffer buff = new StringBuffer (); 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 247 public static String getAddedText(String value){ 248 StringBuffer buff = new StringBuffer (); 250 buff.append("<span style='color:aqua'>"); 251 buff.append(value); 252 buff.append("</span>"); 253 return buff.toString(); 254 } 255 256 262 public static String getChangedText(String value){ 263 StringBuffer buff = new StringBuffer (); 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 |