1 /* 2 * $Id: FreemarkerUtil.java 1360343 2012-07-11 18:35:52Z nlebas $ 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 package org.apache.tiles.request.freemarker.autotag; 23 24 import freemarker.template.TemplateModel; 25 import freemarker.template.TemplateModelException; 26 import freemarker.template.utility.DeepUnwrap; 27 28 /** 29 * Utilities for FreeMarker usage in Tiles. 30 * 31 * @version $Rev: 1360343 $ $Date: 2012-07-12 04:35:52 +1000 (Thu, 12 Jul 2012) $ 32 */ 33 public final class FreemarkerUtil { 34 35 /** 36 * Private constructor to avoid instantiation. 37 */ 38 private FreemarkerUtil() { 39 } 40 41 /** 42 * Unwraps a TemplateModel to extract an object. 43 * 44 * @param model The TemplateModel to unwrap. 45 * @param defaultValue The default value, as specified in the template 46 * model, or null if not specified. 47 * @return The unwrapped object. 48 */ 49 public static <T> T getAsObject(TemplateModel model, Class<T> type, T defaultValue) { 50 try { 51 T retValue = defaultValue; 52 if (model != null) { 53 @SuppressWarnings("unchecked") 54 T value = (T) DeepUnwrap.unwrap(model); 55 if (value != null) { 56 retValue = value; 57 } 58 } 59 return retValue; 60 } catch (TemplateModelException e) { 61 throw new FreemarkerAutotagException("Cannot unwrap a model", e); 62 } 63 } 64 }