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