blob: 1ab26c4b79bffd562bc14a8ec30343f2530e74a5 [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;
21import com.google.common.collect.ImmutableList;
22import com.google.common.collect.ImmutableMap;
23import com.google.common.collect.Lists;
24import com.google.common.collect.Maps;
25import com.google.template.soy.tofu.SoyTofu;
26
27import java.io.File;
28import java.io.IOException;
29import java.net.MalformedURLException;
30import java.net.URL;
31import java.util.List;
32import java.util.Map;
33
34import javax.servlet.http.HttpServletResponse;
35
36/** Renderer for Soy templates used by Gitiles. */
37public abstract class Renderer {
38 private static final List<String> SOY_FILENAMES = ImmutableList.of(
Dave Borowitz6ec0c872014-01-29 13:59:37 -080039 "BlameDetail.soy",
Dave Borowitz9de65952012-08-13 16:09:45 -070040 "Common.soy",
41 "DiffDetail.soy",
42 "HostIndex.soy",
43 "LogDetail.soy",
44 "ObjectDetail.soy",
45 "PathDetail.soy",
Dave Borowitz209d0aa2012-12-28 14:28:53 -080046 "RefList.soy",
Dave Borowitz9de65952012-08-13 16:09:45 -070047 "RevisionDetail.soy",
48 "RepositoryIndex.soy");
49
50 public static final Map<String, String> STATIC_URL_GLOBALS = ImmutableMap.of(
51 "gitiles.CSS_URL", "gitiles.css",
52 "gitiles.PRETTIFY_CSS_URL", "prettify/prettify.css",
53 "gitiles.PRETTIFY_JS_URL", "prettify/prettify.js");
54
55 protected static final URL toFileURL(String filename) {
56 if (filename == null) {
57 return null;
58 }
59 try {
60 return new File(filename).toURI().toURL();
61 } catch (MalformedURLException e) {
62 throw new IllegalArgumentException(e);
63 }
64 }
65
66 protected ImmutableList<URL> templates;
67 protected ImmutableMap<String, String> globals;
68
69 protected Renderer(Function<String, URL> resourceMapper, Map<String, String> globals,
Chad Horohoe2a28d622012-11-12 11:56:59 -080070 String staticPrefix, URL customTemplates, String siteTitle) {
Dave Borowitz9de65952012-08-13 16:09:45 -070071 checkNotNull(staticPrefix, "staticPrefix");
72 List<URL> allTemplates = Lists.newArrayListWithCapacity(SOY_FILENAMES.size() + 1);
73 for (String filename : SOY_FILENAMES) {
74 allTemplates.add(resourceMapper.apply(filename));
75 }
76 if (customTemplates != null) {
77 allTemplates.add(customTemplates);
78 } else {
79 allTemplates.add(resourceMapper.apply("DefaultCustomTemplates.soy"));
80 }
81 templates = ImmutableList.copyOf(allTemplates);
82
83 Map<String, String> allGlobals = Maps.newHashMap();
84 for (Map.Entry<String, String> e : STATIC_URL_GLOBALS.entrySet()) {
85 allGlobals.put(e.getKey(), staticPrefix + e.getValue());
86 }
Chad Horohoe2a28d622012-11-12 11:56:59 -080087 allGlobals.put("gitiles.SITE_TITLE", siteTitle);
Dave Borowitz9de65952012-08-13 16:09:45 -070088 allGlobals.putAll(globals);
89 this.globals = ImmutableMap.copyOf(allGlobals);
90 }
91
92 public void render(HttpServletResponse res, String templateName) throws IOException {
93 render(res, templateName, ImmutableMap.<String, Object> of());
94 }
95
96 public void render(HttpServletResponse res, String templateName, Map<String, ?> soyData)
97 throws IOException {
98 res.setContentType("text/html");
99 res.setCharacterEncoding("UTF-8");
100 byte[] data = newRenderer(templateName).setData(soyData).render().getBytes(Charsets.UTF_8);
101 res.setContentLength(data.length);
102 res.getOutputStream().write(data);
103 }
104
105 SoyTofu.Renderer newRenderer(String templateName) {
106 return getTofu().newRenderer(templateName);
107 }
108
109 protected abstract SoyTofu getTofu();
110}