Skip to content

Package: NodeViewRenderer

NodeViewRenderer

nameinstructionbranchcomplexitylinemethod
NodeViewRenderer(Request, RequestContext, ViewAndControllerLayoutBuilder, BiConsumer)
M: 21 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
postVisit(Layout)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
preVisit(Layout)
M: 31 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
renderView(ViewFactory.ViewAndController, Layout)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * NorthernWind - lightweight CMS
5: * http://tidalwave.it/projects/northernwind
6: *
7: * Copyright (C) 2011 - 2025 Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/northernwind-src
22: * git clone https://github.com/tidalwave-it/northernwind-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.northernwind.frontend.ui.spi;
27:
28: import javax.annotation.Nonnull;
29: import javax.annotation.concurrent.NotThreadSafe;
30: import java.util.ArrayDeque;
31: import java.util.Deque;
32: import java.util.function.BiConsumer;
33: import it.tidalwave.role.Composite;
34: import it.tidalwave.northernwind.core.model.Request;
35: import it.tidalwave.northernwind.core.model.RequestContext;
36: import it.tidalwave.northernwind.frontend.ui.Layout;
37: import it.tidalwave.northernwind.frontend.ui.RenderContext;
38: import it.tidalwave.northernwind.frontend.ui.ViewFactory.ViewAndController;
39: import lombok.Getter;
40:
41: /***************************************************************************************************************************************************************
42: *
43: * A {@link Layout} visitor that renders everything for the given layout.
44: *
45: * @stereotype Visitor
46: *
47: * @author Fabrizio Giudici
48: *
49: **************************************************************************************************************************************************************/
50: @NotThreadSafe
51: public class NodeViewRenderer<T> implements Composite.Visitor<Layout, T>
52: {
53: @Nonnull
54: private final ViewAndControllerLayoutBuilder vacLayoutBuilder;
55:
56: @Nonnull
57: private final BiConsumer<? super T, ? super T> attacher;
58:
59: @Nonnull
60: private final RenderContext renderContext;
61:
62: private final Deque<T> componentStack = new ArrayDeque<>();
63:
64: @Getter
65: private T rootComponent;
66:
67: /***********************************************************************************************************************************************************
68: *
69: **********************************************************************************************************************************************************/
70: public NodeViewRenderer (@Nonnull final Request request,
71: @Nonnull final RequestContext requestContext,
72: @Nonnull final ViewAndControllerLayoutBuilder vacLayoutBuilder,
73: @Nonnull final BiConsumer<? super T, ? super T> attacher)
74: {
75: this.vacLayoutBuilder = vacLayoutBuilder;
76: this.attacher = attacher;
77: this.renderContext = new DefaultRenderContext(request, requestContext);
78: }
79:
80: /***********************************************************************************************************************************************************
81: * {@inheritDoc}
82: **********************************************************************************************************************************************************/
83: @Override
84: public void preVisit (@Nonnull final Layout layout)
85: {
86: final var vac = vacLayoutBuilder.getViewAndControllerFor(layout).get();
87: final var component = renderView(vac, layout);
88:
89:• if (rootComponent == null)
90: {
91: rootComponent = component;
92: }
93: else
94: {
95: attacher.accept(componentStack.peek(), component);
96: }
97:
98: componentStack.push(component);
99: }
100:
101: /***********************************************************************************************************************************************************
102: * {@inheritDoc}
103: **********************************************************************************************************************************************************/
104: @Override
105: public void postVisit (@Nonnull final Layout layout)
106: {
107: componentStack.pop();
108: }
109:
110: /***********************************************************************************************************************************************************
111: *
112: **********************************************************************************************************************************************************/
113: @Nonnull
114: private T renderView (@Nonnull final ViewAndController vac, @Nonnull final Layout layout)
115: {
116: try
117: {
118: return (T)vac.renderView(renderContext);
119: }
120: catch (Throwable e)
121: {
122: return (T)vacLayoutBuilder.getErrorViewSupplier().apply(layout, e);
123: }
124: }
125: }