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