View Javadoc

1   /**
2    * Copyright 2005-2013 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  
21  import org.kuali.rice.krad.uif.component.Component;
22  import org.springframework.util.StringUtils;
23  
24  import freemarker.core.Environment;
25  import freemarker.core.InlineTemplateAdaptor;
26  import freemarker.template.TemplateException;
27  
28  /**
29   * Inline FreeMarker template adaptor for supporting script.ftl 
30   * 
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class FreeMarkerScriptAdaptor implements InlineTemplateAdaptor, Serializable {
34  
35      private static final long serialVersionUID = 1675270336764602352L;
36  
37      /**
38       * Render a script template inline.
39       * 
40       * @see freemarker.core.InlineKradAdaptor#accept(freemarker.core.Environment)
41       */
42      @Override
43      public void accept(Environment env) throws TemplateException, IOException {
44          String script = FreeMarkerInlineRenderUtils.resolve(env, "value", String.class);
45  
46          if (!StringUtils.hasText(script)) {
47              return;
48          }
49  
50          Component component = FreeMarkerInlineRenderUtils.resolve(env, "component", Component.class);
51          String role = FreeMarkerInlineRenderUtils.resolve(env, "role", String.class);
52          FreeMarkerInlineRenderUtils.renderScript(script, component, role, env.getOut());
53      }
54  
55  }