1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.templating.elements;
35
36 import info.magnolia.cms.beans.config.ServerConfiguration;
37 import info.magnolia.context.MgnlContext;
38 import info.magnolia.context.WebContext;
39 import info.magnolia.jcr.inheritance.InheritanceNodeWrapper;
40 import info.magnolia.registry.RegistrationException;
41 import info.magnolia.rendering.context.RenderingContext;
42 import info.magnolia.rendering.engine.AppendableOnlyOutputProvider;
43 import info.magnolia.rendering.engine.RenderException;
44 import info.magnolia.rendering.engine.RenderingEngine;
45 import info.magnolia.rendering.template.TemplateDefinition;
46 import info.magnolia.rendering.template.assignment.TemplateDefinitionAssignment;
47
48 import java.io.IOException;
49 import java.util.HashMap;
50 import java.util.Map;
51
52 import javax.inject.Inject;
53 import javax.jcr.Node;
54
55 import org.apache.commons.lang.StringUtils;
56
57
58
59
60
61
62 public class ComponentElement extends AbstractContentTemplatingElement {
63
64 private Map<String, Object> contextAttributes = new HashMap<String, Object>();
65 private final RenderingEngine renderingEngine;
66 private Node content;
67 private TemplateDefinitionAssignment templateDefinitionAssignment;
68
69 private String dialog;
70
71 @Inject
72 public ComponentElement(ServerConfiguration server, RenderingContext renderingContext, RenderingEngine renderingEngine, TemplateDefinitionAssignment templateDefinitionAssignment ) {
73 super(server, renderingContext);
74 this.renderingEngine = renderingEngine;
75 this.templateDefinitionAssignment = templateDefinitionAssignment;
76 }
77
78 @Override
79 public void begin(Appendable out) throws IOException, RenderException {
80
81 TemplateDefinition componentDefinition = null;
82 content = getPassedContent();
83
84 if(content == null) {
85 throw new RenderException("The 'content' or 'workspace' and 'path' attribute have to be set to rendre a component.");
86 }
87
88 if(isAdmin()){
89 MarkupHelper helper = new MarkupHelper(out);
90
91 helper.openComment("cms:component");
92
93
94 helper.attribute("content", getNodePath(content));
95
96 if(content instanceof InheritanceNodeWrapper) {
97 if (((InheritanceNodeWrapper) content).isInherited()) {
98 helper.attribute("inherited", "true");
99 }
100 }
101
102 try {
103 componentDefinition = templateDefinitionAssignment.getAssignedTemplateDefinition(content);
104 } catch (RegistrationException e) {
105 throw new RenderException("No template definition found for the current content", e);
106 }
107 if(StringUtils.isEmpty(dialog)) {
108 dialog = resolveDialog(componentDefinition);
109 }
110 helper.attribute("dialog", dialog);
111 helper.append(" -->\n");
112 }
113
114
115
116 WebContext webContext = MgnlContext.getWebContext();
117 webContext.push(webContext.getRequest(), webContext.getResponse());
118 setAttributesInWebContext(contextAttributes, WebContext.LOCAL_SCOPE);
119
120
121 try {
122 if(componentDefinition != null) {
123 renderingEngine.render(content, componentDefinition, new HashMap<String, Object>(), new AppendableOnlyOutputProvider(out));
124 } else {
125 renderingEngine.render(content, new AppendableOnlyOutputProvider(out));
126 }
127 } finally {
128 webContext.pop();
129 webContext.setPageContext(null);
130 restoreAttributesInWebContext(contextAttributes, WebContext.LOCAL_SCOPE);
131 }
132 }
133
134 @Override
135 public void end(Appendable out) throws IOException, RenderException {
136 if(isAdmin()){
137 new MarkupHelper(out).closeComment("cms:component");
138 }
139 }
140
141 public Map<String, Object> getContextAttributes() {
142 return contextAttributes;
143 }
144
145 public void setContextAttributes(Map<String, Object> contextAttributes) {
146 this.contextAttributes = contextAttributes;
147 }
148
149 private String resolveDialog(TemplateDefinition component) {
150 if (StringUtils.isNotEmpty(this.dialog)) {
151 return this.dialog;
152 }
153 String dialog = component.getDialog();
154 if (StringUtils.isNotEmpty(dialog)) {
155 return dialog;
156 }
157 return null;
158 }
159
160 public void setDialog(String dialog) {
161 this.dialog = dialog;
162 }
163 }