001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.krad.uif.freemarker;
017
018import java.io.IOException;
019import java.io.Serializable;
020import java.util.Map;
021
022import org.kuali.rice.krad.uif.component.Component;
023
024import freemarker.core.Environment;
025import freemarker.core.InlineTemplateAdaptor;
026import freemarker.template.TemplateException;
027import freemarker.template.TemplateModel;
028
029/**
030 * Inline FreeMarker template adaptor for supporting template.ftl 
031 * 
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034public class FreeMarkerTemplateAdaptor implements InlineTemplateAdaptor, Serializable {
035
036    private static final long serialVersionUID = -4442716566711789593L;
037
038    /**
039     * Render a KRAD component template inline.
040     * 
041     * {@inheritDoc}
042     */
043    @Override
044    public void accept(Environment env) throws TemplateException, IOException {
045        Component component = FreeMarkerInlineRenderUtils.resolve(env, "component", Component.class);
046        
047        if (component == null) {
048            return;
049        }
050
051        String body = FreeMarkerInlineRenderUtils.resolve(env, "body", String.class);
052        boolean componentUpdate = Boolean.TRUE.equals(FreeMarkerInlineRenderUtils.resolve(env, "componentUpdate",
053                Boolean.class));
054        boolean includeSrc = Boolean.TRUE.equals(FreeMarkerInlineRenderUtils.resolve(env, "includeSrc", Boolean.class));
055        @SuppressWarnings("unchecked")
056        Map<String, TemplateModel> tmplParms = FreeMarkerInlineRenderUtils.resolve(env, "tmplParms", Map.class);
057        FreeMarkerInlineRenderUtils.renderTemplate(env, component, body, componentUpdate, includeSrc, tmplParms);
058    }
059
060}