blob: 9ac3f03c45b8da9f088af0b90ee89b96f24f68b7 [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// Copyright 2012 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.gitiles;
16
17import static com.google.common.base.Preconditions.checkNotNull;
18
19import com.google.common.base.Charsets;
20import com.google.common.base.Function;
Dave Borowitz76bbefd2014-03-11 16:57:45 -070021import com.google.common.collect.FluentIterable;
Dave Borowitz9de65952012-08-13 16:09:45 -070022import com.google.common.collect.ImmutableList;
23import com.google.common.collect.ImmutableMap;
Dave Borowitz76bbefd2014-03-11 16:57:45 -070024import com.google.common.collect.Iterables;
Dave Borowitz9de65952012-08-13 16:09:45 -070025import com.google.common.collect.Maps;
26import com.google.template.soy.tofu.SoyTofu;
27
28import java.io.File;
29import java.io.IOException;
30import java.net.MalformedURLException;
31import java.net.URL;
32import java.util.List;
33import java.util.Map;
34
35import javax.servlet.http.HttpServletResponse;
36
37/** Renderer for Soy templates used by Gitiles. */
38public abstract class Renderer {
39 private static final List<String> SOY_FILENAMES = ImmutableList.of(
Dave Borowitz6ec0c872014-01-29 13:59:37 -080040 "BlameDetail.soy",
Dave Borowitz9de65952012-08-13 16:09:45 -070041 "Common.soy",
42 "DiffDetail.soy",
43 "HostIndex.soy",
44 "LogDetail.soy",
45 "ObjectDetail.soy",
46 "PathDetail.soy",
Dave Borowitz209d0aa2012-12-28 14:28:53 -080047 "RefList.soy",
Dave Borowitz9de65952012-08-13 16:09:45 -070048 "RevisionDetail.soy",
49 "RepositoryIndex.soy");
50
51 public static final Map<String, String> STATIC_URL_GLOBALS = ImmutableMap.of(
52 "gitiles.CSS_URL", "gitiles.css",
Dave Borowitzddc0efc2014-04-27 19:12:47 -060053 "gitiles.PRETTIFY_CSS_URL", "prettify/prettify.css");
Dave Borowitz9de65952012-08-13 16:09:45 -070054
Dave Borowitz76bbefd2014-03-11 16:57:45 -070055 protected static class FileUrlMapper implements Function<String, URL> {
56 private final String prefix;
57
58 protected FileUrlMapper() {
59 this("");
Dave Borowitz9de65952012-08-13 16:09:45 -070060 }
Dave Borowitz76bbefd2014-03-11 16:57:45 -070061
62 protected FileUrlMapper(String prefix) {
63 this.prefix = checkNotNull(prefix, "prefix");
64 }
65
66 @Override
67 public URL apply(String filename) {
68 if (filename == null) {
69 return null;
70 }
71 try {
72 return new File(prefix + filename).toURI().toURL();
73 } catch (MalformedURLException e) {
74 throw new IllegalArgumentException(e);
75 }
Dave Borowitz9de65952012-08-13 16:09:45 -070076 }
77 }
78
79 protected ImmutableList<URL> templates;
80 protected ImmutableMap<String, String> globals;
81
82 protected Renderer(Function<String, URL> resourceMapper, Map<String, String> globals,
Dave Borowitz76bbefd2014-03-11 16:57:45 -070083 String staticPrefix, Iterable<URL> customTemplates, String siteTitle) {
Dave Borowitz9de65952012-08-13 16:09:45 -070084 checkNotNull(staticPrefix, "staticPrefix");
Dave Borowitz76bbefd2014-03-11 16:57:45 -070085 Iterable<URL> allTemplates = FluentIterable.from(SOY_FILENAMES).transform(resourceMapper);
86 templates = ImmutableList.copyOf(Iterables.concat(allTemplates, customTemplates));
Dave Borowitz9de65952012-08-13 16:09:45 -070087
88 Map<String, String> allGlobals = Maps.newHashMap();
89 for (Map.Entry<String, String> e : STATIC_URL_GLOBALS.entrySet()) {
90 allGlobals.put(e.getKey(), staticPrefix + e.getValue());
91 }
Chad Horohoe2a28d622012-11-12 11:56:59 -080092 allGlobals.put("gitiles.SITE_TITLE", siteTitle);
Dave Borowitz9de65952012-08-13 16:09:45 -070093 allGlobals.putAll(globals);
94 this.globals = ImmutableMap.copyOf(allGlobals);
95 }
96
97 public void render(HttpServletResponse res, String templateName) throws IOException {
98 render(res, templateName, ImmutableMap.<String, Object> of());
99 }
100
101 public void render(HttpServletResponse res, String templateName, Map<String, ?> soyData)
102 throws IOException {
103 res.setContentType("text/html");
104 res.setCharacterEncoding("UTF-8");
105 byte[] data = newRenderer(templateName).setData(soyData).render().getBytes(Charsets.UTF_8);
106 res.setContentLength(data.length);
107 res.getOutputStream().write(data);
108 }
109
110 SoyTofu.Renderer newRenderer(String templateName) {
111 return getTofu().newRenderer(templateName);
112 }
113
114 protected abstract SoyTofu getTofu();
115}