| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| 17 | import static com.google.common.base.Preconditions.checkNotNull; |
| 18 | |
| 19 | import com.google.common.base.Charsets; |
| 20 | import com.google.common.base.Function; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.ImmutableMap; |
| 23 | import com.google.common.collect.Lists; |
| 24 | import com.google.common.collect.Maps; |
| 25 | import com.google.template.soy.tofu.SoyTofu; |
| 26 | |
| 27 | import java.io.File; |
| 28 | import java.io.IOException; |
| 29 | import java.net.MalformedURLException; |
| 30 | import java.net.URL; |
| 31 | import java.util.List; |
| 32 | import java.util.Map; |
| 33 | |
| 34 | import javax.servlet.http.HttpServletResponse; |
| 35 | |
| 36 | /** Renderer for Soy templates used by Gitiles. */ |
| 37 | public 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 Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame^] | 45 | "RefList.soy", |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 46 | "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 Horohoe | 2a28d62 | 2012-11-12 11:56:59 -0800 | [diff] [blame] | 69 | String staticPrefix, URL customTemplates, String siteTitle) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 70 | 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 Horohoe | 2a28d62 | 2012-11-12 11:56:59 -0800 | [diff] [blame] | 86 | allGlobals.put("gitiles.SITE_TITLE", siteTitle); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 87 | 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 | } |