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; |
35 |
|
|
36 |
|
import java.io.Writer; |
37 |
|
import java.util.HashMap; |
38 |
|
import java.util.Iterator; |
39 |
|
import java.util.Map; |
40 |
|
|
41 |
|
import org.apache.commons.lang.exception.ExceptionUtils; |
42 |
|
|
43 |
|
import info.magnolia.cms.core.Content; |
44 |
|
import info.magnolia.cms.core.AggregationState; |
45 |
|
import info.magnolia.cms.i18n.I18nContentWrapper; |
46 |
|
import info.magnolia.context.MgnlContext; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@author |
54 |
|
@version |
55 |
|
|
56 |
|
|
|
|
| 90.2% |
Uncovered Elements: 9 (92) |
Complexity: 24 |
Complexity Density: 0.39 |
|
57 |
|
public abstract class AbstractRenderer implements RenderingModelBasedRenderer { |
58 |
|
|
59 |
|
private static final String MODEL_ATTRIBUTE = RenderingModel.class.getName(); |
60 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
61 |
13
|
public AbstractRenderer() {... |
62 |
|
} |
63 |
|
|
|
|
| 76.9% |
Uncovered Elements: 6 (26) |
Complexity: 4 |
Complexity Density: 0.2 |
|
64 |
9
|
protected void render(Content content, RenderableDefinition definition, Writer out) throws RenderException {... |
65 |
|
|
66 |
9
|
final RenderingModel parentModel = (RenderingModel) MgnlContext.getAttribute(MODEL_ATTRIBUTE); |
67 |
|
|
68 |
8
|
RenderingModel model; |
69 |
8
|
String actionResult; |
70 |
|
|
71 |
8
|
model = (RenderingModel) MgnlContext.getAttribute(ModelExecutionFilter.MODEL_ATTRIBUTE_PREFIX + content.getUUID()); |
72 |
|
|
73 |
8
|
if (model == null) { |
74 |
|
|
75 |
8
|
model = newModel(content, definition, parentModel); |
76 |
|
|
77 |
8
|
actionResult = model.execute(); |
78 |
|
|
79 |
8
|
if (RenderingModel.SKIP_RENDERING.equals(actionResult)) { |
80 |
2
|
return; |
81 |
|
} |
82 |
|
} else { |
83 |
0
|
actionResult = (String) MgnlContext.getAttribute(ModelExecutionFilter.ACTION_RESULT_ATTRIBUTE_PREFIX + content.getUUID()); |
84 |
0
|
if (model instanceof EarlyExecutionAware) |
85 |
0
|
((EarlyExecutionAware)model).setParent(parentModel); |
86 |
|
} |
87 |
|
|
88 |
6
|
String templatePath = determineTemplatePath(content, definition, model, actionResult); |
89 |
|
|
90 |
4
|
final Map ctx = newContext(); |
91 |
3
|
final Map savedContextState = saveContextState(ctx); |
92 |
3
|
setupContext(ctx, content, definition, model, actionResult); |
93 |
3
|
MgnlContext.setAttribute(MODEL_ATTRIBUTE, model); |
94 |
3
|
onRender(content, definition, out, ctx, templatePath); |
95 |
3
|
MgnlContext.setAttribute(MODEL_ATTRIBUTE, parentModel); |
96 |
|
|
97 |
3
|
restoreContext(ctx, savedContextState); |
98 |
|
} |
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
100 |
6
|
protected String determineTemplatePath(Content content, RenderableDefinition definition, RenderingModel model, final String actionResult) {... |
101 |
6
|
String templatePath = definition.determineTemplatePath(actionResult, model); |
102 |
|
|
103 |
6
|
if (templatePath == null) { |
104 |
2
|
throw new IllegalStateException("Unable to render " + definition.getClass().getName() + " " + definition.getName() + " in page " + content.getHandle() + ": templatePath not set."); |
105 |
|
} |
106 |
4
|
return templatePath; |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
112 |
8
|
public RenderingModel newModel(Content content, RenderableDefinition definition, RenderingModel parentModel) throws RenderException {... |
113 |
8
|
try { |
114 |
8
|
final Content wrappedContent = wrapNodeForModel(content, getMainContentSafely(content)); |
115 |
8
|
return definition.newModel(wrappedContent, definition, parentModel); |
116 |
|
} catch (Exception e) { |
117 |
0
|
throw new RenderException("Can't create rendering model: " + ExceptionUtils.getRootCauseMessage(e), e); |
118 |
|
} |
119 |
|
} |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
121 |
3
|
protected Map saveContextState(final Map ctx) {... |
122 |
3
|
Map state = new HashMap(); |
123 |
|
|
124 |
3
|
saveAttribute(ctx, state, "content"); |
125 |
3
|
saveAttribute(ctx, state, "def"); |
126 |
3
|
saveAttribute(ctx, state, "state"); |
127 |
3
|
saveAttribute(ctx, state, "mgnl"); |
128 |
3
|
saveAttribute(ctx, state, "model"); |
129 |
3
|
saveAttribute(ctx, state, "actionResult"); |
130 |
|
|
131 |
3
|
saveAttribute(ctx, state, getPageAttributeName()); |
132 |
3
|
return state; |
133 |
|
} |
134 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
135 |
24
|
protected void saveAttribute(final Map ctx, Map state, String name) {... |
136 |
24
|
state.put(name, ctx.get(name)); |
137 |
|
} |
138 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
139 |
3
|
protected void restoreContext(final Map ctx, Map state) {... |
140 |
27
|
for (Iterator iterator = state.keySet().iterator(); iterator.hasNext();) { |
141 |
24
|
String name = (String) iterator.next(); |
142 |
24
|
setContextAttribute(ctx, name, state.get(name)); |
143 |
|
} |
144 |
|
} |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
146 |
5
|
protected void setupContext(final Map ctx, Content content, RenderableDefinition definition, RenderingModel model, Object actionResult){... |
147 |
5
|
final Content mainContent = getMainContentSafely(content); |
148 |
|
|
149 |
5
|
setContextAttribute(ctx, getPageAttributeName(), wrapNodeForTemplate(mainContent, mainContent)); |
150 |
5
|
setContextAttribute(ctx, "content", wrapNodeForTemplate(content, mainContent)); |
151 |
5
|
setContextAttribute(ctx, "def", definition); |
152 |
5
|
setContextAttribute(ctx, "state", getAggregationStateSafely()); |
153 |
5
|
setContextAttribute(ctx, "mgnl", getMagnoliaTemplatingUtilities()); |
154 |
5
|
setContextAttribute(ctx, "model", model); |
155 |
5
|
setContextAttribute(ctx, "actionResult", actionResult); |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
161 |
13
|
protected Content getMainContentSafely(Content current) {... |
162 |
13
|
AggregationState state = getAggregationStateSafely(); |
163 |
13
|
if(state != null){ |
164 |
12
|
return state.getMainContent(); |
165 |
|
} |
166 |
1
|
return current; |
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
|
@deprecated |
171 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
172 |
0
|
protected AggregationState getAggrigationStateSafely() {... |
173 |
0
|
return getAggregationStateSafely(); |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
179 |
18
|
protected AggregationState getAggregationStateSafely() {... |
180 |
18
|
if(MgnlContext.isWebContext()){ |
181 |
17
|
return MgnlContext.getAggregationState(); |
182 |
|
} |
183 |
1
|
return null; |
184 |
|
} |
185 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
186 |
5
|
protected MagnoliaTemplatingUtilities getMagnoliaTemplatingUtilities() {... |
187 |
5
|
return MagnoliaTemplatingUtilities.getInstance(); |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
@param |
193 |
|
@param |
194 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
195 |
8
|
protected Content wrapNodeForModel(Content currentContent, Content mainContent) {... |
196 |
8
|
return new I18nContentWrapper(currentContent); |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
@param |
202 |
|
@param |
203 |
|
@see |
204 |
|
|
205 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
206 |
10
|
protected Content wrapNodeForTemplate(Content currentContent, Content mainContent) {... |
207 |
10
|
return new I18nContentWrapper(currentContent); |
208 |
|
} |
209 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
210 |
62
|
protected Object setContextAttribute(final Map ctx, final String name, Object value) {... |
211 |
62
|
return ctx.put(name, value); |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
217 |
6
|
protected String getPageAttributeName() {... |
218 |
6
|
return "page"; |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
protected abstract Map newContext(); |
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
@param |
229 |
|
|
230 |
|
protected abstract void onRender(Content content, RenderableDefinition definition, Writer out, Map ctx, String templatePath) throws RenderException; |
231 |
|
|
232 |
|
} |