public File createDiff(File out, String dest) { File result = out; try { String[] a = DiffPrint.slurp(original); String[] b = DiffPrint.slurp(revised); Diff d = new Diff(a, b); Diff.change script = d.diff_2(false); if (script == null) { JOptionPane.showMessageDialog(null, "Error: No differences"); result = null; } else { DiffPrint.Base p = new DiffPrint.UnifiedPrint(a, b); FileWriter fw = new FileWriter(result); p.setOutput(fw); p.print_header(".orig" + dest, dest); p.print_script(script); p.close(); doubleCheckChunkHeaders(result); } } catch (Exception e) { result = null; } return result; }
private void doubleCheckChunkHeaders(File out) { try { String[] patch = DiffPrint.slurp(out); int currHeader = -1; int before = 0; int after = 0; int offset = 0; ChunkHeader curr = null; for (int i = 0; i < patch.length; i++) { if (patch[i].startsWith("@@")) { if (currHeader != -1 && curr != null) { // finish curr.setBeforeRange(before); curr.setAfterRange(after); patch[currHeader] = curr.toString(); } currHeader = i; curr = new ChunkHeader(patch[i]); offset += (after - before); curr.setOffset(offset); before = 0; after = 0; } else if (patch[i].startsWith("+")) { after++; } else if (patch[i].startsWith("-")) { before++; } else if (patch[i].startsWith(" ")) { before++; after++; } } if (currHeader != -1 && curr != null) { // last header curr.setBeforeRange(before); curr.setAfterRange(after); patch[currHeader] = curr.toString(); } writeToFile(out, patch); } catch (Exception e) { } }