Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DiffNormalOutput.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * DiffNormalOutput.java
  3. * Copyright (c) 2001 Andre Kaplan
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package jdiff.util;
  20. import java.io.IOException;
  21. /** Print a change list in the standard diff format. */
  22. public class DiffNormalOutput extends DiffOutput {
  23. public DiffNormalOutput(Object[] a, Object[] b) {
  24. super(a, b);
  25. }
  26. public void writeScript(Diff.change script) throws IOException {
  27. Diff.change hunk = script;
  28. for (; hunk != null; hunk = hunk.link) {
  29. this.writeHunk(hunk);
  30. }
  31. this.out.flush();
  32. }
  33. /** Writes a hunk of a normal diff. */
  34. protected void writeHunk(Diff.change hunk) throws IOException {
  35. int deletes = hunk.deleted;
  36. int inserts = hunk.inserted;
  37. if (deletes == 0 && inserts == 0) {
  38. return;
  39. }
  40. // Determine range of line numbers involved in each file.
  41. int first0 = hunk.line0;
  42. int first1 = hunk.line1;
  43. int last0 = hunk.line0 + hunk.deleted - 1;
  44. int last1 = hunk.line1 + hunk.inserted - 1;
  45. // Write out the line number header for this hunk
  46. this.writeNumberRange(',', first0, last0);
  47. out.write(DiffOutput.changeLetter(inserts, deletes));
  48. this.writeNumberRange(',', first1, last1);
  49. out.write(this.lineSeparator);
  50. // Write the lines that the first file has.
  51. if (deletes != 0) {
  52. for (int i = first0; i <= last0; i++) {
  53. this.writeLine("< ", this.file0[i]);
  54. }
  55. }
  56. if (inserts != 0 && deletes != 0) {
  57. out.write("---" + this.lineSeparator);
  58. }
  59. // Write the lines that the second file has.
  60. if (inserts != 0) {
  61. for (int i = first1; i <= last1; i++) {
  62. this.writeLine("> ", this.file1[i]);
  63. }
  64. }
  65. }
  66. }