View Javadoc
1   /**
2    * This file Copyright (c) 2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.util;
35  
36  import java.io.IOException;
37  import java.io.Writer;
38  import java.util.Map;
39  
40  import freemarker.cache.ClassTemplateLoader;
41  import freemarker.template.Configuration;
42  import freemarker.template.TemplateException;
43  import freemarker.template.TemplateExceptionHandler;
44  
45  /**
46   * A simple utility to render FreeMarker templates for those rare cases in magnolia-core that can't be extracted (yet),
47   * and thus can't use {@link info.magnolia.freemarker.FreemarkerHelper} of magnolia-freemarker-support or
48   * {@link info.magnolia.rendering.engine.RenderingEngine} of magnolia-rendering.
49   * It only loads templates on the classpath and, unlike FreemarkerHelper, is simply not configurable.
50   * It does not put _any_ variables in the templateContext, so you have to add them all explicitely.
51   * This class might disappear if its current uses can be split out of core.
52   * See https://wiki.magnolia-cms.com/display/DEV/Core+split and MAGNOLIA-6253.
53   */
54  public class SimpleFreemarkerHelper {
55  
56      private final Configuration cfg;
57  
58      /**
59       * If relative is true, templates wil be loaded from the base class' package; if false, the paths will be relative to the root of the classpath.
60       */
61      public SimpleFreemarkerHelper(Class<?> baseClass, boolean relative) {
62          final String basePath = relative ? "" : "/"; // "" will start at current class, "/" will start at root
63          //cfg = new Configuration(Configuration.VERSION_2_3_21);
64          cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
65  
66          cfg.setTemplateLoader(new ClassTemplateLoader(baseClass, basePath));
67          cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
68          cfg.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
69          cfg.setDefaultEncoding("UTF8");
70          cfg.setURLEscapingCharset("UTF8");
71      }
72  
73      public void render(String template, Map<String, ?> templateContext, Writer out) throws IOException, TemplateException {
74          cfg.getTemplate(template).process(templateContext, out);
75      }
76  }