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.

BlobPage.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.util.HashMap;
  18. import java.util.Map;
  19. import org.apache.wicket.Component;
  20. import org.apache.wicket.PageParameters;
  21. import org.apache.wicket.markup.html.basic.Label;
  22. import org.apache.wicket.markup.html.link.BookmarkablePageLink;
  23. import org.eclipse.jgit.lib.Constants;
  24. import org.eclipse.jgit.lib.Repository;
  25. import org.eclipse.jgit.revwalk.RevCommit;
  26. import com.gitblit.GitBlit;
  27. import com.gitblit.Keys;
  28. import com.gitblit.utils.JGitUtils;
  29. import com.gitblit.wicket.RepositoryPage;
  30. import com.gitblit.wicket.WicketUtils;
  31. import com.gitblit.wicket.panels.CommitHeaderPanel;
  32. import com.gitblit.wicket.panels.PathBreadcrumbsPanel;
  33. public class BlobPage extends RepositoryPage {
  34. public BlobPage(PageParameters params) {
  35. super(params);
  36. final String blobPath = WicketUtils.getPath(params);
  37. String extension = null;
  38. if (blobPath.lastIndexOf('.') > -1) {
  39. extension = blobPath.substring(blobPath.lastIndexOf('.') + 1).toLowerCase();
  40. }
  41. // see if we should redirect to the markdown page
  42. for (String ext : GitBlit.self().settings().getStrings(Keys.web.markdownExtensions)) {
  43. if (ext.equals(extension)) {
  44. setResponsePage(MarkdownPage.class, params);
  45. return;
  46. }
  47. }
  48. // standard blob view
  49. Repository r = getRepository();
  50. RevCommit commit = getCommit();
  51. // blob page links
  52. add(new Label("blameLink", getString("gb.blame")));
  53. add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class, WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
  54. add(new BookmarkablePageLink<Void>("rawLink", RawPage.class, WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
  55. add(new BookmarkablePageLink<Void>("headLink", BlobPage.class, WicketUtils.newPathParameter(repositoryName, Constants.HEAD, blobPath)));
  56. add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
  57. add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
  58. // Map the extensions to types
  59. Map<String, Integer> map = new HashMap<String, Integer>();
  60. for (String ext : GitBlit.self().settings().getStrings(Keys.web.prettyPrintExtensions)) {
  61. map.put(ext.toLowerCase(), 1);
  62. }
  63. for (String ext : GitBlit.self().settings().getStrings(Keys.web.imageExtensions)) {
  64. map.put(ext.toLowerCase(), 2);
  65. }
  66. for (String ext : GitBlit.self().settings().getStrings(Keys.web.binaryExtensions)) {
  67. map.put(ext.toLowerCase(), 3);
  68. }
  69. if (extension != null) {
  70. int type = 0;
  71. if (map.containsKey(extension)) {
  72. type = map.get(extension);
  73. }
  74. Component c = null;
  75. switch (type) {
  76. case 1:
  77. // PrettyPrint blob text
  78. c = new Label("blobText", JGitUtils.getRawContentAsString(r, commit, blobPath));
  79. WicketUtils.setCssClass(c, "prettyprint linenums");
  80. break;
  81. case 2:
  82. // TODO image blobs
  83. c = new Label("blobText", "Image File");
  84. break;
  85. case 3:
  86. // TODO binary blobs
  87. c = new Label("blobText", "Binary File");
  88. break;
  89. default:
  90. // plain text
  91. c = new Label("blobText", JGitUtils.getRawContentAsString(r, commit, blobPath));
  92. WicketUtils.setCssClass(c, "plainprint");
  93. }
  94. add(c);
  95. } else {
  96. // plain text
  97. Label blobLabel = new Label("blobText", JGitUtils.getRawContentAsString(r, commit, blobPath));
  98. WicketUtils.setCssClass(blobLabel, "plainprint");
  99. add(blobLabel);
  100. }
  101. }
  102. @Override
  103. protected String getPageName() {
  104. return getString("gb.view");
  105. }
  106. }