View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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 freemarker.ext.util.ModelFactory;
37  import freemarker.template.DefaultObjectWrapper;
38  import freemarker.template.SimpleDate;
39  import freemarker.template.TemplateDateModel;
40  import freemarker.template.TemplateModel;
41  import freemarker.template.TemplateModelException;
42  import info.magnolia.context.Context;
43  import info.magnolia.freemarker.FreemarkerConfig;
44  
45  import java.util.ArrayList;
46  import java.util.Calendar;
47  import java.util.List;
48  
49  /**
50   * A FreeMarker ObjectWrapper that knows about Magnolia specific objects.
51   *
52   * @author Chris Miner
53   * @author gjoseph
54   * @version $Revision: $ ($Author: $)
55   */
56  public class MagnoliaObjectWrapper extends DefaultObjectWrapper {
57      private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MagnoliaObjectWrapper.class);
58  
59      private final static List<MagnoliaModelFactory> DEFAULT_MODEL_FACTORIES = new ArrayList<MagnoliaModelFactory>() {{
60          add(NodeDataModelFactory.INSTANCE);
61          add(ContentModel.FACTORY);
62          add(CalendarModel.FACTORY);
63          add(UserModel.FACTORY);
64          add(ContextModelFactory.INSTANCE);
65      }};
66  
67      private final FreemarkerConfig freemarkerConfig;
68  
69      public MagnoliaObjectWrapper(FreemarkerConfig freemarkerConfig) {
70          super();
71          this.freemarkerConfig = freemarkerConfig;
72      }
73  
74      /**
75       * Unwraps our custom wrappers, let the default wrapper do the rest.
76       */
77      public Object unwrap(TemplateModel model, Class hint) throws TemplateModelException {
78          // all our models implement either AdapterTemplateModel or a already handled by super.unwrap() (boolean, string, ..)
79          return super.unwrap(model, hint);
80      }
81  
82      public TemplateModel wrap(Object obj) throws TemplateModelException {
83          if (obj instanceof Context) {
84              // bypass the default SimpleHash wrapping, we need a MapModel.
85              // handleUnknownType() will relay to our ContextModelFactory.
86              return handleUnknownType(obj);
87          }
88          return super.wrap(obj);
89      }
90  
91      /**
92       * Checks the ModelFactory instances registered in FreemarkerConfig, then
93       * the default ones. If no appropriate ModelFactory was found, delegates to
94       * Freemarker's implementation. These factories are cached by Freemarker,
95       * so this method only gets called once per type of object.
96       *
97       * @see #DEFAULT_MODEL_FACTORIES
98       * @see info.magnolia.freemarker.FreemarkerConfig
99       */
100     protected ModelFactory getModelFactory(Class clazz) {
101         final List<MagnoliaModelFactory> registeredModelFactories = freemarkerConfig.getModelFactories();
102         ModelFactory modelFactory = getModelFactory(clazz, registeredModelFactories);
103         
104         if (modelFactory == null) {
105             modelFactory = getModelFactory(clazz, DEFAULT_MODEL_FACTORIES);
106         }
107         if (modelFactory == null) {
108             modelFactory = super.getModelFactory(clazz);
109         }
110         return modelFactory;
111     }
112 
113     private ModelFactory getModelFactory(Class clazz, List<MagnoliaModelFactory> factories) {
114         for (MagnoliaModelFactory factory : factories) {
115             if (factory.factoryFor().isAssignableFrom(clazz)) {
116                 return factory;
117             }
118         }
119         return null;
120     }
121 
122     /**
123      * Exposes a Calendar as a SimpleDate.
124      * @deprecated since 4.3 use CalendarModel instead.
125      */
126     protected SimpleDate handleCalendar(Calendar cal) {
127         return new SimpleDate(cal.getTime(), TemplateDateModel.DATETIME);
128     }
129 
130 }