View Javadoc

1   /*
2    *  Copyright 2001-2013 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.joda.beans.integrate.freemarker;
17  
18  
19  import org.joda.beans.Bean;
20  
21  import freemarker.template.DefaultObjectWrapper;
22  import freemarker.template.TemplateModel;
23  import freemarker.template.TemplateModelException;
24  
25  /**
26   * Freemarker support class for Joda-Beans.
27   * <p>
28   * This class allows Joda-Beans to be used in the Freemarker templating system.
29   * When creating a Freemarker {@code Configuration}, simply set call
30   * {@code setObjectWrapper(ObjectWrapper)} with an instance of this class.
31   */
32  public class FreemarkerObjectWrapper extends DefaultObjectWrapper {
33  
34      /**
35       * Creates a new instance.
36       */
37      public FreemarkerObjectWrapper() {
38      }
39  
40      //-------------------------------------------------------------------------
41      /**
42       * Overrides to trap instances of {@code Bean} and handle them.
43       * 
44       * @param obj  the object to wrap, not null
45       * @return the template model, not null
46       * @throws TemplateModelException if unable to create the model
47       */
48      @Override
49      public TemplateModel wrap(Object obj) throws TemplateModelException {
50          if (obj instanceof Bean) {
51              return new FreemarkerTemplateModel((Bean) obj, this);
52          }
53          return super.wrap(obj);
54      }
55  
56      //-----------------------------------------------------------------------
57      @Override
58      public String toString() {
59          return "FreemarkerObjectWrapper";
60      }
61  
62  }