1 | |
package org.apache.fulcrum.template; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
import java.io.File; |
25 | |
import java.io.OutputStream; |
26 | |
import java.io.Writer; |
27 | |
import java.util.ArrayList; |
28 | |
import java.util.Hashtable; |
29 | |
import java.util.List; |
30 | |
|
31 | |
import org.apache.avalon.framework.activity.Disposable; |
32 | |
import org.apache.avalon.framework.configuration.Configurable; |
33 | |
import org.apache.avalon.framework.configuration.Configuration; |
34 | |
import org.apache.avalon.framework.configuration.ConfigurationException; |
35 | |
import org.apache.avalon.framework.context.Context; |
36 | |
import org.apache.avalon.framework.context.ContextException; |
37 | |
import org.apache.avalon.framework.context.Contextualizable; |
38 | |
import org.apache.avalon.framework.logger.AbstractLogEnabled; |
39 | |
import org.apache.avalon.framework.service.ServiceException; |
40 | |
import org.apache.avalon.framework.service.ServiceManager; |
41 | |
import org.apache.avalon.framework.service.Serviceable; |
42 | |
import org.apache.avalon.framework.thread.ThreadSafe; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 0 | public abstract class BaseTemplateEngineService |
53 | |
extends AbstractLogEnabled |
54 | |
implements |
55 | |
TemplateEngineService, |
56 | |
Configurable, |
57 | |
Contextualizable, |
58 | |
ThreadSafe, |
59 | |
Disposable, |
60 | |
Serviceable |
61 | |
{ |
62 | |
|
63 | 0 | private ServiceManager manager = null; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
protected String applicationRoot; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | 0 | private Hashtable configuration = new Hashtable(); |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public Hashtable getTemplateEngineServiceConfiguration() |
87 | |
{ |
88 | 0 | return configuration; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public String[] getAssociatedFileExtensions() |
95 | |
{ |
96 | 0 | return (String[]) configuration.get(TEMPLATE_EXTENSIONS); |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public abstract boolean templateExists(String template); |
103 | |
|
104 | |
public abstract String handleRequest( |
105 | |
TemplateContext context, |
106 | |
String template) |
107 | |
throws TemplateException; |
108 | |
|
109 | |
public abstract void handleRequest( |
110 | |
TemplateContext context, |
111 | |
String template, |
112 | |
OutputStream os) |
113 | |
throws TemplateException; |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public abstract void handleRequest( |
119 | |
TemplateContext context, |
120 | |
String template, |
121 | |
Writer writer) |
122 | |
throws TemplateException; |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
protected void registerConfiguration(Configuration conf, String defaultExt) |
130 | |
throws ConfigurationException |
131 | |
{ |
132 | 0 | initConfiguration(conf, defaultExt); |
133 | |
|
134 | 0 | TemplateService component = null; |
135 | |
try |
136 | |
{ |
137 | 0 | component = (TemplateService) manager.lookup(TemplateService.ROLE); |
138 | 0 | component.registerTemplateEngineService(this); |
139 | |
} |
140 | 0 | catch (ServiceException ce) |
141 | |
{ |
142 | 0 | throw new RuntimeException(ce.getMessage()); |
143 | |
} |
144 | |
finally |
145 | |
{ |
146 | 0 | if (component != null) |
147 | |
{ |
148 | 0 | manager.release(component); |
149 | |
} |
150 | |
} |
151 | 0 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
private void initConfiguration(Configuration conf, String defaultExt) |
165 | |
throws ConfigurationException |
166 | |
{ |
167 | |
|
168 | 0 | List extensionList = new ArrayList(); |
169 | 0 | final Configuration[] extensions = |
170 | |
conf.getChildren("template-extension"); |
171 | 0 | if (extensions != null) |
172 | |
{ |
173 | 0 | for (int i = 0; i < extensions.length; i++) |
174 | |
{ |
175 | 0 | extensionList.add(extensions[i].getValue()); |
176 | |
} |
177 | |
} |
178 | |
|
179 | 0 | String[] fileExtensionAssociations = |
180 | |
(String[]) extensionList.toArray(new String[extensionList.size()]); |
181 | |
|
182 | 0 | if (fileExtensionAssociations == null |
183 | |
|| fileExtensionAssociations.length == 0) |
184 | |
{ |
185 | 0 | Configuration defaultConf = |
186 | |
conf.getChild("default-template-extension", true); |
187 | 0 | fileExtensionAssociations = new String[1]; |
188 | 0 | fileExtensionAssociations[0] = defaultConf.getValue(defaultExt); |
189 | |
} |
190 | |
|
191 | 0 | configuration.put(TEMPLATE_EXTENSIONS, fileExtensionAssociations); |
192 | |
|
193 | 0 | configuration.put( |
194 | |
DEFAULT_PAGE_TEMPLATE, |
195 | |
conf.getChild("default-page-template", true).getValue()); |
196 | |
|
197 | 0 | configuration.put( |
198 | |
DEFAULT_LAYOUT_TEMPLATE, |
199 | |
conf.getChild("default-layout-template", true).getValue()); |
200 | 0 | } |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
public String getRealPath(String path) |
206 | |
{ |
207 | 0 | String absolutePath = null; |
208 | 0 | if (applicationRoot == null) |
209 | |
{ |
210 | 0 | absolutePath = new File(path).getAbsolutePath(); |
211 | |
} |
212 | |
else |
213 | |
{ |
214 | 0 | absolutePath = new File(applicationRoot, path).getAbsolutePath(); |
215 | |
} |
216 | |
|
217 | 0 | return absolutePath; |
218 | |
} |
219 | |
|
220 | |
|
221 | |
|
222 | |
public void contextualize(Context context) throws ContextException |
223 | |
{ |
224 | 0 | this.applicationRoot = context.get("urn:avalon:home").toString(); |
225 | 0 | } |
226 | |
|
227 | |
|
228 | |
|
229 | |
public void service(ServiceManager manager) throws ServiceException |
230 | |
{ |
231 | 0 | this.manager = manager; |
232 | |
|
233 | 0 | } |
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
public void dispose() |
239 | |
{ |
240 | 0 | manager = null; |
241 | 0 | } |
242 | |
} |