James Moger
2012-11-06 798581cab5817310a1b9991dac3b10cd7813f86a
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
16e474 18 import java.text.MessageFormat;
5fe7df 19 import java.util.HashMap;
JM 20 import java.util.Map;
21
22 import org.apache.wicket.Component;
23 import org.apache.wicket.PageParameters;
24 import org.apache.wicket.markup.html.basic.Label;
e4f49a 25 import org.apache.wicket.markup.html.image.Image;
608ece 26 import org.apache.wicket.markup.html.link.BookmarkablePageLink;
JM 27 import org.eclipse.jgit.lib.Constants;
5fe7df 28 import org.eclipse.jgit.lib.Repository;
JM 29 import org.eclipse.jgit.revwalk.RevCommit;
30
87cc1e 31 import com.gitblit.GitBlit;
155bf7 32 import com.gitblit.Keys;
5fe7df 33 import com.gitblit.utils.JGitUtils;
4ab184 34 import com.gitblit.utils.StringUtils;
e4f49a 35 import com.gitblit.wicket.ExternalImage;
5fe7df 36 import com.gitblit.wicket.WicketUtils;
c1c3c6 37 import com.gitblit.wicket.panels.CommitHeaderPanel;
5fe7df 38 import com.gitblit.wicket.panels.PathBreadcrumbsPanel;
JM 39
40 public class BlobPage extends RepositoryPage {
41
42     public BlobPage(PageParameters params) {
cebf45 43         super(params);
5fe7df 44
4ab184 45         Repository r = getRepository();
7d35e2 46         final String blobPath = WicketUtils.getPath(params);
ae9e15 47         String [] encodings = GitBlit.getEncodings();
JM 48         
4ab184 49         if (StringUtils.isEmpty(blobPath)) {
JM 50             // blob by objectid
2a7306 51
716745 52             add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
008322 53                     WicketUtils.newPathParameter(repositoryName, objectId, blobPath))
JM 54                     .setEnabled(false));
4ab184 55             add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class).setEnabled(false));
JM 56             add(new BookmarkablePageLink<Void>("rawLink", RawPage.class,
57                     WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
58             add(new BookmarkablePageLink<Void>("headLink", BlobPage.class).setEnabled(false));
59             add(new CommitHeaderPanel("commitHeader", objectId));
60             add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
ae9e15 61             Component c = new Label("blobText", JGitUtils.getStringContent(r, objectId, encodings));
4ab184 62             WicketUtils.setCssClass(c, "plainprint");
5fe7df 63             add(c);
JM 64         } else {
4ab184 65             // standard blob view
JM 66             String extension = null;
67             if (blobPath.lastIndexOf('.') > -1) {
68                 extension = blobPath.substring(blobPath.lastIndexOf('.') + 1).toLowerCase();
69             }
70
71             // see if we should redirect to the markdown page
72             for (String ext : GitBlit.getStrings(Keys.web.markdownExtensions)) {
73                 if (ext.equals(extension)) {
74                     setResponsePage(MarkdownPage.class, params);
75                     return;
76                 }
77             }
78
79             // manually get commit because it can be null
80             RevCommit commit = JGitUtils.getCommit(r, objectId);
81
82             // blob page links
716745 83             add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
JM 84                     WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
4ab184 85             add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
JM 86                     WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
87             add(new BookmarkablePageLink<Void>("rawLink", RawPage.class,
88                     WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));
89             add(new BookmarkablePageLink<Void>("headLink", BlobPage.class,
90                     WicketUtils.newPathParameter(repositoryName, Constants.HEAD, blobPath)));
91
92             add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
93
94             add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, blobPath, objectId));
95
96             // Map the extensions to types
97             Map<String, Integer> map = new HashMap<String, Integer>();
98             for (String ext : GitBlit.getStrings(Keys.web.prettyPrintExtensions)) {
99                 map.put(ext.toLowerCase(), 1);
100             }
101             for (String ext : GitBlit.getStrings(Keys.web.imageExtensions)) {
102                 map.put(ext.toLowerCase(), 2);
103             }
104             for (String ext : GitBlit.getStrings(Keys.web.binaryExtensions)) {
105                 map.put(ext.toLowerCase(), 3);
106             }
107
108             if (extension != null) {
109                 int type = 0;
110                 if (map.containsKey(extension)) {
111                     type = map.get(extension);
112                 }
113                 switch (type) {
114                 case 2:
e4f49a 115                     // image blobs
16e474 116                     add(new Label("blobText").setVisible(false));
JM 117                     add(new ExternalImage("blobImage", urlFor(RawPage.class, WicketUtils.newPathParameter(repositoryName, objectId, blobPath)).toString()));
4ab184 118                     break;
JM 119                 case 3:
e4f49a 120                     // binary blobs
16e474 121                     add(new Label("blobText", "Binary File"));
JM 122                     add(new Image("blobImage").setVisible(false));
4ab184 123                     break;
JM 124                 default:
125                     // plain text
16e474 126                     String source = JGitUtils.getStringContent(r, commit.getTree(), blobPath, encodings);
JM 127                     String table = generateSourceView(source, type == 1);
128                     add(new Label("blobText", table).setEscapeModelStrings(false));
129                     add(new Image("blobImage").setVisible(false));
4ab184 130                 }
JM 131             } else {
132                 // plain text
16e474 133                 String source = JGitUtils.getStringContent(r, commit.getTree(), blobPath, encodings);
9a73d1 134                 String table = generateSourceView(source, false);
16e474 135                 add(new Label("blobText", table).setEscapeModelStrings(false));
e4f49a 136                 add(new Image("blobImage").setVisible(false));
4ab184 137             }
5fe7df 138         }
JM 139     }
16e474 140     
JM 141     protected String generateSourceView(String source, boolean prettyPrint) {
142         String [] lines = source.split("\n");
143         
144         StringBuilder sb = new StringBuilder();
145         sb.append("<!-- start blob table -->");
146         sb.append("<table width=\"100%\"><tbody><tr>");
147         
148         // nums column
149         sb.append("<!-- start nums column -->");
150         sb.append("<td id=\"nums\">");
151         sb.append("<pre>");
9a73d1 152         String numPattern = "<span id=\"L{0}\" class=\"num\">{0}</span>\n";
16e474 153         for (int i = 0; i < lines.length; i++) {
JM 154             sb.append(MessageFormat.format(numPattern, "" + (i + 1)));
155         }
156         sb.append("</pre>");
157         sb.append("<!-- end nums column -->");
158         sb.append("</td>");
159         
160         sb.append("<!-- start lines column -->");
161         sb.append("<td id=\"lines\">");
162         sb.append("<div class=\"sourceview\">");
163         if (prettyPrint) {
164             sb.append("<pre class=\"prettyprint\">");
165         } else {
166             sb.append("<pre class=\"plainprint\">");
167         }
20d304 168         lines = StringUtils.escapeForHtml(source, true).split("\n");
16e474 169         
JM 170         sb.append("<table width=\"100%\"><tbody>");
171         
5c6afb 172         String linePattern = "<tr class=\"{0}\"><td><a href=\"#L{2}\">{1}</a>\r</tr>";
16e474 173         for (int i = 0; i < lines.length; i++) {
5c6afb 174             String line = lines[i].replace('\r', ' ');
16e474 175             String cssClass = (i % 2 == 0) ? "even" : "odd";
5c6afb 176             sb.append(MessageFormat.format(linePattern, cssClass, line, "" + (i + 1)));
16e474 177         }
JM 178         sb.append("</tbody></table></pre>");
9a73d1 179         sb.append("</pre>");
JM 180         sb.append("</div>");
16e474 181         sb.append("</td>");
JM 182         sb.append("<!-- end lines column -->");
183         
184         sb.append("</tr></tbody></table>");
185         sb.append("<!-- end blob table -->");
186         
187         return sb.toString();
188     }
155bf7 189
cebf45 190     @Override
JM 191     protected String getPageName() {
1e47ab 192         return getString("gb.view");
cebf45 193     }
5fe7df 194 }