Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BlobPage.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket.pages;
  17. import java.text.MessageFormat;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import org.apache.wicket.Component;
  21. import org.apache.wicket.PageParameters;
  22. import org.apache.wicket.markup.html.basic.Label;
  23. import org.apache.wicket.markup.html.image.Image;
  24. import org.apache.wicket.markup.html.link.BookmarkablePageLink;
  25. import org.eclipse.jgit.lib.Constants;
  26. import org.eclipse.jgit.lib.Repository;
  27. import org.eclipse.jgit.revwalk.RevCommit;
  28. import com.gitblit.GitBlit;
  29. import com.gitblit.Keys;
  30. import com.gitblit.utils.JGitUtils;
  31. import com.gitblit.utils.StringUtils;
  32. import com.gitblit.wicket.CacheControl;
  33. import com.gitblit.wicket.ExternalImage;
  34. import com.gitblit.wicket.WicketUtils;
  35. import com.gitblit.wicket.CacheControl.LastModified;
  36. import com.gitblit.wicket.panels.CommitHeaderPanel;
  37. import com.gitblit.wicket.panels.PathBreadcrumbsPanel;
  38. @CacheControl(LastModified.BOOT)
  39. public class BlobPage extends RepositoryPage {
  40. protected String fileExtension;
  41. public BlobPage(PageParameters params) {
  42. super(params);
  43. Repository r = getRepository();
  44. final String blobPath = WicketUtils.getPath(params);
  45. String [] encodings = GitBlit.getEncodings();
  46. if (StringUtils.isEmpty(blobPath)) {
  47. // blob by objectid
  48. add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
  49. WicketUtils.newPathParameter(repositoryName, objectId, blobPath))
  50. .setEnabled(false));
  51. add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class).setEnabled(false));
  52. add(new BookmarkablePageLink<Void>("rawLink", RawPage.class,
  53. WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
  54. add(new BookmarkablePageLink<Void>("headLink", BlobPage.class).setEnabled(false));
  55. add(new CommitHeaderPanel("commitHeader", objectId));
  56. add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
  57. Component c = new Label("blobText", JGitUtils.getStringContent(r, objectId, encodings));
  58. WicketUtils.setCssClass(c, "plainprint");
  59. add(c);
  60. } else {
  61. // standard blob view
  62. String extension = null;
  63. if (blobPath.lastIndexOf('.') > -1) {
  64. extension = blobPath.substring(blobPath.lastIndexOf('.') + 1).toLowerCase();
  65. }
  66. // see if we should redirect to the markdown page
  67. for (String ext : GitBlit.getStrings(Keys.web.markdownExtensions)) {
  68. if (ext.equals(extension)) {
  69. setResponsePage(MarkdownPage.class, params);
  70. return;
  71. }
  72. }
  73. // manually get commit because it can be null
  74. RevCommit commit = JGitUtils.getCommit(r, objectId);
  75. // blob page links
  76. add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
  77. WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
  78. add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
  79. WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
  80. add(new BookmarkablePageLink<Void>("rawLink", RawPage.class,
  81. WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
  82. add(new BookmarkablePageLink<Void>("headLink", BlobPage.class,
  83. WicketUtils.newPathParameter(repositoryName, Constants.HEAD, blobPath)));
  84. add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
  85. add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
  86. // Map the extensions to types
  87. Map<String, Integer> map = new HashMap<String, Integer>();
  88. for (String ext : GitBlit.getStrings(Keys.web.prettyPrintExtensions)) {
  89. map.put(ext.toLowerCase(), 1);
  90. }
  91. for (String ext : GitBlit.getStrings(Keys.web.imageExtensions)) {
  92. map.put(ext.toLowerCase(), 2);
  93. }
  94. for (String ext : GitBlit.getStrings(Keys.web.binaryExtensions)) {
  95. map.put(ext.toLowerCase(), 3);
  96. }
  97. if (extension != null) {
  98. int type = 0;
  99. if (map.containsKey(extension)) {
  100. type = map.get(extension);
  101. }
  102. switch (type) {
  103. case 2:
  104. // image blobs
  105. add(new Label("blobText").setVisible(false));
  106. add(new ExternalImage("blobImage", urlFor(RawPage.class, WicketUtils.newPathParameter(repositoryName, objectId, blobPath)).toString()));
  107. break;
  108. case 3:
  109. // binary blobs
  110. add(new Label("blobText", "Binary File"));
  111. add(new Image("blobImage").setVisible(false));
  112. break;
  113. default:
  114. // plain text
  115. String source = JGitUtils.getStringContent(r, commit.getTree(), blobPath, encodings);
  116. String table;
  117. if (source == null) {
  118. table = missingBlob(blobPath, commit);
  119. } else {
  120. table = generateSourceView(source, extension, type == 1);
  121. }
  122. add(new Label("blobText", table).setEscapeModelStrings(false));
  123. add(new Image("blobImage").setVisible(false));
  124. fileExtension = extension;
  125. }
  126. } else {
  127. // plain text
  128. String source = JGitUtils.getStringContent(r, commit.getTree(), blobPath, encodings);
  129. String table;
  130. if (source == null) {
  131. table = missingBlob(blobPath, commit);
  132. } else {
  133. table = generateSourceView(source, null, false);
  134. }
  135. add(new Label("blobText", table).setEscapeModelStrings(false));
  136. add(new Image("blobImage").setVisible(false));
  137. }
  138. }
  139. }
  140. protected String missingBlob(String blobPath, RevCommit commit) {
  141. StringBuilder sb = new StringBuilder();
  142. sb.append("<div class=\"alert alert-error\">");
  143. String pattern = getString("gb.doesNotExistInTree").replace("{0}", "<b>{0}</b>").replace("{1}", "<b>{1}</b>");
  144. sb.append(MessageFormat.format(pattern, blobPath, commit.getTree().getId().getName()));
  145. sb.append("</div>");
  146. return sb.toString();
  147. }
  148. protected String generateSourceView(String source, String extension, boolean prettyPrint) {
  149. String [] lines = source.split("\n");
  150. StringBuilder sb = new StringBuilder();
  151. sb.append("<!-- start blob table -->");
  152. sb.append("<table width=\"100%\"><tbody><tr>");
  153. // nums column
  154. sb.append("<!-- start nums column -->");
  155. sb.append("<td id=\"nums\">");
  156. sb.append("<pre>");
  157. String numPattern = "<span id=\"L{0}\" class=\"num\">{0}</span>\n";
  158. for (int i = 0; i < lines.length; i++) {
  159. sb.append(MessageFormat.format(numPattern, "" + (i + 1)));
  160. }
  161. sb.append("</pre>");
  162. sb.append("<!-- end nums column -->");
  163. sb.append("</td>");
  164. sb.append("<!-- start lines column -->");
  165. sb.append("<td id=\"lines\">");
  166. sb.append("<div class=\"sourceview\">");
  167. if (prettyPrint) {
  168. sb.append("<pre class=\"prettyprint lang-" + extension + "\">");
  169. } else {
  170. sb.append("<pre class=\"plainprint\">");
  171. }
  172. lines = StringUtils.escapeForHtml(source, true).split("\n");
  173. sb.append("<table width=\"100%\"><tbody>");
  174. String linePattern = "<tr class=\"{0}\"><td><a href=\"#L{2}\">{1}</a>\r</tr>";
  175. for (int i = 0; i < lines.length; i++) {
  176. String line = lines[i].replace('\r', ' ');
  177. String cssClass = (i % 2 == 0) ? "even" : "odd";
  178. sb.append(MessageFormat.format(linePattern, cssClass, line, "" + (i + 1)));
  179. }
  180. sb.append("</tbody></table></pre>");
  181. sb.append("</pre>");
  182. sb.append("</div>");
  183. sb.append("</td>");
  184. sb.append("<!-- end lines column -->");
  185. sb.append("</tr></tbody></table>");
  186. sb.append("<!-- end blob table -->");
  187. return sb.toString();
  188. }
  189. @Override
  190. protected String getPageName() {
  191. return getString("gb.view");
  192. }
  193. @Override
  194. protected Class<? extends BasePage> getRepoNavPageClass() {
  195. return TreePage.class;
  196. }
  197. }