forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewManager.java
More file actions
317 lines (277 loc) · 9.58 KB
/
ViewManager.java
File metadata and controls
317 lines (277 loc) · 9.58 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package act.view;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.Act;
import act.app.App;
import act.conf.AppConfig;
import act.util.ActContext;
import act.util.DestroyableBase;
import org.osgl.$;
import org.osgl.exception.UnexpectedException;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.S;
import javax.enterprise.context.ApplicationScoped;
import java.util.*;
import static act.Destroyable.Util.tryDestroyAll;
/**
* Manage different view solutions
*/
public class ViewManager extends DestroyableBase {
private C.List<View> viewList = C.newList();
private Map<String, ActionViewVarDef> implicitActionViewVariables = new HashMap<>();
private Map<String, MailerViewVarDef> implicitMailerViewVariables = new HashMap<>();
private Map<String, VarDef> appDefined = new HashMap<>();
private Map<String, Template> templateCache = new HashMap<>();
private boolean multiViews = false;
void register(View view) {
E.NPE(view);
if (registered(view)) {
throw new UnexpectedException("View[%s] already registered", view.name());
}
viewList.add(view);
}
void register(ImplicitVariableProvider implicitVariableProvider) {
E.NPE(implicitVariableProvider);
List<ActionViewVarDef> l0 = implicitVariableProvider.implicitActionViewVariables();
for (ActionViewVarDef var : l0) {
_register(var, implicitActionViewVariables, false);
}
List<MailerViewVarDef> l1 = implicitVariableProvider.implicitMailerViewVariables();
for (MailerViewVarDef var : l1) {
_register(var, implicitMailerViewVariables, false);
}
}
void registerAppDefinedVar(ActionViewVarDef var) {
_register(var, implicitActionViewVariables, true);
}
void registerAppDefinedVar(MailerViewVarDef var) {
_register(var, implicitMailerViewVariables, true);
}
private <T extends VarDef> void _register(T var, Map<String, T> map, boolean fromApp) {
if (map.containsKey(var.name())) {
throw new UnexpectedException("Implicit variable[%s] has already been registered", var.name());
}
map.put(var.name(), var);
if (fromApp) {
appDefined.put(var.name(), var);
}
}
public void clearAppDefinedVars() {
for (String name : appDefined.keySet()) {
implicitActionViewVariables.remove(name);
implicitMailerViewVariables.remove(name);
}
appDefined.clear();
}
public void onAppStart() {
int viewCount = viewList.size();
multiViews = viewCount > 1;
}
public void reload(App app) {
for (View view : viewList) {
view.reload(app);
}
}
public View view(String name) {
$.Option<View> viewBag = findViewByName(name);
return viewBag.isDefined() ? viewBag.get() : null;
}
public Template load(ActContext context) {
Template cached = context.cachedTemplate();
if (null != cached) {
return cached;
}
AppConfig config = context.config();
Template template;
String templateContent = context.templateContent();
if (S.notEmpty(templateContent)) {
template = getInlineTemplate(context, config, templateContent);
} else {
TemplatePathResolver resolver = config.templatePathResolver();
String path = resolver.resolve(context);
template = getTemplate(context, config, path);
if (null == template) {
String amendedPath = resolver.resolveWithContextMethodPath(context);
if (S.neq(amendedPath, path)) {
template = getTemplate(context, config, amendedPath);
if (null != template) {
context.templatePath(amendedPath);
}
}
}
}
return template;
}
public Template getTemplate(String path) {
ActContext.Base ctx = ActContext.Base.currentContext();
if (null != ctx) {
String curPath = ctx.templatePath();
ctx.templateLiteral(path);
try {
return load(ctx);
} finally {
ctx.templatePath(curPath);
}
}
final String templatePath = S.ensureStartsWith(path, '/');
Template template = null;
View defView = Act.appConfig().defaultView();
if (null != defView) {
template = !isTemplatePath(path) ? defView.loadInlineTemplate(path) : defView.loadTemplate(templatePath);
}
if (null == template && multiViews) {
for (View view : viewList) {
if (view == defView) continue;
template = view.loadTemplate(templatePath);
if (null != template) {
break;
}
}
}
if (null != template) {
templateCache.put(path, template);
}
return template;
}
private Template getInlineTemplate(ActContext context, AppConfig config, String content) {
View defView = config.defaultView();
if (null != defView && defView.appliedTo(context)) {
return defView.loadInlineTemplate(content);
}
return null;
}
private Template getTemplate(ActContext context, AppConfig config, String path) {
Template template = cached(path);
if (null != template) {
return template;
}
final String templatePath = S.ensureStartsWith(path, '/');
View defView = config.defaultView();
if (null != defView && defView.appliedTo(context)) {
template = defView.loadTemplate(templatePath);
}
if (null == template && multiViews) {
for (View view : viewList) {
if (view == defView || !view.appliedTo(context)) continue;
template = view.loadTemplate(templatePath);
if (null != template) {
break;
}
}
}
if (null != template) {
context.cacheTemplate(template);
cache(path, template);
}
return template;
}
public Collection<ActionViewVarDef> implicitActionViewVariables() {
return implicitActionViewVariables.values();
}
public Collection<MailerViewVarDef> implicitMailerViewVariables() {
return implicitMailerViewVariables.values();
}
public void reset() {
viewList.clear();
templateCache.clear();
}
@Override
protected void releaseResources() {
tryDestroyAll(viewList, ApplicationScoped.class);
viewList = null;
implicitActionViewVariables.clear();
implicitActionViewVariables = null;
implicitMailerViewVariables.clear();
implicitMailerViewVariables = null;
templateCache.clear();
templateCache = null;
}
private boolean registered(View view) {
final String name = view.name().toUpperCase();
return findViewByName(name).isDefined();
}
private $.Option<View> findViewByName(final String name) {
return viewList.findFirst(new $.Predicate<View>() {
@Override
public boolean test(View view) {
return view.name().toUpperCase().equals(name.toUpperCase());
}
});
}
private Template cached(String key) {
return Act.isDev() ? null : templateCache.get(key);
}
private void cache(String key, Template template) {
if (Act.isProd()) {
templateCache.put(key, template);
}
}
/**
* Check if a given string is a template path or template content
*
* If the string contains anyone the following characters then we assume it
* is content, otherwise it is path:
*
* * space characters
* * non numeric-alphabetic characters except:
* ** dot "."
* ** dollar: "$"
*
* @param string the string to be tested
* @return `true` if the string literal is template content or `false` otherwise
*/
public static boolean isTemplatePath(String string) {
int sz = string.length();
if (sz == 0) {
return true;
}
for (int i = 0; i < sz; ++i) {
char c = string.charAt(i);
switch (c) {
case ' ':
case '\t':
case '\b':
case '<':
case '>':
case '(':
case ')':
case '[':
case ']':
case '{':
case '}':
case '!':
case '@':
case '#':
case '*':
case '?':
case '%':
case '|':
case ',':
case ':':
case ';':
case '^':
case '&':
return false;
}
}
return true;
}
}