View Javadoc
1   /**
2    * This file Copyright (c) 2003-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.freemarker.models;
35  
36  import info.magnolia.freemarker.FreemarkerConfig;
37  
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  import freemarker.ext.util.ModelFactory;
42  import freemarker.template.Configuration;
43  import freemarker.template.DefaultObjectWrapper;
44  import freemarker.template.TemplateModel;
45  import freemarker.template.TemplateModelException;
46  
47  /**
48   * A FreeMarker ObjectWrapper that knows about Magnolia specific objects.
49   */
50  public class MagnoliaObjectWrapper extends DefaultObjectWrapper {
51      private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MagnoliaObjectWrapper.class);
52  
53      private final static List<MagnoliaModelFactory> DEFAULT_MODEL_FACTORIES = new ArrayList<MagnoliaModelFactory>() {{
54          add(NodeDataModelFactory.INSTANCE);
55          add(ContentModel.FACTORY);
56          add(ContentMapModel.FACTORY);
57          add(CalendarModel.FACTORY);
58          add(UserModel.FACTORY);
59          add(ContextModelFactory.INSTANCE);
60      }};
61  
62      private final FreemarkerConfig freemarkerConfig;
63  
64      public MagnoliaObjectWrapper(FreemarkerConfig freemarkerConfig) {
65          super(Configuration.VERSION_2_3_28);
66          this.freemarkerConfig = freemarkerConfig;
67      }
68  
69      @Override
70      public TemplateModel wrap(Object obj) throws TemplateModelException {
71  
72          if (obj == null) {
73              return super.wrap(null);
74          }
75  
76          // wrap enums
77          // can't do this later, as the Class instance passed to getModelFactory() lost the information about its enum.
78          if ((obj instanceof Class) && ((Class) obj).isEnum()) {
79              final String enumClassName = ((Class) obj).getName();
80              return getEnumModels().get(enumClassName);
81          }
82  
83          // We let our own model factories have precedence over freemarker defaults, typically used to prevent classes
84          // implementing Map or Collection from being wrapped as SimpleHash and SimpleSequence. We intentionally don't
85          // consider model factories created in the super class method getModelFactory(Class) here because that would
86          // side step default wrapping in super.wrap(Object). I.e. the model factory returned for Map is a MapModel
87          // instead of SimpleHash returned by super.wrap(Object).
88          Class<?> clazz = obj.getClass();
89          ModelFactory modelFactory = getModelFactory(clazz, freemarkerConfig.getModelFactories());
90          if (modelFactory == null) {
91              modelFactory = getModelFactory(clazz, DEFAULT_MODEL_FACTORIES);
92          }
93          if (modelFactory != null) {
94              return handleUnknownType(obj);
95          }
96  
97          return super.wrap(obj);
98      }
99  
100     /**
101      * Checks the ModelFactory instances registered in FreemarkerConfig, then
102      * the default ones. If no appropriate ModelFactory was found, delegates to
103      * Freemarker's implementation. This is called by {@link freemarker.ext.beans.BeansModelCache},
104      * which is itself called by {@link freemarker.ext.beans.BeansWrapper#wrap}.
105      * These factories are cached by Freemarker, so this method only gets called
106      * once per type of object.
107      *
108      * @see #DEFAULT_MODEL_FACTORIES
109      * @see info.magnolia.freemarker.FreemarkerConfig
110      */
111     @Override
112     protected ModelFactory getModelFactory(Class clazz) {
113         final List<MagnoliaModelFactory> registeredModelFactories = freemarkerConfig.getModelFactories();
114         ModelFactory modelFactory = getModelFactory(clazz, registeredModelFactories);
115 
116         if (modelFactory == null) {
117             modelFactory = getModelFactory(clazz, DEFAULT_MODEL_FACTORIES);
118         }
119         if (modelFactory == null) {
120             modelFactory = super.getModelFactory(clazz);
121         }
122         return modelFactory;
123     }
124 
125     private ModelFactory getModelFactory(Class clazz, List<MagnoliaModelFactory> factories) {
126         for (MagnoliaModelFactory factory : factories) {
127             if (factory.factoryFor().isAssignableFrom(clazz)) {
128                 return factory;
129             }
130         }
131         return null;
132     }
133 }