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 */ 016package org.joda.beans.integrate.freemarker; 017 018 019import org.joda.beans.Bean; 020 021import freemarker.template.DefaultObjectWrapper; 022import freemarker.template.TemplateModel; 023import 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 */ 032public 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 * @param obj the object to wrap, not null 045 * @return the template model, not null 046 * @throws TemplateModelException if unable to create the model 047 */ 048 @Override 049 public TemplateModel wrap(Object obj) throws TemplateModelException { 050 if (obj instanceof Bean) { 051 return new FreemarkerTemplateModel((Bean) obj, this); 052 } 053 return super.wrap(obj); 054 } 055 056 //----------------------------------------------------------------------- 057 @Override 058 public String toString() { 059 return "FreemarkerObjectWrapper"; 060 } 061 062}