View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2004-2010 Frederico Caldeira Knabben
4    * 
5    * == BEGIN LICENSE ==
6    * 
7    * Licensed under the terms of any of the following licenses at your
8    * choice:
9    * 
10   *  - GNU General Public License Version 2 or later (the "GPL")
11   *    http://www.gnu.org/licenses/gpl.html
12   * 
13   *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14   *    http://www.gnu.org/licenses/lgpl.html
15   * 
16   *  - Mozilla Public License Version 1.1 or later (the "MPL")
17   *    http://www.mozilla.org/MPL/MPL-1.1.html
18   * 
19   * == END LICENSE ==
20   */
21  package net.fckeditor.handlers;
22  
23  import java.io.BufferedInputStream;
24  import java.io.InputStream;
25  import java.util.Properties;
26  
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * Manages FCKeditor.Java properties files.
32   * <p>
33   * It manages/loads the properties files in the following order:
34   * <ol>
35   * <li>the default properties as defined <a
36   * href="http://java.fckeditor.net/properties.html">here</a>,
37   * <li>the user-defined properties (<code>fckeditor.properties</code>) if
38   * present.
39   * </ol>
40   * This means that user-defined properties <em>override</em> default ones. In
41   * the backend it utilizes the regular {@link Properties} class.
42   * </p>
43   * <p>
44   * Moreover, you can set properties programmatically too but make sure to
45   * override them <em>before</em> the first call of that specific property.
46   * 
47   * @version $Id: PropertiesLoader.java 4785 2009-12-21 20:10:28Z mosipov $
48   */
49  public class PropertiesLoader {
50  	private static final Logger logger = LoggerFactory
51  			.getLogger(PropertiesLoader.class);
52  	private static final String DEFAULT_FILENAME = "default.properties";
53  	private static final String LOCAL_PROPERTIES = "/fckeditor.properties";
54  	private static Properties properties = new Properties();
55  
56  	static {
57  
58  		// 1. load library defaults
59  		InputStream in = PropertiesLoader.class
60  				.getResourceAsStream(DEFAULT_FILENAME);
61  
62  		if (in == null) {
63  			logger.error("{} not found", DEFAULT_FILENAME);
64  			throw new RuntimeException(DEFAULT_FILENAME + " not found");
65  		} else {
66  			if (!(in instanceof BufferedInputStream))
67  				in = new BufferedInputStream(in);
68  
69  			try {
70  				properties.load(in);
71  				in.close();
72  				logger.debug("{} loaded", DEFAULT_FILENAME);
73  			} catch (Exception e) {
74  				logger.error("Error while processing {}", DEFAULT_FILENAME);
75  				throw new RuntimeException("Error while processing "
76  						+ DEFAULT_FILENAME, e);
77  			}
78  		}
79  
80  		// 2. load user defaults if present
81  		InputStream in2 = PropertiesLoader.class
82  				.getResourceAsStream(LOCAL_PROPERTIES);
83  
84  		if (in2 == null) {
85  			logger.info("{} not found", LOCAL_PROPERTIES);
86  		} else {
87  
88  			if (!(in2 instanceof BufferedInputStream))
89  				in2 = new BufferedInputStream(in2);
90  
91  			try {
92  				properties.load(in2);
93  				in2.close();
94  				logger.debug("{} loaded", LOCAL_PROPERTIES);
95  			} catch (Exception e) {
96  				logger.error("Error while processing {}", LOCAL_PROPERTIES);
97  				throw new RuntimeException("Error while processing "
98  						+ LOCAL_PROPERTIES, e);
99  			}
100 
101 		}
102 	}
103 
104 	/**
105 	 * Searches for the property with the specified key in this property list.
106 	 * 
107 	 * @see Properties#getProperty(String)
108 	 */
109 	public static String getProperty(final String key) {
110 		return properties.getProperty(key);
111 	}
112 
113 	/**
114 	 * Sets the property with the specified key in this property list.
115 	 * 
116 	 * @see Properties#setProperty(String, String)
117 	 */
118 	public static void setProperty(final String key, final String value) {
119 		properties.setProperty(key, value);
120 	}
121 
122 	/**
123 	 * Returns <code>fckeditor.toolbarSet</code> property
124 	 */
125 	public static String getEditorToolbarSet() {
126 		return properties.getProperty("fckeditor.toolbarSet");
127 	}
128 
129 	/**
130 	 * Returns <code>fckeditor.width</code> property
131 	 */
132 	public static String getEditorWidth() {
133 		return properties.getProperty("fckeditor.width");
134 	}
135 
136 	/**
137 	 * Returns <code>fckeditor.height</code> property
138 	 */
139 	public static String getEditorHeight() {
140 		return properties.getProperty("fckeditor.height");
141 	}
142 
143 	/**
144 	 * Returns <code>fckeditor.basePath</code> property
145 	 */
146 	public static String getEditorBasePath() {
147 		return properties.getProperty("fckeditor.basePath");
148 	}
149 
150 	/**
151 	 * Returns <code>connector.resourceType.file.path</code> property
152 	 */
153 	public static String getFileResourceTypePath() {
154 		return properties.getProperty("connector.resourceType.file.path");
155 	}
156 
157 	/**
158 	 * Returns <code>connector.resourceType.flash.path</code> property
159 	 */
160 	public static String getFlashResourceTypePath() {
161 		return properties.getProperty("connector.resourceType.flash.path");
162 	}
163 
164 	/**
165 	 * Returns <code>connector.resourceType.image.path</code> property
166 	 */
167 	public static String getImageResourceTypePath() {
168 		return properties.getProperty("connector.resourceType.image.path");
169 	}
170 
171 	/**
172 	 * Returns <code>connector.resourceType.media.path</code> property
173 	 */
174 	public static String getMediaResourceTypePath() {
175 		return properties.getProperty("connector.resourceType.media.path");
176 	}
177 
178 	/**
179 	 * Returns <code>connector.resourceType.file.extensions.allowed</code>
180 	 * property
181 	 */
182 	public static String getFileResourceTypeAllowedExtensions() {
183 		return properties
184 				.getProperty("connector.resourceType.file.extensions.allowed");
185 	}
186 
187 	/**
188 	 * Returns <code>connector.resourceType.file.extensions.denied</code>
189 	 * property
190 	 */
191 	public static String getFileResourceTypeDeniedExtensions() {
192 		return properties
193 				.getProperty("connector.resourceType.file.extensions.denied");
194 	}
195 
196 	/**
197 	 * Returns <code>connector.resourceType.flash.extensions.allowed</code>
198 	 * property
199 	 */
200 	public static String getFlashResourceTypeAllowedExtensions() {
201 		return properties
202 				.getProperty("connector.resourceType.flash.extensions.allowed");
203 	}
204 
205 	/**
206 	 * Returns <code>connector.resourceType.flash.extensions.denied</code>
207 	 * property
208 	 */
209 	public static String getFlashResourceTypeDeniedExtensions() {
210 		return properties
211 				.getProperty("connector.resourceType.flash.extensions.denied");
212 	}
213 
214 	/**
215 	 * Returns <code>connector.resourceType.image.extensions.allowed</code>
216 	 * property
217 	 */
218 	public static String getImageResourceTypeAllowedExtensions() {
219 		return properties
220 				.getProperty("connector.resourceType.image.extensions.allowed");
221 	}
222 
223 	/**
224 	 * Returns <code>connector.resourceType.image.extensions.denied</code>
225 	 * property
226 	 */
227 	public static String getImageResourceTypeDeniedExtensions() {
228 		return properties
229 				.getProperty("connector.resourceType.image.extensions.denied");
230 	}
231 
232 	/**
233 	 * Returns <code>connector.resourceType.media.extensions.allowed</code>
234 	 * property
235 	 */
236 	public static String getMediaResourceTypeAllowedExtensions() {
237 		return properties
238 				.getProperty("connector.resourceType.media.extensions.allowed");
239 	}
240 
241 	/**
242 	 * Returns <code>connector.resourceType.media.extensions.denied</code>
243 	 * property
244 	 */
245 	public static String getMediaResourceTypeDeniedExtensions() {
246 		return properties
247 				.getProperty("connector.resourceType.media.extensions.denied");
248 	}
249 
250 	/**
251 	 * Returns <code>connector.userFilesPath</code> property
252 	 */
253 	public static String getUserFilesPath() {
254 		return properties.getProperty("connector.userFilesPath");
255 	}
256 
257 	/**
258 	 * Returns <code>connector.userFilesAbsolutePath</code> property
259 	 */
260 	public static String getUserFilesAbsolutePath() {
261 		return properties.getProperty("connector.userFilesAbsolutePath");
262 	}
263 
264 	/**
265 	 * Returns <code>connector.forceSingleExtension</code> property
266 	 */
267 	public static boolean isForceSingleExtension() {
268 		return Boolean.valueOf(properties
269 				.getProperty("connector.forceSingleExtension"));
270 	}
271 
272 	/**
273 	 * Returns <code>connector.secureImageUploads</code> property
274 	 */
275 	public static boolean isSecureImageUploads() {
276 		return Boolean.valueOf(properties
277 				.getProperty("connector.secureImageUploads"));
278 	}
279 
280 	/**
281 	 * Returns <code>connector.impl</code> property
282 	 */
283 	public static String getConnectorImpl() {
284 		return properties.getProperty("connector.impl");
285 	}
286 
287 	/**
288 	 * Returns <code>connector.userActionImpl</code> property
289 	 */
290 	public static String getUserActionImpl() {
291 		return properties.getProperty("connector.userActionImpl");
292 	}
293 
294 	/**
295 	 * Returns <code>connector.userPathBuilderImpl</code> property
296 	 */
297 	public static String getUserPathBuilderImpl() {
298 		return properties.getProperty("connector.userPathBuilderImpl");
299 	}
300 
301 	/**
302 	 * Returns <code>connector.localeResolverImpl</code> property
303 	 */
304 	public static String getLocaleResolverImpl() {
305 		return properties.getProperty("localization.localeResolverImpl");
306 	}
307 }