| 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.Iterator; |
| 24 | |
import java.util.List; |
| 25 | |
import java.util.regex.Matcher; |
| 26 | |
import java.util.regex.Pattern; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 0 | public class PatchEntry implements Iterable<Difference> { |
| 40 | |
|
| 41 | 0 | public PatchEntry() { |
| 42 | 0 | this.diffs = new ArrayList<Difference>(); |
| 43 | 0 | this.sourceStart = 0; |
| 44 | 0 | this.targetStart = 0; |
| 45 | 0 | this.sourceLength = 0; |
| 46 | 0 | this.targetLength = 0; |
| 47 | 0 | } |
| 48 | |
|
| 49 | |
|
| 50 | |
public PatchEntry(String patchText) { |
| 51 | 0 | this(); |
| 52 | 0 | fromText(patchText); |
| 53 | 0 | } |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public int getSourceStart() { |
| 59 | 0 | return sourceStart; |
| 60 | |
} |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public void setSourceStart(int start) { |
| 67 | 0 | this.sourceStart = start; |
| 68 | 0 | } |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public void adjustSourceStart(int adjustment) { |
| 75 | 0 | this.sourceStart += adjustment; |
| 76 | 0 | } |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public int getTargetStart() { |
| 82 | 0 | return targetStart; |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
public void setTargetStart(int start) { |
| 90 | 0 | this.targetStart = start; |
| 91 | 0 | } |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
public void adjustTargetStart(int adjustment) { |
| 98 | 0 | this.targetStart += adjustment; |
| 99 | 0 | } |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public int getSourceLength() { |
| 105 | 0 | return sourceLength; |
| 106 | |
} |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public void setSourceLength(int length) { |
| 113 | 0 | this.sourceLength = length; |
| 114 | 0 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public void adjustSourceLength(int adjustment) { |
| 121 | 0 | this.sourceLength += adjustment; |
| 122 | 0 | } |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
public int getTargetLength() { |
| 128 | 0 | return targetLength; |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
public void setTargetLength(int length) { |
| 136 | 0 | this.targetLength = length; |
| 137 | 0 | } |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
public void adjustTargetLength(int adjustment) { |
| 144 | 0 | this.targetLength += adjustment; |
| 145 | 0 | } |
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
@Override |
| 151 | |
public String toString() { |
| 152 | 0 | StringBuilder txt = new StringBuilder(); |
| 153 | 0 | txt.append("@@ -"); |
| 154 | 0 | txt.append(getCoordinates(sourceStart, sourceLength)); |
| 155 | 0 | txt.append(" +"); |
| 156 | 0 | txt.append(getCoordinates(targetStart, targetLength)); |
| 157 | 0 | txt.append(" @@\n"); |
| 158 | |
|
| 159 | 0 | for (Difference diff : diffs) { |
| 160 | 0 | txt.append(diff.getEditType().getSymbol()); |
| 161 | 0 | txt.append(encode(diff.getText())); |
| 162 | 0 | txt.append('\n'); |
| 163 | |
} |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | 0 | return txt.toString(); |
| 174 | |
} |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
public PatchEntry fromText(String input) { |
| 185 | 0 | diffs.clear(); |
| 186 | 0 | String[] text = newlinePattern.split(input); |
| 187 | 0 | char sign = '\0'; |
| 188 | 0 | String line = ""; |
| 189 | |
|
| 190 | 0 | Matcher matcher = patchPattern.matcher(text[0]); |
| 191 | 0 | matcher.matches(); |
| 192 | 0 | assert matcher.groupCount() == 4 : "Invalid patch string:\n" + text[0]; |
| 193 | |
|
| 194 | |
|
| 195 | 0 | sourceStart = Integer.parseInt(matcher.group(1)); |
| 196 | |
|
| 197 | 0 | if (matcher.group(2).length() == 0) { |
| 198 | 0 | sourceStart--; |
| 199 | 0 | sourceLength = 1; |
| 200 | 0 | } else if (matcher.group(2).charAt(0) == '0') { |
| 201 | 0 | setSourceLength(0); |
| 202 | |
} else { |
| 203 | 0 | sourceStart--; |
| 204 | 0 | sourceLength = Integer.parseInt(matcher.group(2)); |
| 205 | |
} |
| 206 | |
|
| 207 | 0 | targetStart = Integer.parseInt(matcher.group(3)); |
| 208 | 0 | if (matcher.group(4).length() == 0) { |
| 209 | 0 | targetStart--; |
| 210 | 0 | targetLength = 1; |
| 211 | 0 | } else if (matcher.group(4).charAt(0) == '0') { |
| 212 | 0 | targetLength = 0; |
| 213 | |
} else { |
| 214 | 0 | targetStart--; |
| 215 | 0 | targetLength = Integer.parseInt(matcher.group(4)); |
| 216 | |
} |
| 217 | |
|
| 218 | 0 | for (int lineCount = 1; lineCount < text.length; lineCount++) { |
| 219 | 0 | line = text[lineCount]; |
| 220 | 0 | if (line.length() > 0) { |
| 221 | 0 | sign = line.charAt(0); |
| 222 | 0 | line = decode(line.substring(1)); |
| 223 | 0 | diffs.add(new Difference(EditType.fromSymbol(sign), line)); |
| 224 | |
} |
| 225 | |
} |
| 226 | 0 | return this; |
| 227 | |
} |
| 228 | |
|
| 229 | |
|
| 230 | |
public String getSourceText() { |
| 231 | 0 | StringBuilder txt = new StringBuilder(); |
| 232 | 0 | for (Difference diff : diffs) { |
| 233 | 0 | if (!EditType.INSERT.equals(diff.getEditType())) { |
| 234 | 0 | txt.append(diff.getText()); |
| 235 | |
} |
| 236 | |
} |
| 237 | 0 | return txt.toString(); |
| 238 | |
} |
| 239 | |
|
| 240 | |
|
| 241 | |
public String getTargetText() { |
| 242 | 0 | StringBuilder txt = new StringBuilder(); |
| 243 | 0 | for (Difference diff : diffs) { |
| 244 | 0 | if (!EditType.DELETE.equals(diff.getEditType())) { |
| 245 | 0 | txt.append(diff.getText()); |
| 246 | |
} |
| 247 | |
} |
| 248 | 0 | return txt.toString(); |
| 249 | |
} |
| 250 | |
|
| 251 | |
public void addContext(String text) { |
| 252 | 0 | int maxPatternLength = new Match().maxPatternLength(); |
| 253 | 0 | int padding = 0; |
| 254 | 0 | String pattern = text.substring(targetStart, targetStart + sourceLength); |
| 255 | 0 | int textLength = text.length(); |
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | 0 | int end = maxPatternLength - PatchEntry.margin - PatchEntry.margin; |
| 261 | 0 | while (text.indexOf(pattern) != text.lastIndexOf(pattern) && pattern.length() < end) { |
| 262 | 0 | padding += PatchEntry.margin; |
| 263 | 0 | pattern = text.substring(Math.max(0, targetStart - padding), Math.min(textLength, targetStart + sourceLength + padding)); |
| 264 | |
} |
| 265 | |
|
| 266 | |
|
| 267 | 0 | padding += PatchEntry.margin; |
| 268 | |
|
| 269 | |
|
| 270 | 0 | String prefix = text.substring(Math.max(0, targetStart - padding), targetStart); |
| 271 | 0 | int prefixLength = prefix.length(); |
| 272 | 0 | if (prefixLength > 0) { |
| 273 | 0 | diffs.add(0, new Difference(EditType.EQUAL, prefix)); |
| 274 | |
} |
| 275 | |
|
| 276 | |
|
| 277 | 0 | String suffix = text.substring(targetStart + sourceLength, Math.min(textLength, targetStart + sourceLength + padding)); |
| 278 | 0 | int suffixLength = suffix.length(); |
| 279 | 0 | if (suffixLength > 0) { |
| 280 | 0 | diffs.add(new Difference(EditType.EQUAL, suffix)); |
| 281 | |
} |
| 282 | |
|
| 283 | |
|
| 284 | 0 | sourceStart -= prefixLength; |
| 285 | 0 | targetStart -= prefixLength; |
| 286 | |
|
| 287 | |
|
| 288 | 0 | sourceLength += prefixLength + suffixLength; |
| 289 | 0 | targetLength += prefixLength + suffixLength; |
| 290 | 0 | } |
| 291 | |
|
| 292 | |
public void addDifference(Difference diff) { |
| 293 | 0 | diffs.add(diff); |
| 294 | 0 | } |
| 295 | |
|
| 296 | |
public int getDifferenceCount() { |
| 297 | 0 | return diffs.size(); |
| 298 | |
} |
| 299 | |
|
| 300 | |
public boolean hasDifferences() { |
| 301 | 0 | return !diffs.isEmpty(); |
| 302 | |
} |
| 303 | |
|
| 304 | |
public Iterator<Difference> iterator() { |
| 305 | 0 | return diffs.iterator(); |
| 306 | |
} |
| 307 | |
|
| 308 | |
public Difference getFirstDifference() { |
| 309 | 0 | if (diffs.isEmpty()) { |
| 310 | 0 | return null; |
| 311 | |
} |
| 312 | 0 | return diffs.get(0); |
| 313 | |
} |
| 314 | |
|
| 315 | |
public Difference removeFirstDifference() { |
| 316 | 0 | if (diffs.isEmpty()) { |
| 317 | 0 | return null; |
| 318 | |
} |
| 319 | 0 | return diffs.remove(0); |
| 320 | |
} |
| 321 | |
|
| 322 | |
public Difference getLastDifference() { |
| 323 | 0 | if (diffs.isEmpty()) { |
| 324 | 0 | return null; |
| 325 | |
} |
| 326 | 0 | return diffs.get(diffs.size() - 1); |
| 327 | |
} |
| 328 | |
|
| 329 | |
protected void setDifferences(List<Difference> newDiffs) { |
| 330 | 0 | diffs = newDiffs; |
| 331 | 0 | } |
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
public static void setMargin(int newMargin) { |
| 338 | 0 | PatchEntry.margin = newMargin; |
| 339 | 0 | } |
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
public static int getMargin() { |
| 345 | 0 | return margin; |
| 346 | |
} |
| 347 | |
|
| 348 | |
private String getCoordinates(int start, int length) { |
| 349 | 0 | StringBuilder buf = new StringBuilder(); |
| 350 | |
|
| 351 | 0 | if (length == 0) { |
| 352 | 0 | buf.append(start); |
| 353 | 0 | buf.append(",0"); |
| 354 | 0 | } else if (length == 1) { |
| 355 | 0 | buf.append(sourceStart + 1); |
| 356 | |
} else { |
| 357 | 0 | buf.append(start + 1); |
| 358 | 0 | buf.append(','); |
| 359 | 0 | buf.append(length); |
| 360 | |
} |
| 361 | |
|
| 362 | 0 | return buf.toString(); |
| 363 | |
} |
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
private String encode(String str) { |
| 375 | 0 | int strlen = str.length(); |
| 376 | 0 | StringBuilder buf = new StringBuilder(2 * strlen); |
| 377 | 0 | for (int i = 0; i < strlen; i++) { |
| 378 | 0 | char c = str.charAt(i); |
| 379 | 0 | switch (c) { |
| 380 | |
case '%': |
| 381 | 0 | buf.append("%25"); |
| 382 | 0 | break; |
| 383 | |
case '\n': |
| 384 | 0 | buf.append("%0A"); |
| 385 | 0 | break; |
| 386 | |
default: |
| 387 | 0 | buf.append(c); |
| 388 | |
} |
| 389 | |
} |
| 390 | 0 | return buf.toString(); |
| 391 | |
} |
| 392 | |
|
| 393 | |
|
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
private String decode(String str) { |
| 401 | 0 | int strlen = str.length(); |
| 402 | 0 | StringBuilder buf = new StringBuilder(2 * strlen); |
| 403 | 0 | int i = 0; |
| 404 | 0 | for (i = 0; i < strlen; i++) { |
| 405 | 0 | char c = str.charAt(i); |
| 406 | 0 | if (c == '%') { |
| 407 | 0 | if ("%0A".equals(str.substring(i, i + 3))) { |
| 408 | 0 | buf.append('\n'); |
| 409 | |
} else { |
| 410 | 0 | buf.append('%'); |
| 411 | |
} |
| 412 | 0 | i += 2; |
| 413 | |
} else { |
| 414 | 0 | buf.append(c); |
| 415 | |
} |
| 416 | |
} |
| 417 | 0 | return buf.toString(); |
| 418 | |
} |
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
|
| 423 | |
private static final int MARGIN = 4; |
| 424 | 0 | private static int margin = MARGIN; |
| 425 | 0 | private static Pattern newlinePattern = Pattern.compile("\n"); |
| 426 | 0 | private static Pattern patchPattern = Pattern.compile("^@@ -(\\d+),?(\\d*) \\+(\\d+),?(\\d*) @@$"); |
| 427 | |
|
| 428 | |
private List<Difference> diffs; |
| 429 | |
private int sourceStart; |
| 430 | |
private int targetStart; |
| 431 | |
private int sourceLength; |
| 432 | |
private int targetLength; |
| 433 | |
} |