forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.java
More file actions
123 lines (108 loc) · 3.6 KB
/
View.java
File metadata and controls
123 lines (108 loc) · 3.6 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
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.app.event.SysEventId;
import act.plugin.AppServicePlugin;
import act.util.ActContext;
import org.osgl.http.H;
import org.osgl.util.C;
import org.osgl.util.IO;
import org.osgl.util.S;
import java.io.File;
import java.util.List;
/**
* The base class that different View solution should extends
*/
public abstract class View extends AppServicePlugin {
/**
* Returns the View solution's name. Recommended name should
* be in lower case characters. E.g. freemarker, velocity,
* rythm etc
*/
public abstract String name();
@Override
protected void applyTo(final App app) {
Act.viewManager().register(this);
app.jobManager().on(SysEventId.CLASS_LOADER_INITIALIZED, new Runnable() {
@Override
public void run() {
init(app);
}
});
}
public boolean appliedTo(ActContext context) {
H.Format format = context.accept();
return format.isText();
}
/**
* Sub class must implement this method to load the template
*
* @param resourcePath the path to the template
* @param context the view context
* @return the template instance or {@code null} if template not found
*/
// TODO it shall not need context to load template, revise this interface to remove it
protected abstract Template loadTemplate(String resourcePath);
/**
* Sub class must implement this method to load the template from string literal
*
* @param content the template content string literal
* @param context the view context
* @return the template instance
* @since 1.6
*/
protected abstract Template loadInlineTemplate(String content);
/**
* Sub class could use this method initialize the implementation
*/
protected void init(App app) {
}
protected void reload(App app) {
init(app);
}
protected final String templateHome() {
String templateHome = Act.appConfig().templateHome();
if (S.blank(templateHome) || "default".equals(templateHome)) {
templateHome = "/" + name();
}
return templateHome;
}
/**
* Load template content.
* <p>
* This method is used by error reporting feature when app running in dev mode
*
* @param template the template path
* @return the template content in lines
*/
public List<String> loadContent(String template) {
File file = new File(templateRootDir(), template);
if (file.exists() && file.canRead()) {
return IO.readLines(file);
}
return C.list();
}
protected final File templateRootDir() {
App app = Act.app();
return new File(app.layout().resource(app.base()), templateHome());
}
}