001: package org.acm.seguin.pmd.swingui;
002:
003: import org.acm.seguin.pmd.PMDException;
004: import org.acm.seguin.pmd.Rule;
005:
006: import java.io.File;
007: import java.io.FileInputStream;
008: import java.io.FileNotFoundException;
009: import java.io.FileOutputStream;
010: import java.io.IOException;
011: import java.text.MessageFormat;
012: import java.util.Properties;
013:
014: /**
015: *
016: * @author Donald A. Leckie
017: * @since August 29, 2002
018: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
019: */
020: class Preferences {
021:
022: private Properties m_properties = new Properties();
023: private String m_defaultUserPathToPMD;
024: private String m_defaultSharedPathToPMD;
025: private String m_defaultCurrentPathToPMD;
026: private String m_defaultAnalysisResultsPath;
027: private String m_preferencesPath;
028: private static Preferences m_preferences;
029:
030: // Constants
031: private final String USER_PATH_TO_PMD = "user_path_to_pmd";
032: private final String SHARED_PATH_TO_PMD = "shared_path_to_pmd";
033: private final String CURRENT_PATH_TO_PMD = "current_path_to_pmd";
034: private final String LOWEST_PRIORITY_FOR_ANALYSIS = "lowest_priority_for_analysis";
035: private final String ANALYSIS_RESULTS_PATH = "analysis_results_path";
036: private final String UNIVERSAL_SEPARATOR = "&US;";
037: private final String PREFERENCES_FILE_NAME = "user.preferences";
038: private final String PMD_DIRECTORY = "pmd";
039: private final String ANALYSIS_RESULTS_DIRECTORY = "Analysis_Results";
040: private final int LOWEST_RULE_PRIORITY = Rule.LOWEST_PRIORITY;
041:
042: /**
043: *******************************************************************************
044: *
045: * @return
046: */
047: private Preferences() throws PMDException {
048: //
049: // Default user rule set directory.
050: //
051: m_defaultUserPathToPMD = System.getProperty("user.home");
052: setPath(USER_PATH_TO_PMD, m_defaultUserPathToPMD);
053:
054: //
055: // Current rule set directory.
056: //
057: m_defaultCurrentPathToPMD = m_defaultUserPathToPMD;
058: setPath(CURRENT_PATH_TO_PMD, m_defaultCurrentPathToPMD);
059:
060: //
061: // Default shared rule set directory.
062: //
063: m_defaultSharedPathToPMD = System.getProperty("user.dir");
064: setPath(SHARED_PATH_TO_PMD, m_defaultSharedPathToPMD);
065:
066: //
067: // Default analysis results path.
068: //
069: m_defaultAnalysisResultsPath = m_defaultUserPathToPMD
070: + File.separator + PMD_DIRECTORY + File.separator
071: + ANALYSIS_RESULTS_DIRECTORY;
072: setPath(ANALYSIS_RESULTS_PATH, m_defaultAnalysisResultsPath);
073:
074: //
075: // Preferences path.
076: //
077: getPreferencesPath();
078: }
079:
080: /**
081: *******************************************************************************
082: *
083: * @return
084: */
085: protected static final Preferences getPreferences()
086: throws PMDException {
087: if (m_preferences == null) {
088: m_preferences = new Preferences();
089: m_preferences.load();
090: }
091:
092: return m_preferences;
093: }
094:
095: /**
096: *******************************************************************************
097: *
098: * @return
099: *
100: * @throws PMDException
101: */
102: protected void getPreferencesPath() throws PMDException {
103: m_preferencesPath = System.getProperty("user.home")
104: + File.separator + PMD_DIRECTORY + File.separator
105: + PREFERENCES_FILE_NAME;
106:
107: File file = new File(m_preferencesPath);
108:
109: if (file.exists() == false) {
110: File directory = file.getParentFile();
111:
112: try {
113: directory.mkdirs();
114: file.createNewFile();
115: } catch (IOException exception) {
116: String template = "Could not create file \"{0}\" in your home directory \"{1}\".";
117: Object[] args = { PREFERENCES_FILE_NAME, directory };
118: String message = MessageFormat.format(template, args);
119: PMDException pmdException = new PMDException(message,
120: exception);
121: pmdException.fillInStackTrace();
122: throw pmdException;
123: }
124: }
125: }
126:
127: /**
128: *******************************************************************************
129: *
130: * @return
131: */
132: protected boolean load() throws PMDException {
133: File file = new File(m_preferencesPath);
134: FileInputStream inputStream = null;
135:
136: try {
137: inputStream = new FileInputStream(file);
138:
139: m_properties.load(inputStream);
140:
141: if (m_properties.containsKey(USER_PATH_TO_PMD) == false) {
142: m_properties.setProperty(USER_PATH_TO_PMD,
143: m_defaultUserPathToPMD);
144: }
145:
146: if (m_properties.containsKey(SHARED_PATH_TO_PMD) == false) {
147: m_properties.setProperty(SHARED_PATH_TO_PMD,
148: m_defaultSharedPathToPMD);
149: }
150:
151: if (m_properties.containsKey(CURRENT_PATH_TO_PMD) == false) {
152: m_properties.setProperty(CURRENT_PATH_TO_PMD,
153: m_defaultCurrentPathToPMD);
154: }
155:
156: if (m_properties.containsKey(ANALYSIS_RESULTS_PATH) == false) {
157: m_properties.setProperty(ANALYSIS_RESULTS_PATH,
158: m_defaultAnalysisResultsPath);
159: }
160:
161: return true;
162: } catch (FileNotFoundException exception) {
163: String template = "Could not find file \"{0}\" in directory \"{1}\".";
164: Object[] args = { PREFERENCES_FILE_NAME, m_preferencesPath };
165: String message = MessageFormat.format(template, args);
166: PMDException pmdException = new PMDException(message,
167: exception);
168: pmdException.fillInStackTrace();
169: throw pmdException;
170: } catch (IOException exception) {
171: String template = "Could not load file \"{0}\" from directory \"{1}\".";
172: Object[] args = { PREFERENCES_FILE_NAME, m_preferencesPath };
173: String message = MessageFormat.format(template, args);
174: PMDException pmdException = new PMDException(message,
175: exception);
176: pmdException.fillInStackTrace();
177: throw pmdException;
178: } finally {
179: if (inputStream != null) {
180: try {
181: inputStream.close();
182: } catch (IOException exception) {
183: exception.printStackTrace();
184: }
185: }
186: }
187: }
188:
189: /**
190: *******************************************************************************
191: *
192: */
193: protected void save() throws PMDException {
194: FileOutputStream outputStream = null;
195:
196: try {
197: File file = new File(m_preferencesPath);
198:
199: if (file.exists() == false) {
200: file.createNewFile();
201: }
202:
203: outputStream = new FileOutputStream(m_preferencesPath);
204:
205: m_properties.store(outputStream, null);
206: } catch (FileNotFoundException exception) {
207: String template = "Could not find your \"{0}\" file in your home directory \"{1}\".";
208: Object[] args = { PREFERENCES_FILE_NAME, m_preferencesPath };
209: String message = MessageFormat.format(template, args);
210: PMDException pmdException = new PMDException(message,
211: exception);
212: pmdException.fillInStackTrace();
213: throw pmdException;
214: } catch (IOException exception) {
215: String template = "Could not save your \"{0}\" file in your home directory \"{1}\".";
216: Object[] args = { PREFERENCES_FILE_NAME, m_preferencesPath };
217: String message = MessageFormat.format(template, args);
218: PMDException pmdException = new PMDException(message,
219: exception);
220: pmdException.fillInStackTrace();
221: throw pmdException;
222: } finally {
223: if (outputStream != null) {
224: try {
225: outputStream.close();
226: } catch (IOException exception) {
227: exception.printStackTrace();
228: }
229: }
230: }
231: }
232:
233: /**
234: *******************************************************************************
235: *
236: * @param path
237: */
238: protected void setCurrentPathToPMD(String path) {
239: setPath(CURRENT_PATH_TO_PMD, path);
240: }
241:
242: /**
243: *******************************************************************************
244: *
245: * @param path
246: */
247: protected void setUserPathToPMD(String path) {
248: setPath(USER_PATH_TO_PMD, path);
249: }
250:
251: /**
252: *******************************************************************************
253: *
254: * @param path
255: */
256: protected void setSharedPathToPMD(String path) {
257: setPath(SHARED_PATH_TO_PMD, path);
258: }
259:
260: /**
261: *******************************************************************************
262: *
263: * @param name
264: * @param directory
265: */
266: private boolean setPath(String name, String directory) {
267: name = trim(name);
268: directory = trim(directory);
269:
270: if ((name.length() == 0) || (directory.length() == 0)) {
271: return false;
272: }
273:
274: String key;
275:
276: key = name.toLowerCase();
277: directory = encodePath(directory);
278:
279: m_properties.put(key, directory);
280:
281: return true;
282: }
283:
284: /**
285: *******************************************************************************
286: *
287: * @param directory
288: */
289: protected void setAnalysisResultPath(String directory) {
290: directory = encodePath(trim(directory));
291:
292: m_properties.put(ANALYSIS_RESULTS_PATH, directory);
293: }
294:
295: /**
296: *******************************************************************************
297: *
298: * @param priority
299: */
300: protected void setLowestPriorityForAnalysis(int priority) {
301: if (priority < 0) {
302: priority = 0;
303: } else if (priority > LOWEST_RULE_PRIORITY) {
304: priority = LOWEST_RULE_PRIORITY;
305: }
306:
307: m_properties.put(LOWEST_PRIORITY_FOR_ANALYSIS, String
308: .valueOf(priority));
309: }
310:
311: /**
312: *******************************************************************************
313: *
314: * @return
315: */
316: protected int getLowestPriorityForAnalysis() {
317: int priority;
318:
319: try {
320: priority = Integer.parseInt((String) m_properties
321: .get(LOWEST_PRIORITY_FOR_ANALYSIS));
322: } catch (NumberFormatException exception) {
323: priority = LOWEST_RULE_PRIORITY;
324: }
325:
326: return priority;
327: }
328:
329: /**
330: *******************************************************************************
331: *
332: * @param name
333: * @param directory
334: *
335: * @return
336: */
337: private String encodePath(String directory) {
338: if (directory != null) {
339: StringBuffer buffer = new StringBuffer(
340: directory.length() + 50);
341:
342: buffer.append(directory);
343:
344: for (int n = 0; n < buffer.length(); n++) {
345: if (buffer.charAt(n) == File.separatorChar) {
346: buffer.replace(n, n + 1, UNIVERSAL_SEPARATOR);
347: }
348: }
349:
350: directory = buffer.toString();
351: }
352:
353: return directory;
354: }
355:
356: /**
357: *******************************************************************************
358: *
359: * @param value
360: *
361: * @return
362: */
363: private String decodePath(String value) {
364: if (value != null) {
365: StringBuffer buffer = new StringBuffer(value);
366: int universalSeparatorLength = UNIVERSAL_SEPARATOR.length();
367:
368: for (int n = 0; n < buffer.length(); n++) {
369: if (buffer.charAt(n) == '&') {
370: if ((n + universalSeparatorLength) <= buffer
371: .length()) {
372: if (buffer.charAt(n + 1) == 'U') {
373: if (buffer.charAt(n + 2) == 'S') {
374: if (buffer.charAt(n + 3) == ';') {
375: buffer.replace(n, n
376: + universalSeparatorLength,
377: File.separator);
378: }
379: }
380: }
381: }
382: }
383: }
384:
385: value = buffer.toString();
386: }
387:
388: return value;
389: }
390:
391: /**
392: *******************************************************************************
393: *
394: * @return
395: */
396: protected String getAnalysisResultsPath() {
397: String path = decodePath(m_properties
398: .getProperty(ANALYSIS_RESULTS_PATH));
399:
400: if (path == null) {
401: path = m_defaultAnalysisResultsPath;
402: }
403:
404: (new File(path)).mkdirs();
405:
406: return path;
407: }
408:
409: /**
410: *******************************************************************************
411: *
412: * @param pathName
413: *
414: * @return
415: */
416: private String getPathToPMD(String pathName) {
417: String key = trim(pathName).toLowerCase();
418: String directory = decodePath(m_properties.getProperty(key));
419:
420: if (directory == null) {
421: directory = "";
422: }
423:
424: return directory;
425: }
426:
427: /**
428: *******************************************************************************
429: *
430: * @return
431: */
432: protected String getCurrentPathToPMD() {
433: return getPathToPMD(CURRENT_PATH_TO_PMD);
434: }
435:
436: /**
437: *******************************************************************************
438: *
439: * @return
440: */
441: protected String getUserPathToPMD() {
442: return getPathToPMD(USER_PATH_TO_PMD);
443: }
444:
445: /**
446: *******************************************************************************
447: *
448: * @return
449: */
450: protected String getSharedPathToPMD() {
451: return getPathToPMD(SHARED_PATH_TO_PMD);
452: }
453:
454: /**
455: *************************************************************************
456: *
457: * @param text
458: *
459: * @return
460: */
461: private String trim(String text) {
462: if (text == null) {
463: text = "";
464: } else {
465: text = text.trim();
466:
467: if (text.length() == 0) {
468: text = "";
469: }
470: }
471:
472: return text;
473: }
474: }
|