001    /*
002     *  Copyright 2001-2013 Stephen Colebourne
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.joda.beans.integrate.freemarker;
017    
018    
019    import org.joda.beans.Bean;
020    
021    import freemarker.template.DefaultObjectWrapper;
022    import freemarker.template.TemplateModel;
023    import freemarker.template.TemplateModelException;
024    
025    /**
026     * Freemarker support class for Joda-Beans.
027     * <p>
028     * This class allows Joda-Beans to be used in the Freemarker templating system.
029     * When creating a Freemarker {@code Configuration}, simply set call
030     * {@code setObjectWrapper(ObjectWrapper)} with an instance of this class.
031     */
032    public class FreemarkerObjectWrapper extends DefaultObjectWrapper {
033    
034        /**
035         * Creates a new instance.
036         */
037        public FreemarkerObjectWrapper() {
038        }
039    
040        //-------------------------------------------------------------------------
041        /**
042         * Overrides to trap instances of {@code Bean} and handle them.
043         */
044        @Override
045        public TemplateModel wrap(Object obj) throws TemplateModelException {
046            if (obj instanceof Bean) {
047                return new FreemarkerTemplateModel((Bean) obj, this);
048            }
049            return super.wrap(obj);
050        }
051    
052        //-----------------------------------------------------------------------
053        @Override
054        public String toString() {
055            return "FreemarkerObjectWrapper";
056        }
057    
058    }