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 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.RedirectException;
  23. import org.apache.wicket.markup.html.basic.Label;
  24. import org.apache.wicket.markup.html.image.Image;
  25. import org.apache.wicket.markup.html.link.BookmarkablePageLink;
  26. import org.apache.wicket.markup.html.link.ExternalLink;
  27. import org.eclipse.jgit.lib.Repository;
  28. import org.eclipse.jgit.revwalk.RevCommit;
  29. import com.gitblit.Keys;
  30. import com.gitblit.servlet.RawServlet;
  31. import com.gitblit.utils.JGitUtils;
  32. import com.gitblit.utils.StringUtils;
  33. import com.gitblit.wicket.CacheControl;
  34. import com.gitblit.wicket.CacheControl.LastModified;
  35. import com.gitblit.wicket.ExternalImage;
  36. import com.gitblit.wicket.MarkupProcessor;
  37. import com.gitblit.wicket.WicketUtils;
  38. import com.gitblit.wicket.panels.CommitHeaderPanel;
  39. import com.gitblit.wicket.panels.PathBreadcrumbsPanel;
  40. @CacheControl(LastModified.BOOT)
  41. public class BlobPage extends RepositoryPage {
  42. protected String fileExtension;
  43. public BlobPage(PageParameters params) {
  44. super(params);
  45. Repository r = getRepository();
  46. final String blobPath = WicketUtils.getPath(params);
  47. String [] encodings = getEncodings();
  48. if (StringUtils.isEmpty(objectId) && StringUtils.isEmpty(blobPath)) {
  49. throw new RedirectException(TreePage.class, WicketUtils.newRepositoryParameter(repositoryName));
  50. }
  51. if (StringUtils.isEmpty(blobPath)) {
  52. // blob by objectid
  53. add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
  54. WicketUtils.newPathParameter(repositoryName, objectId, blobPath))
  55. .setEnabled(false));
  56. add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class).setEnabled(false));
  57. String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, objectId, blobPath);
  58. add(new ExternalLink("rawLink", rawUrl));
  59. add(new CommitHeaderPanel("commitHeader", objectId));
  60. add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
  61. Component c = new Label("blobText", JGitUtils.getStringContent(r, objectId, encodings));
  62. WicketUtils.setCssClass(c, "plainprint");
  63. add(c);
  64. } else {
  65. // standard blob view
  66. String extension = null;
  67. if (blobPath.lastIndexOf('.') > -1) {
  68. extension = blobPath.substring(blobPath.lastIndexOf('.') + 1).toLowerCase();
  69. }
  70. // see if we should redirect to the doc page
  71. MarkupProcessor processor = new MarkupProcessor(app().settings(), app().xssFilter());
  72. for (String ext : processor.getMarkupExtensions()) {
  73. if (ext.equals(extension)) {
  74. setResponsePage(DocPage.class, params);
  75. return;
  76. }
  77. }
  78. RevCommit commit = getCommit();
  79. // blob page links
  80. add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
  81. WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
  82. add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
  83. WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
  84. String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, objectId, blobPath);
  85. add(new ExternalLink("rawLink", rawUrl));
  86. add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
  87. add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
  88. // Map the extensions to types
  89. Map<String, Integer> map = new HashMap<String, Integer>();
  90. for (String ext : app().settings().getStrings(Keys.web.prettyPrintExtensions)) {
  91. map.put(ext.toLowerCase(), 1);
  92. }
  93. for (String ext : app().settings().getStrings(Keys.web.imageExtensions)) {
  94. map.put(ext.toLowerCase(), 2);
  95. }
  96. for (String ext : app().settings().getStrings(Keys.web.binaryExtensions)) {
  97. map.put(ext.toLowerCase(), 3);
  98. }
  99. if (extension != null) {
  100. int type = 0;
  101. if (map.containsKey(extension)) {
  102. type = map.get(extension);
  103. }
  104. switch (type) {
  105. case 2:
  106. // image blobs
  107. add(new Label("blobText").setVisible(false));
  108. add(new ExternalImage("blobImage", rawUrl));
  109. break;
  110. case 3:
  111. // binary blobs
  112. add(new Label("blobText", "Binary File"));
  113. add(new Image("blobImage").setVisible(false));
  114. break;
  115. default:
  116. // plain text
  117. String source = JGitUtils.getStringContent(r, commit.getTree(), blobPath, encodings);
  118. String table;
  119. if (source == null) {
  120. table = missingBlob(blobPath, commit);
  121. } else {
  122. table = generateSourceView(source, extension, type == 1);
  123. addBottomScriptInline("jQuery(prettyPrint);");
  124. }
  125. add(new Label("blobText", table).setEscapeModelStrings(false));
  126. add(new Image("blobImage").setVisible(false));
  127. fileExtension = extension;
  128. }
  129. } else {
  130. // plain text
  131. String source = JGitUtils.getStringContent(r, commit.getTree(), blobPath, encodings);
  132. String table;
  133. if (source == null) {
  134. table = missingBlob(blobPath, commit);
  135. } else {
  136. table = generateSourceView(source, null, false);
  137. addBottomScriptInline("jQuery(prettyPrint);");
  138. }
  139. add(new Label("blobText", table).setEscapeModelStrings(false));
  140. add(new Image("blobImage").setVisible(false));
  141. }
  142. }
  143. }
  144. protected String missingBlob(String blobPath, RevCommit commit) {
  145. StringBuilder sb = new StringBuilder();
  146. sb.append("<div class=\"alert alert-error\">");
  147. String pattern = getString("gb.doesNotExistInTree").replace("{0}", "<b>{0}</b>").replace("{1}", "<b>{1}</b>");
  148. sb.append(MessageFormat.format(pattern, blobPath, commit.getTree().getId().getName()));
  149. sb.append("</div>");
  150. return sb.toString();
  151. }
  152. protected String generateSourceView(String source, String extension, boolean prettyPrint) {
  153. String [] lines = source.split("\n");
  154. StringBuilder sb = new StringBuilder();
  155. sb.append("<!-- start blob table -->");
  156. sb.append("<table width=\"100%\"><tbody><tr>");
  157. // nums column
  158. sb.append("<!-- start nums column -->");
  159. sb.append("<td id=\"nums\">");
  160. sb.append("<pre>");
  161. String numPattern = "<span id=\"L{0}\" class=\"jump\"></span><a href=\"#L{0}\">{0}</a>\n";
  162. for (int i = 0; i < lines.length; i++) {
  163. sb.append(MessageFormat.format(numPattern, "" + (i + 1)));
  164. }
  165. sb.append("</pre>");
  166. sb.append("<!-- end nums column -->");
  167. sb.append("</td>");
  168. sb.append("<!-- start lines column -->");
  169. sb.append("<td id=\"lines\">");
  170. sb.append("<div class=\"sourceview\">");
  171. if (prettyPrint) {
  172. sb.append("<pre class=\"prettyprint lang-" + extension + "\">");
  173. } else {
  174. sb.append("<pre class=\"plainprint\">");
  175. }
  176. final int tabLength = app().settings().getInteger(Keys.web.tabLength, 4);
  177. lines = StringUtils.escapeForHtml(source, true, tabLength).split("\n");
  178. sb.append("<table width=\"100%\"><tbody>");
  179. String linePattern = "<tr class=\"{0}\"><td><div><span class=\"line\">{1}</span></div>\r</tr>";
  180. for (int i = 0; i < lines.length; i++) {
  181. String line = lines[i].replace('\r', ' ');
  182. String cssClass = (i % 2 == 0) ? "even" : "odd";
  183. if (StringUtils.isEmpty(line.trim())) {
  184. line = "&nbsp;";
  185. }
  186. sb.append(MessageFormat.format(linePattern, cssClass, line, "" + (i + 1)));
  187. }
  188. sb.append("</tbody></table></pre>");
  189. sb.append("</pre>");
  190. sb.append("</div>");
  191. sb.append("</td>");
  192. sb.append("<!-- end lines column -->");
  193. sb.append("</tr></tbody></table>");
  194. sb.append("<!-- end blob table -->");
  195. return sb.toString();
  196. }
  197. @Override
  198. protected String getPageName() {
  199. return getString("gb.view");
  200. }
  201. @Override
  202. protected boolean isCommitPage() {
  203. return true;
  204. }
  205. @Override
  206. protected Class<? extends BasePage> getRepoNavPageClass() {
  207. return TreePage.class;
  208. }
  209. }