| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
package org.crosswire.common.diff; |
| 21 | |
|
| 22 | |
import java.util.ArrayList; |
| 23 | |
import java.util.List; |
| 24 | |
import java.util.ListIterator; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
public class Diff { |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public Diff(final String source, final String target) { |
| 48 | 0 | this(source, target, true); |
| 49 | 0 | } |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | 0 | public Diff(final String source, final String target, final boolean checkLines) { |
| 64 | 0 | this.source = source; |
| 65 | 0 | this.target = target; |
| 66 | 0 | this.checkLines = checkLines; |
| 67 | 0 | } |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public List<Difference> compare() { |
| 76 | |
|
| 77 | |
List<Difference> diffs; |
| 78 | 0 | if (source.equals(target)) { |
| 79 | 0 | diffs = new ArrayList<Difference>(); |
| 80 | 0 | diffs.add(new Difference(EditType.EQUAL, source)); |
| 81 | 0 | return diffs; |
| 82 | |
} |
| 83 | |
|
| 84 | |
|
| 85 | 0 | int commonLength = Commonality.prefix(source, target); |
| 86 | 0 | String commonPrefix = source.substring(0, commonLength); |
| 87 | 0 | source = source.substring(commonLength); |
| 88 | 0 | target = target.substring(commonLength); |
| 89 | |
|
| 90 | |
|
| 91 | 0 | commonLength = Commonality.suffix(source, target); |
| 92 | 0 | String commonSuffix = source.substring(source.length() - commonLength); |
| 93 | 0 | source = source.substring(0, source.length() - commonLength); |
| 94 | 0 | target = target.substring(0, target.length() - commonLength); |
| 95 | |
|
| 96 | |
|
| 97 | 0 | diffs = compute(); |
| 98 | |
|
| 99 | |
|
| 100 | 0 | if (!"".equals(commonPrefix)) { |
| 101 | 0 | diffs.add(0, new Difference(EditType.EQUAL, commonPrefix)); |
| 102 | |
} |
| 103 | |
|
| 104 | 0 | if (!"".equals(commonSuffix)) { |
| 105 | 0 | diffs.add(new Difference(EditType.EQUAL, commonSuffix)); |
| 106 | |
} |
| 107 | |
|
| 108 | 0 | DiffCleanup.cleanupMerge(diffs); |
| 109 | |
|
| 110 | 0 | return diffs; |
| 111 | |
} |
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
private List<Difference> compute() { |
| 119 | 0 | List<Difference> diffs = new ArrayList<Difference>(); |
| 120 | |
|
| 121 | 0 | if ("".equals(source)) { |
| 122 | |
|
| 123 | 0 | diffs.add(new Difference(EditType.INSERT, target)); |
| 124 | 0 | return diffs; |
| 125 | |
} |
| 126 | |
|
| 127 | 0 | if ("".equals(target)) { |
| 128 | |
|
| 129 | 0 | diffs.add(new Difference(EditType.DELETE, source)); |
| 130 | 0 | return diffs; |
| 131 | |
} |
| 132 | |
|
| 133 | 0 | String longText = source.length() > target.length() ? source : target; |
| 134 | 0 | String shortText = source.length() > target.length() ? target : source; |
| 135 | 0 | int i = longText.indexOf(shortText); |
| 136 | 0 | if (i != -1) { |
| 137 | |
|
| 138 | 0 | EditType editType = (source.length() > target.length()) ? EditType.DELETE : EditType.INSERT; |
| 139 | 0 | diffs.add(new Difference(editType, longText.substring(0, i))); |
| 140 | 0 | diffs.add(new Difference(EditType.EQUAL, shortText)); |
| 141 | 0 | diffs.add(new Difference(editType, longText.substring(i + shortText.length()))); |
| 142 | 0 | return diffs; |
| 143 | |
} |
| 144 | |
|
| 145 | |
|
| 146 | 0 | CommonMiddle middleMatch = Commonality.halfMatch(source, target); |
| 147 | 0 | if (middleMatch != null) { |
| 148 | |
|
| 149 | |
|
| 150 | 0 | Diff startDiff = new Diff(middleMatch.getSourcePrefix(), middleMatch.getTargetPrefix(), checkLines); |
| 151 | 0 | Diff endDiff = new Diff(middleMatch.getSourceSuffix(), middleMatch.getTargetSuffix(), checkLines); |
| 152 | |
|
| 153 | 0 | diffs = startDiff.compare(); |
| 154 | 0 | diffs.add(new Difference(EditType.EQUAL, middleMatch.getCommonality())); |
| 155 | 0 | diffs.addAll(endDiff.compare()); |
| 156 | 0 | return diffs; |
| 157 | |
} |
| 158 | |
|
| 159 | |
|
| 160 | 0 | if (checkLines && source.length() + target.length() < 250) { |
| 161 | 0 | checkLines = false; |
| 162 | |
} |
| 163 | |
|
| 164 | 0 | LineMap lineMap = null; |
| 165 | 0 | if (checkLines) { |
| 166 | |
|
| 167 | 0 | lineMap = new LineMap(source, target); |
| 168 | 0 | source = lineMap.getSourceMap(); |
| 169 | 0 | target = lineMap.getTargetMap(); |
| 170 | |
} |
| 171 | |
|
| 172 | 0 | diffs = new DifferenceEngine(source, target).generate(); |
| 173 | |
|
| 174 | 0 | if (diffs == null) { |
| 175 | |
|
| 176 | 0 | diffs = new ArrayList<Difference>(); |
| 177 | 0 | diffs.add(new Difference(EditType.DELETE, source)); |
| 178 | 0 | diffs.add(new Difference(EditType.INSERT, target)); |
| 179 | |
} |
| 180 | |
|
| 181 | 0 | if (checkLines && lineMap != null) { |
| 182 | |
|
| 183 | 0 | lineMap.restore(diffs); |
| 184 | |
|
| 185 | 0 | DiffCleanup.cleanupSemantic(diffs); |
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | 0 | diffs.add(new Difference(EditType.EQUAL, "")); |
| 190 | 0 | int countDeletes = 0; |
| 191 | 0 | int countInserts = 0; |
| 192 | 0 | StringBuilder textDelete = new StringBuilder(); |
| 193 | 0 | StringBuilder textInsert = new StringBuilder(); |
| 194 | 0 | ListIterator<Difference> pointer = diffs.listIterator(); |
| 195 | 0 | Difference curDiff = pointer.next(); |
| 196 | 0 | while (curDiff != null) { |
| 197 | 0 | EditType editType = curDiff.getEditType(); |
| 198 | 0 | if (EditType.INSERT.equals(editType)) { |
| 199 | 0 | countInserts++; |
| 200 | 0 | textInsert.append(curDiff.getText()); |
| 201 | 0 | } else if (EditType.DELETE.equals(editType)) { |
| 202 | 0 | countDeletes++; |
| 203 | 0 | textDelete.append(curDiff.getText()); |
| 204 | |
} else { |
| 205 | |
|
| 206 | 0 | if (countDeletes >= 1 && countInserts >= 1) { |
| 207 | |
|
| 208 | 0 | pointer.previous(); |
| 209 | 0 | for (int j = 0; j < countDeletes + countInserts; j++) { |
| 210 | 0 | pointer.previous(); |
| 211 | 0 | pointer.remove(); |
| 212 | |
} |
| 213 | 0 | Diff newDiff = new Diff(textDelete.toString(), textInsert.toString(), false); |
| 214 | 0 | for (Difference diff : newDiff.compare()) { |
| 215 | 0 | pointer.add(diff); |
| 216 | |
} |
| 217 | |
} |
| 218 | 0 | countInserts = 0; |
| 219 | 0 | countDeletes = 0; |
| 220 | 0 | textDelete.delete(0, textDelete.length()); |
| 221 | 0 | textInsert.delete(0, textInsert.length()); |
| 222 | |
} |
| 223 | 0 | curDiff = pointer.hasNext() ? pointer.next() : null; |
| 224 | 0 | } |
| 225 | 0 | diffs.remove(diffs.size() - 1); |
| 226 | |
|
| 227 | |
} |
| 228 | 0 | return diffs; |
| 229 | |
} |
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
public int xIndex(final List<Difference> diffs, final int loc) { |
| 242 | 0 | int chars1 = 0; |
| 243 | 0 | int chars2 = 0; |
| 244 | 0 | int lastChars1 = 0; |
| 245 | 0 | int lastChars2 = 0; |
| 246 | 0 | Difference lastDiff = null; |
| 247 | 0 | for (Difference diff : diffs) { |
| 248 | 0 | EditType editType = diff.getEditType(); |
| 249 | |
|
| 250 | |
|
| 251 | 0 | if (!EditType.INSERT.equals(editType)) { |
| 252 | 0 | chars1 += diff.getText().length(); |
| 253 | |
} |
| 254 | |
|
| 255 | |
|
| 256 | 0 | if (!EditType.DELETE.equals(editType)) { |
| 257 | 0 | chars2 += diff.getText().length(); |
| 258 | |
} |
| 259 | |
|
| 260 | |
|
| 261 | 0 | if (chars1 > loc) { |
| 262 | 0 | lastDiff = diff; |
| 263 | 0 | break; |
| 264 | |
} |
| 265 | 0 | lastChars1 = chars1; |
| 266 | 0 | lastChars2 = chars2; |
| 267 | 0 | } |
| 268 | |
|
| 269 | |
|
| 270 | 0 | if (lastDiff != null && EditType.DELETE.equals(lastDiff.getEditType())) { |
| 271 | 0 | return lastChars2; |
| 272 | |
} |
| 273 | |
|
| 274 | |
|
| 275 | 0 | return lastChars2 + (loc - lastChars1); |
| 276 | |
} |
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
public String prettyHtml(List<Difference> diffs) { |
| 286 | 0 | StringBuilder buf = new StringBuilder(); |
| 287 | 0 | for (int x = 0; x < diffs.size(); x++) { |
| 288 | 0 | Difference diff = diffs.get(x); |
| 289 | 0 | EditType editType = diff.getEditType(); |
| 290 | |
|
| 291 | 0 | String text = diff.getText(); |
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | 0 | if (EditType.DELETE.equals(editType)) { |
| 298 | 0 | buf.append("<del style=\"background:#FFE6E6;\">"); |
| 299 | 0 | buf.append(text); |
| 300 | 0 | buf.append("</del>"); |
| 301 | 0 | } else if (EditType.INSERT.equals(editType)) { |
| 302 | 0 | buf.append("<ins style=\"background:#E6FFE6;\">"); |
| 303 | 0 | buf.append(text); |
| 304 | 0 | buf.append("</ins>"); |
| 305 | |
} else { |
| 306 | 0 | buf.append("<span>"); |
| 307 | 0 | buf.append(text); |
| 308 | 0 | buf.append("</span>"); |
| 309 | |
} |
| 310 | |
} |
| 311 | 0 | return buf.toString(); |
| 312 | |
} |
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
private String source; |
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
private String target; |
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
private boolean checkLines; |
| 328 | |
} |