lemval
2012-01-31 1c30dad2115fc513791d8a5b292ad0f7d7b85749
commit | author | age
f13c4c 1 /*
JM 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  */
5fe7df 16 package com.gitblit.wicket.pages;
JM 17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.apache.wicket.Component;
22 import org.apache.wicket.PageParameters;
23 import org.apache.wicket.markup.html.basic.Label;
608ece 24 import org.apache.wicket.markup.html.link.BookmarkablePageLink;
JM 25 import org.eclipse.jgit.lib.Constants;
5fe7df 26 import org.eclipse.jgit.lib.Repository;
JM 27 import org.eclipse.jgit.revwalk.RevCommit;
28
87cc1e 29 import com.gitblit.GitBlit;
155bf7 30 import com.gitblit.Keys;
5fe7df 31 import com.gitblit.utils.JGitUtils;
4ab184 32 import com.gitblit.utils.StringUtils;
5fe7df 33 import com.gitblit.wicket.WicketUtils;
c1c3c6 34 import com.gitblit.wicket.panels.CommitHeaderPanel;
5fe7df 35 import com.gitblit.wicket.panels.PathBreadcrumbsPanel;
JM 36
37 public class BlobPage extends RepositoryPage {
38
39     public BlobPage(PageParameters params) {
cebf45 40         super(params);
5fe7df 41
4ab184 42         Repository r = getRepository();
7d35e2 43         final String blobPath = WicketUtils.getPath(params);
5fe7df 44
4ab184 45         if (StringUtils.isEmpty(blobPath)) {
JM 46             // blob by objectid
2a7306 47
716745 48             add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
008322 49                     WicketUtils.newPathParameter(repositoryName, objectId, blobPath))
JM 50                     .setEnabled(false));
4ab184 51             add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class).setEnabled(false));
JM 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));
58             WicketUtils.setCssClass(c, "plainprint");
5fe7df 59             add(c);
JM 60         } else {
4ab184 61             // standard blob view
JM 62             String extension = null;
63             if (blobPath.lastIndexOf('.') > -1) {
64                 extension = blobPath.substring(blobPath.lastIndexOf('.') + 1).toLowerCase();
65             }
66
67             // see if we should redirect to the markdown page
68             for (String ext : GitBlit.getStrings(Keys.web.markdownExtensions)) {
69                 if (ext.equals(extension)) {
70                     setResponsePage(MarkdownPage.class, params);
71                     return;
72                 }
73             }
74
75             // manually get commit because it can be null
76             RevCommit commit = JGitUtils.getCommit(r, objectId);
77
78             // blob page links
716745 79             add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
JM 80                     WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
4ab184 81             add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
JM 82                     WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
83             add(new BookmarkablePageLink<Void>("rawLink", RawPage.class,
84                     WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
85             add(new BookmarkablePageLink<Void>("headLink", BlobPage.class,
86                     WicketUtils.newPathParameter(repositoryName, Constants.HEAD, blobPath)));
87
88             add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
89
90             add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
91
92             // Map the extensions to types
93             Map<String, Integer> map = new HashMap<String, Integer>();
94             for (String ext : GitBlit.getStrings(Keys.web.prettyPrintExtensions)) {
95                 map.put(ext.toLowerCase(), 1);
96             }
97             for (String ext : GitBlit.getStrings(Keys.web.imageExtensions)) {
98                 map.put(ext.toLowerCase(), 2);
99             }
100             for (String ext : GitBlit.getStrings(Keys.web.binaryExtensions)) {
101                 map.put(ext.toLowerCase(), 3);
102             }
103
104             if (extension != null) {
105                 int type = 0;
106                 if (map.containsKey(extension)) {
107                     type = map.get(extension);
108                 }
109                 Component c = null;
110                 switch (type) {
111                 case 1:
112                     // PrettyPrint blob text
113                     c = new Label("blobText", JGitUtils.getStringContent(r, commit.getTree(),
114                             blobPath));
115                     WicketUtils.setCssClass(c, "prettyprint linenums");
116                     break;
117                 case 2:
118                     // TODO image blobs
119                     c = new Label("blobText", "Image File");
120                     break;
121                 case 3:
122                     // TODO binary blobs
123                     c = new Label("blobText", "Binary File");
124                     break;
125                 default:
126                     // plain text
127                     c = new Label("blobText", JGitUtils.getStringContent(r, commit.getTree(),
128                             blobPath));
129                     WicketUtils.setCssClass(c, "plainprint");
130                 }
131                 add(c);
132             } else {
133                 // plain text
134                 Label blobLabel = new Label("blobText", JGitUtils.getStringContent(r,
135                         commit.getTree(), blobPath));
136                 WicketUtils.setCssClass(blobLabel, "plainprint");
137                 add(blobLabel);
138             }
5fe7df 139         }
JM 140     }
155bf7 141
cebf45 142     @Override
JM 143     protected String getPageName() {
1e47ab 144         return getString("gb.view");
cebf45 145     }
5fe7df 146 }