blob: aa128694cd8a378db0df1e8693895507447385f6 [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(
39 "Common.soy",
40 "DiffDetail.soy",
41 "HostIndex.soy",
42 "LogDetail.soy",
43 "ObjectDetail.soy",
44 "PathDetail.soy",
Dave Borowitz209d0aa2012-12-28 14:28:53 -080045 "RefList.soy",
Dave Borowitz9de65952012-08-13 16:09:45 -070046 "RevisionDetail.soy",
47 "RepositoryIndex.soy");
48
49 public static final Map<String, String> STATIC_URL_GLOBALS = ImmutableMap.of(
50 "gitiles.CSS_URL", "gitiles.css",
51 "gitiles.PRETTIFY_CSS_URL", "prettify/prettify.css",
52 "gitiles.PRETTIFY_JS_URL", "prettify/prettify.js");
53
54 protected static final URL toFileURL(String filename) {
55 if (filename == null) {
56 return null;
57 }
58 try {
59 return new File(filename).toURI().toURL();
60 } catch (MalformedURLException e) {
61 throw new IllegalArgumentException(e);
62 }
63 }
64
65 protected ImmutableList<URL> templates;
66 protected ImmutableMap<String, String> globals;
67
68 protected Renderer(Function<String, URL> resourceMapper, Map<String, String> globals,
Chad Horohoe2a28d622012-11-12 11:56:59 -080069 String staticPrefix, URL customTemplates, String siteTitle) {
Dave Borowitz9de65952012-08-13 16:09:45 -070070 checkNotNull(staticPrefix, "staticPrefix");
71 List<URL> allTemplates = Lists.newArrayListWithCapacity(SOY_FILENAMES.size() + 1);
72 for (String filename : SOY_FILENAMES) {
73 allTemplates.add(resourceMapper.apply(filename));
74 }
75 if (customTemplates != null) {
76 allTemplates.add(customTemplates);
77 } else {
78 allTemplates.add(resourceMapper.apply("DefaultCustomTemplates.soy"));
79 }
80 templates = ImmutableList.copyOf(allTemplates);
81
82 Map<String, String> allGlobals = Maps.newHashMap();
83 for (Map.Entry<String, String> e : STATIC_URL_GLOBALS.entrySet()) {
84 allGlobals.put(e.getKey(), staticPrefix + e.getValue());
85 }
Chad Horohoe2a28d622012-11-12 11:56:59 -080086 allGlobals.put("gitiles.SITE_TITLE", siteTitle);
Dave Borowitz9de65952012-08-13 16:09:45 -070087 allGlobals.putAll(globals);
88 this.globals = ImmutableMap.copyOf(allGlobals);
89 }
90
91 public void render(HttpServletResponse res, String templateName) throws IOException {
92 render(res, templateName, ImmutableMap.<String, Object> of());
93 }
94
95 public void render(HttpServletResponse res, String templateName, Map<String, ?> soyData)
96 throws IOException {
97 res.setContentType("text/html");
98 res.setCharacterEncoding("UTF-8");
99 byte[] data = newRenderer(templateName).setData(soyData).render().getBytes(Charsets.UTF_8);
100 res.setContentLength(data.length);
101 res.getOutputStream().write(data);
102 }
103
104 SoyTofu.Renderer newRenderer(String templateName) {
105 return getTofu().newRenderer(templateName);
106 }
107
108 protected abstract SoyTofu getTofu();
109}