1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.freemarker;
17
18 import java.io.IOException;
19 import java.util.Collections;
20
21 import org.kuali.rice.krad.uif.component.Component;
22 import org.kuali.rice.krad.uif.lifecycle.AbstractViewLifecycleTask;
23 import org.kuali.rice.krad.uif.lifecycle.ViewLifecycle;
24 import org.kuali.rice.krad.uif.lifecycle.ViewLifecyclePhase;
25
26 import freemarker.core.Environment;
27 import freemarker.core.Macro;
28 import freemarker.template.TemplateException;
29 import freemarker.template.TemplateModel;
30
31
32
33
34
35
36 public class RenderComponentTask extends AbstractViewLifecycleTask {
37
38
39
40
41
42
43 public RenderComponentTask(ViewLifecyclePhase phase) {
44 super(phase);
45 }
46
47
48
49
50 @Override
51 protected void performLifecycleTask() {
52 Component component = getPhase().getComponent();
53 LifecycleRenderingContext renderingContext = ViewLifecycle.getRenderingContext();
54 renderingContext.clearRenderingBuffer();
55
56 try {
57 Environment env = renderingContext.getEnvironment();
58
59
60
61 Macro fmMacro = (Macro) env.getMainNamespace().get(component.getTemplateName());
62
63 if (fmMacro == null) {
64 return;
65 }
66
67 String[] args = fmMacro.getArgumentNames();
68 if (args == null || args.length != 1 || !component.getComponentTypeName().equals(args[0])) {
69 return;
70 }
71
72 FreeMarkerInlineRenderUtils.renderTemplate(env, component,
73 null, false, false, Collections.<String, TemplateModel> emptyMap());
74 } catch (TemplateException e) {
75 throw new IllegalStateException("Error rendering component " + component.getId(), e);
76 } catch (IOException e) {
77 throw new IllegalStateException("Error rendering component " + component.getId(), e);
78 }
79
80 component.setSelfRendered(true);
81 component.setRenderedHtmlOutput(renderingContext.getRenderedOutput());
82 }
83
84 }