View Javadoc

1   /*
2    *  Java HTML Tidy - JTidy
3    *  HTML parser and pretty printer
4    *
5    *  Copyright (c) 1998-2000 World Wide Web Consortium (Massachusetts
6    *  Institute of Technology, Institut National de Recherche en
7    *  Informatique et en Automatique, Keio University). All Rights
8    *  Reserved.
9    *
10   *  Contributing Author(s):
11   *
12   *     Dave Raggett <dsr@w3.org>
13   *     Andy Quick <ac.quick@sympatico.ca> (translation to Java)
14   *     Gary L Peskin <garyp@firstech.com> (Java development)
15   *     Sami Lempinen <sami@lempinen.net> (release management)
16   *     Fabrizio Giustina <fgiust at users.sourceforge.net>
17   *     Vlad Skarzhevskyy <vlads at users.sourceforge.net> (JTidy servlet  development)
18   *
19   *  The contributing author(s) would like to thank all those who
20   *  helped with testing, bug fixes, and patience.  This wouldn't
21   *  have been possible without all of you.
22   *
23   *  COPYRIGHT NOTICE:
24   *
25   *  This software and documentation is provided "as is," and
26   *  the copyright holders and contributing author(s) make no
27   *  representations or warranties, express or implied, including
28   *  but not limited to, warranties of merchantability or fitness
29   *  for any particular purpose or that the use of the software or
30   *  documentation will not infringe any third party patents,
31   *  copyrights, trademarks or other rights.
32   *
33   *  The copyright holders and contributing author(s) will not be
34   *  liable for any direct, indirect, special or consequential damages
35   *  arising out of any use of the software or documentation, even if
36   *  advised of the possibility of such damage.
37   *
38   *  Permission is hereby granted to use, copy, modify, and distribute
39   *  this source code, or portions hereof, documentation and executables,
40   *  for any purpose, without fee, subject to the following restrictions:
41   *
42   *  1. The origin of this source code must not be misrepresented.
43   *  2. Altered versions must be plainly marked as such and must
44   *     not be misrepresented as being the original source.
45   *  3. This Copyright notice may not be removed or altered from any
46   *     source or altered source distribution.
47   *
48   *  The copyright holders and contributing author(s) specifically
49   *  permit, without fee, and encourage the use of this source code
50   *  as a component for supporting the Hypertext Markup Language in
51   *  commercial products. If you use this source code in a product,
52   *  acknowledgment is not required but would be appreciated.
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 }