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 |
| package org.w3c.tidy.servlet.properties; |
56 |
| |
57 |
| import java.util.Properties; |
58 |
| import java.io.InputStream; |
59 |
| import java.io.IOException; |
60 |
| |
61 |
| import javax.servlet.http.HttpSession; |
62 |
| |
63 |
| import org.apache.commons.logging.Log; |
64 |
| import org.apache.commons.logging.LogFactory; |
65 |
| import org.w3c.tidy.servlet.RepositoryFactory; |
66 |
| import org.w3c.tidy.servlet.ResponseRecordRepository; |
67 |
| import org.w3c.tidy.servlet.data.DefaultRepositoryFactory; |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| public class JTidyServletProperties |
76 |
| { |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| public static final String DEFAULT_FILENAME = "JTidyServlet.properties"; |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| public static final String PROPERTY_CLASS_REPOSITORYFACTORY = "repositoryFactory.class"; |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| public static final String PROPERTY_STRING_IMAGENAMEPREFIX = "imageNamePrefix"; |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| public static final String PROPERTY_STRING_IMAGENAMEEXTENSION = "imageNameExtension"; |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| public static final String PROPERTY_STRING_IMAGE_WIDTH = "imageWidth"; |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| public static final String PROPERTY_STRING_IMAGE_HEIGHT = "imageHeight"; |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| public static final String PROPERTY_INT_IMAGEGETTIMEOUT = "imageGetTimeout"; |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| public static final String JTIDYSERVLET_URI = "JTidyServletURI"; |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| public static final String PROPERTY_BOOLEAN_XHTML = "xhtml"; |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| private static Log log = LogFactory.getLog(JTidyServletProperties.class); |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| private Properties properties; |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| private static JTidyServletProperties instance; |
137 |
| |
138 |
1
| private JTidyServletProperties()
|
139 |
| { |
140 |
1
| this.properties = new Properties();
|
141 |
1
| loadFile(DEFAULT_FILENAME);
|
142 |
| } |
143 |
| |
144 |
22
| public void loadFile(String fileName)
|
145 |
| { |
146 |
22
| if (fileName == null)
|
147 |
| { |
148 |
18
| return;
|
149 |
| } |
150 |
4
| Properties tmpProperties = new Properties();
|
151 |
4
| InputStream in = this.getClass().getClassLoader().getResourceAsStream(fileName);
|
152 |
4
| if (in != null)
|
153 |
| { |
154 |
4
| try
|
155 |
| { |
156 |
4
| tmpProperties.load(in);
|
157 |
4
| this.properties.putAll(tmpProperties);
|
158 |
4
| log.info("property file " + fileName + " loaded");
|
159 |
| } |
160 |
| catch (IOException e) |
161 |
| { |
162 |
0
| log.error("Error loading JTidy property file " + fileName, e);
|
163 |
| } |
164 |
| } |
165 |
| else |
166 |
| { |
167 |
0
| log.error("Properties file [" + fileName + "] not found in class path");
|
168 |
| } |
169 |
| } |
170 |
| |
171 |
128
| public static JTidyServletProperties getInstance()
|
172 |
| { |
173 |
128
| if (instance == null)
|
174 |
| { |
175 |
1
| instance = new JTidyServletProperties();
|
176 |
| } |
177 |
128
| return instance;
|
178 |
| } |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
179
| public String getProperty(String key)
|
186 |
| { |
187 |
179
| return this.properties.getProperty(key);
|
188 |
| } |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
87
| public String getProperty(String key, String defaultValue)
|
197 |
| { |
198 |
87
| String val = getProperty(key);
|
199 |
87
| if (val == null)
|
200 |
| { |
201 |
73
| val = defaultValue;
|
202 |
| } |
203 |
87
| return val;
|
204 |
| } |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
| |
211 |
| |
212 |
7
| public int getIntProperty(String key, int defaultValue)
|
213 |
| { |
214 |
7
| int intValue = defaultValue;
|
215 |
| |
216 |
7
| try
|
217 |
| { |
218 |
7
| String sValue = getProperty(key);
|
219 |
7
| if (sValue == null)
|
220 |
| { |
221 |
0
| if (log.isDebugEnabled())
|
222 |
| { |
223 |
0
| log.debug("Value " + key + " does not exists");
|
224 |
| } |
225 |
| } |
226 |
| else |
227 |
| { |
228 |
7
| intValue = Integer.parseInt(sValue);
|
229 |
| } |
230 |
| } |
231 |
| catch (NumberFormatException e) |
232 |
| { |
233 |
0
| log.warn("Invalid value for \""
|
234 |
| + key |
235 |
| + "\" property: value=\"" |
236 |
| + getProperty(key) |
237 |
| + "\"; using default \"" |
238 |
| + defaultValue |
239 |
| + "\""); |
240 |
| } |
241 |
| |
242 |
7
| return intValue;
|
243 |
| } |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
| |
249 |
| |
250 |
| |
251 |
27
| public boolean getBooleanProperty(String key, boolean defaultValue)
|
252 |
| { |
253 |
27
| boolean intValue = defaultValue;
|
254 |
| |
255 |
27
| String sValue = getProperty(key);
|
256 |
27
| if (sValue == null)
|
257 |
| { |
258 |
0
| if (log.isDebugEnabled())
|
259 |
| { |
260 |
0
| log.debug("Value " + key + " does not exists");
|
261 |
| } |
262 |
| } |
263 |
| else |
264 |
| { |
265 |
27
| intValue = "true".equalsIgnoreCase(sValue);
|
266 |
| } |
267 |
| |
268 |
27
| return intValue;
|
269 |
| } |
270 |
| |
271 |
| |
272 |
| |
273 |
| |
274 |
| |
275 |
| |
276 |
58
| public RepositoryFactory getRepositoryFactoryInstance()
|
277 |
| { |
278 |
58
| String className = getProperty(PROPERTY_CLASS_REPOSITORYFACTORY);
|
279 |
| |
280 |
58
| if (className != null)
|
281 |
| { |
282 |
23
| try
|
283 |
| { |
284 |
23
| Class classProperty = Class.forName(className);
|
285 |
23
| return (RepositoryFactory) classProperty.newInstance();
|
286 |
| } |
287 |
| catch (Throwable e) |
288 |
| { |
289 |
0
| log.error("Error creating RepositoryFactory " + className, e);
|
290 |
| } |
291 |
| } |
292 |
35
| return new DefaultRepositoryFactory();
|
293 |
| } |
294 |
| |
295 |
| |
296 |
| |
297 |
| |
298 |
| |
299 |
| |
300 |
16
| public ResponseRecordRepository getRepositoryInstance(HttpSession httpSession)
|
301 |
| { |
302 |
16
| return getRepositoryFactoryInstance().getRepositoryInstance(httpSession);
|
303 |
| } |
304 |
| |
305 |
| } |