View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.uif.freemarker;
17  
18  import java.io.IOException;
19  import java.io.Serializable;
20  import java.util.Map;
21  
22  import org.kuali.rice.krad.uif.component.Component;
23  
24  import freemarker.core.Environment;
25  import freemarker.core.InlineTemplateAdaptor;
26  import freemarker.template.TemplateException;
27  import freemarker.template.TemplateModel;
28  
29  /**
30   * Inline FreeMarker template adaptor for supporting template.ftl 
31   * 
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  public class FreeMarkerTemplateAdaptor implements InlineTemplateAdaptor, Serializable {
35  
36      private static final long serialVersionUID = -4442716566711789593L;
37  
38      /**
39       * Render a KRAD component template inline.
40       * 
41       * {@inheritDoc}
42       */
43      @Override
44      public void accept(Environment env) throws TemplateException, IOException {
45          Component component = FreeMarkerInlineRenderUtils.resolve(env, "component", Component.class);
46          
47          if (component == null) {
48              return;
49          }
50  
51          String body = FreeMarkerInlineRenderUtils.resolve(env, "body", String.class);
52          boolean componentUpdate = Boolean.TRUE.equals(FreeMarkerInlineRenderUtils.resolve(env, "componentUpdate",
53                  Boolean.class));
54          boolean includeSrc = Boolean.TRUE.equals(FreeMarkerInlineRenderUtils.resolve(env, "includeSrc", Boolean.class));
55          @SuppressWarnings("unchecked")
56          Map<String, TemplateModel> tmplParms = FreeMarkerInlineRenderUtils.resolve(env, "tmplParms", Map.class);
57          FreeMarkerInlineRenderUtils.renderTemplate(env, component, body, componentUpdate, includeSrc, tmplParms);
58      }
59  
60  }