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