001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.freemarker;
016    
017    import com.liferay.portal.kernel.templateparser.TemplateNode;
018    
019    import freemarker.ext.beans.BeanModel;
020    import freemarker.ext.beans.BeansWrapper;
021    
022    import freemarker.template.ObjectWrapper;
023    import freemarker.template.SimpleHash;
024    import freemarker.template.TemplateModel;
025    import freemarker.template.TemplateModelException;
026    
027    /**
028     * @author Mika Koivisto
029     */
030    public class LiferayTemplateModel extends SimpleHash {
031    
032            public LiferayTemplateModel(
033                    TemplateNode templateNode, ObjectWrapper objectWrapper) {
034    
035                    super(templateNode, objectWrapper);
036    
037                    _beanModel = new BeanModel(templateNode, (BeansWrapper)objectWrapper);
038            }
039    
040            @Override
041            public TemplateModel get(String key) throws TemplateModelException {
042                    TemplateModel templateModel = super.get(key);
043    
044                    if (templateModel != null) {
045                            return templateModel;
046                    }
047    
048                    return _beanModel.get(key);
049            }
050    
051            private BeanModel _beanModel;
052    
053    }