1 package net.sourceforge.pmd.swingui;
2
3 import net.sourceforge.pmd.PMDException;
4 import net.sourceforge.pmd.Rule;
5
6 import java.io.File;
7 import java.io.FileInputStream;
8 import java.io.FileNotFoundException;
9 import java.io.FileOutputStream;
10 import java.io.IOException;
11 import java.text.MessageFormat;
12 import java.util.Properties;
13
14 /***
15 *
16 * @author Donald A. Leckie
17 * @since August 29, 2002
18 * @version $Revision: 1.14 $, $Date: 2003/03/14 19:04:07 $
19 */
20 class Preferences {
21
22 private Properties m_properties = new Properties();
23 private String m_defaultUserPathToPMD;
24 private String m_defaultSharedPathToPMD;
25 private String m_defaultCurrentPathToPMD;
26 private String m_defaultAnalysisResultsPath;
27 private String m_preferencesPath;
28 private static Preferences m_preferences;
29
30 // Constants
31 private final String USER_PATH_TO_PMD = "user_path_to_pmd";
32 private final String SHARED_PATH_TO_PMD = "shared_path_to_pmd";
33 private final String CURRENT_PATH_TO_PMD = "current_path_to_pmd";
34 private final String LOWEST_PRIORITY_FOR_ANALYSIS = "lowest_priority_for_analysis";
35 private final String ANALYSIS_RESULTS_PATH = "analysis_results_path";
36 private final String UNIVERSAL_SEPARATOR = "&US;";
37 private final String PREFERENCES_FILE_NAME = "user.preferences";
38 private final String PMD_DIRECTORY = "pmd";
39 private final String ANALYSIS_RESULTS_DIRECTORY = "Analysis_Results";
40 private final int LOWEST_RULE_PRIORITY = Rule.LOWEST_PRIORITY;
41
42
43 /***
44 *******************************************************************************
45 *
46 * @return
47 */
48 private Preferences() throws PMDException {
49 //
50 // Default user rule set directory.
51 //
52 m_defaultUserPathToPMD = System.getProperty("user.home");
53 setPath(USER_PATH_TO_PMD, m_defaultUserPathToPMD);
54
55 //
56 // Current rule set directory.
57 //
58 m_defaultCurrentPathToPMD = m_defaultUserPathToPMD;
59 setPath(CURRENT_PATH_TO_PMD, m_defaultCurrentPathToPMD);
60
61 //
62 // Default shared rule set directory.
63 //
64 m_defaultSharedPathToPMD = System.getProperty("user.dir");
65 setPath(SHARED_PATH_TO_PMD, m_defaultSharedPathToPMD);
66
67 //
68 // Default analysis results path.
69 //
70 m_defaultAnalysisResultsPath = m_defaultUserPathToPMD + File.separator + PMD_DIRECTORY + File.separator + ANALYSIS_RESULTS_DIRECTORY;
71 setPath(ANALYSIS_RESULTS_PATH, m_defaultAnalysisResultsPath);
72
73 //
74 // Preferences path.
75 //
76 getPreferencesPath();
77 }
78
79 /***
80 *******************************************************************************
81 *
82 * @return
83 */
84 protected static final Preferences getPreferences() throws PMDException {
85 if (m_preferences == null) {
86 m_preferences = new Preferences();
87 m_preferences.load();
88 }
89
90 return m_preferences;
91 }
92
93 /***
94 *******************************************************************************
95 *
96 * @return
97 *
98 * @throws PMDException
99 */
100 protected void getPreferencesPath() throws PMDException {
101 m_preferencesPath = System.getProperty("user.home") + File.separator + PMD_DIRECTORY + File.separator + PREFERENCES_FILE_NAME;
102
103 File file = new File(m_preferencesPath);
104
105 if (file.exists() == false) {
106 File directory = file.getParentFile();
107
108 try {
109 directory.mkdirs();
110 file.createNewFile();
111 } catch (IOException exception) {
112 String template = "Could not create file \"{0}\" in your home directory \"{1}\".";
113 Object[] args = {PREFERENCES_FILE_NAME, directory};
114 String message = MessageFormat.format(template, args);
115 PMDException pmdException = new PMDException(message, exception);
116 pmdException.fillInStackTrace();
117 throw pmdException;
118 }
119 }
120 }
121
122
123 /***
124 *******************************************************************************
125 *
126 * @return
127 */
128 protected boolean load() throws PMDException {
129 File file = new File(m_preferencesPath);
130 FileInputStream inputStream = null;
131
132 try {
133 inputStream = new FileInputStream(file);
134
135 m_properties.load(inputStream);
136
137 if (m_properties.containsKey(USER_PATH_TO_PMD) == false) {
138 m_properties.setProperty(USER_PATH_TO_PMD, m_defaultUserPathToPMD);
139 }
140
141 if (m_properties.containsKey(SHARED_PATH_TO_PMD) == false) {
142 m_properties.setProperty(SHARED_PATH_TO_PMD, m_defaultSharedPathToPMD);
143 }
144
145 if (m_properties.containsKey(CURRENT_PATH_TO_PMD) == false) {
146 m_properties.setProperty(CURRENT_PATH_TO_PMD, m_defaultCurrentPathToPMD);
147 }
148
149 if (m_properties.containsKey(ANALYSIS_RESULTS_PATH) == false) {
150 m_properties.setProperty(ANALYSIS_RESULTS_PATH, m_defaultAnalysisResultsPath);
151 }
152
153 return true;
154 } catch (FileNotFoundException exception) {
155 String template = "Could not find file \"{0}\" in directory \"{1}\".";
156 Object[] args = {PREFERENCES_FILE_NAME, m_preferencesPath};
157 String message = MessageFormat.format(template, args);
158 PMDException pmdException = new PMDException(message, exception);
159 pmdException.fillInStackTrace();
160 throw pmdException;
161 } catch (IOException exception) {
162 String template = "Could not load file \"{0}\" from directory \"{1}\".";
163 Object[] args = {PREFERENCES_FILE_NAME, m_preferencesPath};
164 String message = MessageFormat.format(template, args);
165 PMDException pmdException = new PMDException(message, exception);
166 pmdException.fillInStackTrace();
167 throw pmdException;
168 } finally {
169 if (inputStream != null) {
170 try {
171 inputStream.close();
172 } catch (IOException exception) {
173 exception.printStackTrace();
174 }
175 }
176 }
177 }
178
179 /***
180 *******************************************************************************
181 *
182 */
183 protected void save() throws PMDException {
184 FileOutputStream outputStream = null;
185
186 try {
187 File file = new File(m_preferencesPath);
188
189 if (file.exists() == false) {
190 file.createNewFile();
191 }
192
193 outputStream = new FileOutputStream(m_preferencesPath);
194
195 m_properties.store(outputStream, null);
196 } catch (FileNotFoundException exception) {
197 String template = "Could not find your \"{0}\" file in your home directory \"{1}\".";
198 Object[] args = {PREFERENCES_FILE_NAME, m_preferencesPath};
199 String message = MessageFormat.format(template, args);
200 PMDException pmdException = new PMDException(message, exception);
201 pmdException.fillInStackTrace();
202 throw pmdException;
203 } catch (IOException exception) {
204 String template = "Could not save your \"{0}\" file in your home directory \"{1}\".";
205 Object[] args = {PREFERENCES_FILE_NAME, m_preferencesPath};
206 String message = MessageFormat.format(template, args);
207 PMDException pmdException = new PMDException(message, exception);
208 pmdException.fillInStackTrace();
209 throw pmdException;
210 } finally {
211 if (outputStream != null) {
212 try {
213 outputStream.close();
214 } catch (IOException exception) {
215 exception.printStackTrace();
216 }
217 }
218 }
219 }
220
221 /***
222 *******************************************************************************
223 *
224 * @param path
225 */
226 protected void setCurrentPathToPMD(String path) {
227 setPath(CURRENT_PATH_TO_PMD, path);
228 }
229
230 /***
231 *******************************************************************************
232 *
233 * @param path
234 */
235 protected void setUserPathToPMD(String path) {
236 setPath(USER_PATH_TO_PMD, path);
237 }
238
239 /***
240 *******************************************************************************
241 *
242 * @param path
243 */
244 protected void setSharedPathToPMD(String path) {
245 setPath(SHARED_PATH_TO_PMD, path);
246 }
247
248 /***
249 *******************************************************************************
250 *
251 * @param name
252 * @param directory
253 */
254 private boolean setPath(String name, String directory) {
255 name = trim(name);
256 directory = trim(directory);
257
258 if ((name.length() == 0) || (directory.length() == 0)) {
259 return false;
260 }
261
262 String key;
263
264 key = name.toLowerCase();
265 directory = encodePath(directory);
266
267 m_properties.put(key, directory);
268
269 return true;
270 }
271
272 /***
273 *******************************************************************************
274 *
275 * @param directory
276 */
277 protected void setAnalysisResultPath(String directory) {
278 directory = encodePath(trim(directory));
279
280 m_properties.put(ANALYSIS_RESULTS_PATH, directory);
281 }
282
283 /***
284 *******************************************************************************
285 *
286 * @param priority
287 */
288 protected void setLowestPriorityForAnalysis(int priority) {
289 if (priority < 0) {
290 priority = 0;
291 } else if (priority > LOWEST_RULE_PRIORITY) {
292 priority = LOWEST_RULE_PRIORITY;
293 }
294
295 m_properties.put(LOWEST_PRIORITY_FOR_ANALYSIS, String.valueOf(priority));
296 }
297
298 /***
299 *******************************************************************************
300 *
301 * @return
302 */
303 protected int getLowestPriorityForAnalysis() {
304 int priority;
305
306 try {
307 priority = Integer.parseInt((String) m_properties.get(LOWEST_PRIORITY_FOR_ANALYSIS));
308 } catch (NumberFormatException exception) {
309 priority = LOWEST_RULE_PRIORITY;
310 }
311
312 return priority;
313 }
314
315 /***
316 *******************************************************************************
317 *
318 * @param name
319 * @param directory
320 *
321 * @return
322 */
323 private String encodePath(String directory) {
324 if (directory != null) {
325 StringBuffer buffer = new StringBuffer(directory.length() + 50);
326
327 buffer.append(directory);
328
329 for (int n = 0; n < buffer.length(); n++) {
330 if (buffer.charAt(n) == File.separatorChar) {
331 buffer.replace(n, n + 1, UNIVERSAL_SEPARATOR);
332 }
333 }
334
335 directory = buffer.toString();
336 }
337
338 return directory;
339 }
340
341 /***
342 *******************************************************************************
343 *
344 * @param value
345 *
346 * @return
347 */
348 private String decodePath(String value) {
349 if (value != null) {
350 StringBuffer buffer = new StringBuffer(value);
351 int universalSeparatorLength = UNIVERSAL_SEPARATOR.length();
352
353 for (int n = 0; n < buffer.length(); n++) {
354 if (buffer.charAt(n) == '&') {
355 if ((n + universalSeparatorLength) <= buffer.length()) {
356 if (buffer.charAt(n + 1) == 'U') {
357 if (buffer.charAt(n + 2) == 'S') {
358 if (buffer.charAt(n + 3) == ';') {
359 buffer.replace(n, n + universalSeparatorLength, File.separator);
360 }
361 }
362 }
363 }
364 }
365 }
366
367 value = buffer.toString();
368 }
369
370 return value;
371 }
372
373 /***
374 *******************************************************************************
375 *
376 * @return
377 */
378 protected String getAnalysisResultsPath() {
379 String path = decodePath(m_properties.getProperty(ANALYSIS_RESULTS_PATH));
380
381 if (path == null) {
382 path = m_defaultAnalysisResultsPath;
383 }
384
385 (new File(path)).mkdirs();
386
387 return path;
388 }
389
390 /***
391 *******************************************************************************
392 *
393 * @param pathName
394 *
395 * @return
396 */
397 private String getPathToPMD(String pathName) {
398 String key = trim(pathName).toLowerCase();
399 String directory = decodePath(m_properties.getProperty(key));
400
401 if (directory == null) {
402 directory = "";
403 }
404
405 return directory;
406 }
407
408 /***
409 *******************************************************************************
410 *
411 * @return
412 */
413 protected String getCurrentPathToPMD() {
414 return getPathToPMD(CURRENT_PATH_TO_PMD);
415 }
416
417 /***
418 *******************************************************************************
419 *
420 * @return
421 */
422 protected String getUserPathToPMD() {
423 return getPathToPMD(USER_PATH_TO_PMD);
424 }
425
426 /***
427 *******************************************************************************
428 *
429 * @return
430 */
431 protected String getSharedPathToPMD() {
432 return getPathToPMD(SHARED_PATH_TO_PMD);
433 }
434
435 /***
436 *************************************************************************
437 *
438 * @param text
439 *
440 * @return
441 */
442 private String trim(String text) {
443 if (text == null) {
444 text = "";
445 } else {
446 text = text.trim();
447
448 if (text.length() == 0) {
449 text = "";
450 }
451 }
452
453 return text;
454 }
455 }
This page was automatically generated by Maven