1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
package org.webmacro.resource; |
24 | |
|
25 | |
import java.io.BufferedReader; |
26 | |
import java.io.File; |
27 | |
import java.io.IOException; |
28 | |
import java.io.InputStream; |
29 | |
import java.io.InputStreamReader; |
30 | |
import java.io.Reader; |
31 | |
import java.net.URL; |
32 | |
import java.util.HashMap; |
33 | |
import java.util.Locale; |
34 | |
import java.util.Properties; |
35 | |
|
36 | |
import org.slf4j.Logger; |
37 | |
import org.slf4j.LoggerFactory; |
38 | |
|
39 | |
import org.webmacro.Broker; |
40 | |
import org.webmacro.TemplateException; |
41 | |
import org.webmacro.WMConstants; |
42 | |
import org.webmacro.engine.WMTemplate; |
43 | |
import org.webmacro.servlet.LocaleTool; |
44 | |
import org.webmacro.util.NativeAsciiReader; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public class URLTemplate extends WMTemplate |
51 | |
{ |
52 | |
|
53 | 0 | static Logger _log = LoggerFactory.getLogger(URLTemplate.class); |
54 | |
|
55 | |
|
56 | |
public static final String RCS = "@(#) $Id: URLTemplate.java,v 1.13 2010/02/24 21:02:17 timp Exp $"; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
private final URL _url; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 0 | private File underLyingFile = null; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | 0 | private long underLyingFileLastModTime = 0; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | 0 | private final HashMap propertiesCache = new HashMap(); |
77 | |
|
78 | 0 | private String _inputEncoding = null; |
79 | 0 | private String _outputEncoding = null; |
80 | 0 | private Locale _outputLocale = null; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | 0 | private static final Object dummy = new Object(); |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public URLTemplate ( |
100 | |
Broker broker, |
101 | |
URL templateURL |
102 | |
) |
103 | |
{ |
104 | 0 | super(broker); |
105 | 0 | _url = templateURL; |
106 | 0 | String _u = _url.toExternalForm(); |
107 | |
|
108 | 0 | _log.debug("URLTemplate: " + _u); |
109 | |
|
110 | 0 | if (_u.startsWith("jar:")) |
111 | |
{ |
112 | 0 | int p = _u.indexOf("!"); |
113 | 0 | _u = _u.substring(4, p); |
114 | |
} |
115 | |
|
116 | 0 | if (_u.startsWith("file:")) |
117 | |
{ |
118 | 0 | underLyingFile = new File(_u.substring(5)); |
119 | 0 | underLyingFileLastModTime = underLyingFile.lastModified(); |
120 | |
} |
121 | |
|
122 | 0 | setupLocalProperties(); |
123 | 0 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
public boolean shouldReload () |
135 | |
{ |
136 | 0 | if (underLyingFile == null) return false; |
137 | 0 | long lastMod = underLyingFile.lastModified(); |
138 | |
|
139 | 0 | return ((lastMod > 0) && (lastMod > underLyingFileLastModTime)); |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
private void setupLocalProperties () |
151 | |
{ |
152 | 0 | InputStream is = null; |
153 | 0 | URL u = null; |
154 | |
|
155 | |
try |
156 | |
{ |
157 | |
|
158 | 0 | u = new URL(_url, WMConstants.WEBMACRO_LOCAL_FILE); |
159 | 0 | _log.debug("Looking for encodings file: " + u); |
160 | 0 | Object obj = propertiesCache.get(u); |
161 | |
|
162 | 0 | if (obj != null) |
163 | |
{ |
164 | |
return; |
165 | |
} |
166 | |
|
167 | 0 | Properties p = new Properties(); |
168 | 0 | is = u.openStream(); |
169 | 0 | p.load(is); |
170 | 0 | _inputEncoding = p.getProperty(WMConstants.TEMPLATE_INPUT_ENCODING); |
171 | 0 | _outputEncoding = p.getProperty(WMConstants.TEMPLATE_OUTPUT_ENCODING); |
172 | |
|
173 | 0 | String loc = p.getProperty(WMConstants.TEMPLATE_LOCALE); |
174 | 0 | if (loc != null) |
175 | |
{ |
176 | 0 | _outputLocale = LocaleTool.buildLocale(loc); |
177 | |
} |
178 | |
|
179 | 0 | propertiesCache.put(u, dummy); |
180 | |
|
181 | |
|
182 | |
} |
183 | 0 | catch (Exception e) |
184 | |
{ |
185 | |
|
186 | |
|
187 | |
|
188 | 0 | propertiesCache.put(u, dummy); |
189 | |
} |
190 | |
finally |
191 | |
{ |
192 | 0 | if (_inputEncoding == null) |
193 | |
{ |
194 | 0 | _inputEncoding = getDefaultEncoding(); |
195 | |
} |
196 | |
|
197 | 0 | if (is != null) |
198 | |
{ |
199 | |
try |
200 | |
{ |
201 | 0 | is.close(); |
202 | |
} |
203 | 0 | catch (Exception ignore) |
204 | |
{ |
205 | 0 | } |
206 | |
} |
207 | |
} |
208 | 0 | } |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
protected Reader getReader () throws IOException |
216 | |
{ |
217 | 0 | _log.debug("Using encoding " + _inputEncoding); |
218 | |
|
219 | 0 | if (_inputEncoding.equals("native_ascii")) |
220 | |
{ |
221 | 0 | return new NativeAsciiReader( |
222 | |
new InputStreamReader(_url.openStream(), "ASCII")); |
223 | |
} |
224 | |
else |
225 | |
{ |
226 | 0 | return new BufferedReader( |
227 | |
new InputStreamReader(_url.openStream(), _inputEncoding)); |
228 | |
} |
229 | |
} |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
public URL getURL () |
235 | |
{ |
236 | 0 | return _url; |
237 | |
} |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
public String toString () |
245 | |
{ |
246 | 0 | return "URLTemplate:" + _url; |
247 | |
} |
248 | |
|
249 | |
public void parse () throws IOException, TemplateException |
250 | |
{ |
251 | 0 | super.parse(); |
252 | 0 | if ((_outputEncoding != null) |
253 | |
&& (getParam(WMConstants.TEMPLATE_OUTPUT_ENCODING) == null)) |
254 | |
{ |
255 | 0 | _log.debug("Setting output encoding to " + _outputEncoding); |
256 | 0 | setParam(WMConstants.TEMPLATE_OUTPUT_ENCODING, _outputEncoding); |
257 | |
} |
258 | 0 | if ((_outputLocale != null) |
259 | |
&& (getParam(WMConstants.TEMPLATE_LOCALE) == null)) |
260 | |
{ |
261 | 0 | _log.debug("Setting output locale to " + _outputLocale); |
262 | 0 | setParam(WMConstants.TEMPLATE_LOCALE, _outputLocale); |
263 | |
} |
264 | 0 | } |
265 | |
} |