View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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;
35  
36  import freemarker.cache.ClassTemplateLoader;
37  import freemarker.cache.MultiTemplateLoader;
38  import freemarker.cache.TemplateLoader;
39  import freemarker.template.ObjectWrapper;
40  import freemarker.template.TemplateExceptionHandler;
41  import freemarker.template.TemplateModel;
42  import freemarker.template.TemplateModelException;
43  import info.magnolia.freemarker.models.MagnoliaModelFactory;
44  import info.magnolia.freemarker.models.MagnoliaObjectWrapper;
45  import info.magnolia.objectfactory.Components;
46  
47  import java.util.ArrayList;
48  import java.util.HashMap;
49  import java.util.List;
50  import java.util.Map;
51  
52  /**
53   * Observed bean holding Freemarker configuration. Not to be confused with
54   * Freemarker's own {@link freemarker.template.Configuration}. This only exposes the few
55   * properties that Magnolia allows to configure and is able to handle properly.
56   * It also provides a few additional methods used internally.
57   *
58   * @see info.magnolia.freemarker.FreemarkerHelper
59   * @see info.magnolia.freemarker.models.MagnoliaObjectWrapper
60   * @see info.magnolia.freemarker.models.MagnoliaModelFactory
61   */
62  public class FreemarkerConfig {
63  
64      /**
65       * @deprecated since 4.3 should not be needed - components using this can keep their instance
66       */
67      public static FreemarkerConfig getInstance() {
68          return Components.getSingleton(FreemarkerConfig.class);
69      }
70  
71      /**
72       * The MagnoliaModelFactory implementations explicitly registered by modules.
73       */
74      private final List<MagnoliaModelFactory> registeredModelFactories;
75      private final List<TemplateLoader> templateLoaders;
76      private final Map<String, TemplateModel> sharedVariables;
77      private TemplateExceptionHandler templateExceptionHandler = new ModeDependentTemplateExceptionHandler();
78  
79      private ObjectWrapper objectWrapper;
80      private TemplateLoader multiTL;
81  
82      public FreemarkerConfig() {
83          this.registeredModelFactories = new ArrayList<MagnoliaModelFactory>();
84          this.templateLoaders = new ArrayList<TemplateLoader>();
85          this.sharedVariables = new HashMap<String, TemplateModel>();
86      }
87  
88      public ObjectWrapper getObjectWrapper() {
89          if (objectWrapper == null) {
90              objectWrapper = newObjectWrapper();
91          }
92          return objectWrapper;
93      }
94  
95      protected ObjectWrapper newObjectWrapper() {
96          return new MagnoliaObjectWrapper(this);
97      }
98  
99      public TemplateLoader getTemplateLoader() {
100         if (multiTL == null) {
101             // using getTemplateLoaders() instead of the variable to make sure we go to the proxied object!?
102             final List<TemplateLoader> loaders = getTemplateLoaders();
103             final int s = loaders.size();
104             // add a ClassTemplateLoader as our last loader
105             final TemplateLoader[] tl = loaders.toArray(new TemplateLoader[s + 1]);
106             tl[s] = new ClassTemplateLoader(getClass(), "/");
107             multiTL = new MultiTemplateLoader(tl);
108         }
109         return multiTL;
110     }
111 
112     public List<MagnoliaModelFactory> getModelFactories() {
113         return registeredModelFactories;
114     }
115 
116     public void addModelFactory(MagnoliaModelFactory modelFactory) {
117         this.registeredModelFactories.add(modelFactory);
118     }
119 
120     public List<TemplateLoader> getTemplateLoaders() {
121         return templateLoaders;
122     }
123 
124     public void addTemplateLoader(TemplateLoader templateLoader) {
125         this.templateLoaders.add(templateLoader);
126     }
127 
128     public Map<String, TemplateModel> getSharedVariables() {
129         return sharedVariables;
130     }
131 
132     public void addSharedVariable(String name, Object value) throws TemplateModelException {
133         sharedVariables.put(name, getObjectWrapper().wrap(value));
134     }
135 
136     public TemplateExceptionHandler getTemplateExceptionHandler() {
137         return templateExceptionHandler;
138     }
139 
140     public void setTemplateExceptionHandler(TemplateExceptionHandler templateExceptionHandler) {
141         this.templateExceptionHandler = templateExceptionHandler;
142     }
143 }