1 /** 2 * This file Copyright (c) 2015-2018 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 info.magnolia.context.Context; 37 38 import java.io.IOException; 39 import java.io.Writer; 40 import java.util.Map; 41 42 import freemarker.cache.ClassTemplateLoader; 43 import freemarker.ext.beans.BeansWrapper; 44 import freemarker.ext.beans.MapModel; 45 import freemarker.ext.util.ModelFactory; 46 import freemarker.template.Configuration; 47 import freemarker.template.DefaultObjectWrapper; 48 import freemarker.template.ObjectWrapper; 49 import freemarker.template.TemplateException; 50 import freemarker.template.TemplateExceptionHandler; 51 import freemarker.template.TemplateModel; 52 import freemarker.template.TemplateModelException; 53 import freemarker.template.Version; 54 55 /** 56 * A simple utility to render FreeMarker templates for those rare cases in magnolia-core that can't be extracted (yet), 57 * and thus can't use {@link info.magnolia.freemarker.FreemarkerHelper} of magnolia-freemarker-support or 58 * {@link info.magnolia.rendering.engine.RenderingEngine} of magnolia-rendering. 59 * 60 * <p>It only loads templates on the classpath and, unlike FreemarkerHelper, is simply not configurable. 61 * It does not put _any_ variables in the templateContext, so you have to add them all explicitly. 62 * This class might disappear if its current uses can be split out of core.</p> 63 * 64 * @see <a href="https://wiki.magnolia-cms.com/display/DEV/Core+split">Core split</a> 65 * @see <a href="https://jira.magnolia-cms.com/browse/MAGNOLIA-6253">MAGNOLIA-6253</a> 66 */ 67 public class SimpleFreemarkerHelper { 68 69 private final Configuration cfg; 70 71 /** 72 * If relative is true, templates will be loaded from the base class' package; if false, the paths will be relative to the root of the classpath. 73 */ 74 public SimpleFreemarkerHelper(Class<?> baseClass, boolean relative) { 75 final String basePath = relative ? "" : "/"; // "" will start at current class, "/" will start at root 76 cfg = new Configuration(Configuration.VERSION_2_3_28); 77 cfg.setObjectWrapper(new SimpleFreemarkerHelperObjectWrapper(Configuration.VERSION_2_3_28)); 78 cfg.setTemplateLoader(new ClassTemplateLoader(baseClass, basePath)); 79 cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); 80 cfg.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX); 81 cfg.setDefaultEncoding("UTF-8"); 82 cfg.setURLEscapingCharset("UTF-8"); 83 } 84 85 public void render(String template, Map<String, ?> templateContext, Writer out) throws IOException, TemplateException { 86 cfg.getTemplate(template).process(templateContext, out); 87 } 88 89 /** 90 * Custom {@link DefaultObjectWrapper} that enables us to wrap the {@link Context} in order to make it "available" 91 * in templates. 92 */ 93 private static class SimpleFreemarkerHelperObjectWrapper extends DefaultObjectWrapper { 94 95 public SimpleFreemarkerHelperObjectWrapper(Version version) { 96 super(version); 97 } 98 99 /** 100 * In order to use a custom model factory for {@link Context} we need to intercept the call to 101 * {@link DefaultObjectWrapper#wrap(Object)} because wrap would otherwise return a 102 * {@link freemarker.template.SimpleHash} for our context as it implements a {@link java.util.Map}. 103 * 104 * <p><i>Note</i>: we only want to use {@link #handleUnknownType(Object)} for our custom 105 * {@link ModelFactory ModelFactories} which is why we introduce/use {@link #getCustomModelFactory(Class)}.</p> 106 * 107 * <p>This behavior is heavily based on what happens in 108 * {@link info.magnolia.freemarker.models.MagnoliaObjectWrapper}.</p> 109 */ 110 @Override 111 public TemplateModel wrap(Object obj) throws TemplateModelException { 112 if (obj != null) { 113 final Class<?> clazz = obj.getClass(); 114 final ModelFactory modelFactory = getCustomModelFactory(clazz); 115 if (modelFactory != null) { 116 return handleUnknownType(obj); 117 } 118 } 119 120 return super.wrap(obj); 121 } 122 123 @Override 124 protected ModelFactory getModelFactory(Class clazz) { 125 final ModelFactory customModelFactory = getCustomModelFactory(clazz); 126 if (customModelFactory != null) { 127 return customModelFactory; 128 } 129 return super.getModelFactory(clazz); 130 } 131 132 /** 133 * This method returns any additional, custom {@link ModelFactory}. It allows us to bypass 134 * {@link DefaultObjectWrapper#wrap(Object)} *only* for our customs types returned here. 135 * 136 * @see #wrap(Object) 137 */ 138 private ModelFactory getCustomModelFactory(Class clazz) { 139 if (Context.class.isAssignableFrom(clazz)) { 140 return CONTEXT_MODEL_FACTORY; 141 } 142 return null; 143 } 144 145 /** 146 * @see info.magnolia.freemarker.models.ContextModelFactory 147 */ 148 static final ModelFactory CONTEXT_MODEL_FACTORY = new ModelFactory() { 149 @Override 150 public TemplateModel create(Object object, ObjectWrapper wrapper) { 151 return new MapModel((Map) object, (BeansWrapper) wrapper); 152 } 153 }; 154 155 } 156 }