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.module.templating.engine;
35
36 import java.io.IOException;
37 import java.io.Writer;
38
39 import info.magnolia.cms.core.AggregationState;
40 import info.magnolia.cms.core.Content;
41 import info.magnolia.cms.core.ItemType;
42 import info.magnolia.context.MgnlContext;
43 import info.magnolia.module.templating.Paragraph;
44 import info.magnolia.module.templating.ParagraphManager;
45 import info.magnolia.module.templating.ParagraphRenderer;
46 import info.magnolia.module.templating.ParagraphRendererManager;
47 import info.magnolia.module.templating.RenderException;
48 import info.magnolia.module.templating.RenderableDefinition;
49 import info.magnolia.module.templating.Template;
50 import info.magnolia.module.templating.TemplateManager;
51 import info.magnolia.module.templating.TemplateRenderer;
52 import info.magnolia.module.templating.TemplateRendererManager;
53
54
55
56
57
58
59
60
61
62
63 public class DefaultRenderingEngine implements RenderingEngine {
64
65 protected enum RenderingHelper {
66 PARAGRAPH {
67
68 public RenderableDefinition getDefinition(String definitionName) {
69 return ParagraphManager.getInstance().getParagraphDefinition(definitionName);
70 }
71
72 public Object getRenderer(RenderableDefinition definition) {
73 return ParagraphRendererManager.getInstance().getRenderer(definition.getType());
74 }
75
76 void render(Content content, RenderableDefinition definition, Object renderer, Writer out) throws RenderException, IOException {
77 ((ParagraphRenderer) renderer).render(content, (Paragraph) definition, out);
78 }
79 },
80
81 TEMPLATE {
82
83 public RenderableDefinition getDefinition(String definitionName) {
84 AggregationState state = getAggregationStateSafely();
85 String extension = null;
86 if (state != null) {
87 extension = state.getExtension();
88 }
89 Template template = TemplateManager.getInstance().getTemplateDefinition(definitionName);
90 if (template != null && extension != null) {
91 Template subTemplate = template.getSubTemplate(extension);
92 if (subTemplate != null) {
93 template = subTemplate;
94 }
95 }
96 return template;
97
98 }
99
100 public Object getRenderer(RenderableDefinition definition) {
101 return TemplateRendererManager.getInstance().getRenderer(definition.getType());
102 }
103
104 public void render(Content content, RenderableDefinition definition, Object renderer, Writer out) throws RenderException, IOException {
105 ((TemplateRenderer) renderer).renderTemplate(content, (Template) definition, out);
106 }
107 };
108
109 abstract RenderableDefinition getDefinition(String definitionName);
110
111 abstract Object getRenderer(RenderableDefinition definition);
112
113 abstract void render(Content content, RenderableDefinition definition, Object renderer, Writer out) throws RenderException, IOException;
114 }
115
116 public void render(Content content, Writer out) throws RenderException {
117 render(content, determineAssignedDefinitionName(content), out);
118 }
119
120 public void render(Content content, String definitionName, Writer out) throws RenderException {
121
122
123 if (content != null && content.isNodeType(ItemType.CONTENTNODE.getSystemName())) {
124 render(content, definitionName, RenderingHelper.PARAGRAPH, out);
125 }
126 else {
127 render(content, definitionName, RenderingHelper.TEMPLATE, out);
128 }
129 }
130
131
132
133
134 protected String determineAssignedDefinitionName(Content content) {
135 return content.getMetaData().getTemplate();
136 }
137
138
139
140
141 protected void render(Content content, String definitionName, RenderingHelper helper, Writer out) throws RenderException {
142 Content orgMainContent = null;
143 Content orgCurrentContent = null;
144
145 AggregationState state = getAggregationStateSafely();
146 if (state != null) {
147 orgMainContent = state.getMainContent();
148 orgCurrentContent = state.getCurrentContent();
149
150 state.setCurrentContent(content);
151
152 if (orgMainContent == null) {
153 state.setMainContent(content);
154 }
155 }
156
157 RenderableDefinition definition = helper.getDefinition(definitionName);
158 if (definition == null) {
159 throw new RenderException("Can't find renderable definition " + definitionName);
160 }
161
162 Object renderer = helper.getRenderer(definition);
163 if (renderer == null) {
164 throw new RenderException("Can't find renderer for type " + definition.getType());
165 }
166
167 try {
168 helper.render(content, definition, renderer, out);
169 }
170 catch (IOException e) {
171 throw new RenderException("Can't render " + content.getHandle(), e);
172 }
173
174 if (state != null) {
175 state.setMainContent(orgMainContent);
176 state.setCurrentContent(orgCurrentContent);
177 }
178 }
179
180 protected static AggregationState getAggregationStateSafely() {
181 if (MgnlContext.isWebContext()) {
182 return MgnlContext.getAggregationState();
183 }
184 return null;
185 }
186
187 }