You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HtmlDiffFormatter.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.gitblit.utils;
  2. import static org.eclipse.jgit.lib.Constants.encodeASCII;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import org.eclipse.jgit.diff.DiffFormatter;
  7. import org.eclipse.jgit.diff.RawText;
  8. public class HtmlDiffFormatter extends DiffFormatter {
  9. private final OutputStream os;
  10. public HtmlDiffFormatter(OutputStream os) {
  11. super(os);
  12. this.os = os;
  13. }
  14. /**
  15. * Output a hunk header
  16. *
  17. * @param aStartLine
  18. * within first source
  19. * @param aEndLine
  20. * within first source
  21. * @param bStartLine
  22. * within second source
  23. * @param bEndLine
  24. * within second source
  25. * @throws IOException
  26. */
  27. @Override
  28. protected void writeHunkHeader(int aStartLine, int aEndLine, int bStartLine, int bEndLine) throws IOException {
  29. os.write("<div class=\"diff hunk_header\"><span class=\"diff hunk_info\">".getBytes());
  30. os.write('@');
  31. os.write('@');
  32. writeRange('-', aStartLine + 1, aEndLine - aStartLine);
  33. writeRange('+', bStartLine + 1, bEndLine - bStartLine);
  34. os.write(' ');
  35. os.write('@');
  36. os.write('@');
  37. os.write("</span></div>".getBytes());
  38. }
  39. private void writeRange(final char prefix, final int begin, final int cnt) throws IOException {
  40. os.write(' ');
  41. os.write(prefix);
  42. switch (cnt) {
  43. case 0:
  44. // If the range is empty, its beginning number must
  45. // be the
  46. // line just before the range, or 0 if the range is
  47. // at the
  48. // start of the file stream. Here, begin is always 1
  49. // based,
  50. // so an empty file would produce "0,0".
  51. //
  52. os.write(encodeASCII(begin - 1));
  53. os.write(',');
  54. os.write('0');
  55. break;
  56. case 1:
  57. // If the range is exactly one line, produce only
  58. // the number.
  59. //
  60. os.write(encodeASCII(begin));
  61. break;
  62. default:
  63. os.write(encodeASCII(begin));
  64. os.write(',');
  65. os.write(encodeASCII(cnt));
  66. break;
  67. }
  68. }
  69. @Override
  70. protected void writeLine(final char prefix, final RawText text, final int cur) throws IOException {
  71. switch (prefix) {
  72. case '+':
  73. os.write("<span class=\"diff add\">".getBytes());
  74. break;
  75. case '-':
  76. os.write("<span class=\"diff remove\">".getBytes());
  77. break;
  78. }
  79. os.write(prefix);
  80. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  81. text.writeLine(bos, cur);
  82. String line = bos.toString();
  83. line = StringUtils.escapeForHtml(line, false);
  84. os.write(line.getBytes());
  85. switch (prefix) {
  86. case '+':
  87. case '-':
  88. os.write("</span>\n".getBytes());
  89. break;
  90. default:
  91. os.write('\n');
  92. }
  93. }
  94. /**
  95. * Workaround function for complex private methods in DiffFormatter. This
  96. * sets the html for the diff headers.
  97. *
  98. * @return
  99. */
  100. public String getHtml() {
  101. String html = os.toString();
  102. String[] lines = html.split("\n");
  103. StringBuilder sb = new StringBuilder();
  104. sb.append("<div class=\"diff\">");
  105. for (String line : lines) {
  106. if (line.startsWith("diff")) {
  107. sb.append("<div class=\"diff header\">").append(line).append("</div>");
  108. } else if (line.startsWith("---")) {
  109. sb.append("<span class=\"diff remove\">").append(line).append("</span><br/>");
  110. } else if (line.startsWith("+++")) {
  111. sb.append("<span class=\"diff add\">").append(line).append("</span><br/>");
  112. } else {
  113. sb.append(line).append('\n');
  114. }
  115. }
  116. sb.append("</div>\n");
  117. return sb.toString();
  118. }
  119. }