1 package net.sourceforge.pmd;
2
3 import net.sourceforge.pmd.swingui.Resources;
4
5 import java.io.BufferedReader;
6 import java.io.File;
7 import java.io.FileNotFoundException;
8 import java.io.FileReader;
9 import java.io.FileWriter;
10 import java.io.IOException;
11 import java.io.PrintWriter;
12 import java.util.StringTokenizer;
13
14 /***
15 * Reads and writes a list of included rule sets. Used by the PMD Viewer to select the
16 * rule sets to be used during analysis. The PMD Viewer provides the editing capability
17 * to include or exclude rule sets.
18 *
19 * @author Donald A. Leckie
20 * @since September 11, 2002
21 * @version $Revision: 1.4 $, $Date: 2003/03/14 19:03:44 $
22 */
23 public class RuleSetList {
24
25 private static final String RULE_SET_LIST_FILE_NAME = "Included_Rule_Set_Names.txt";
26
27 /***
28 ********************************************************************************
29 *
30 * @param directoryPath
31 *
32 * @return
33 */
34 public static final String[] getIncludedRuleSetNames(String directoryPath) throws PMDException {
35 String[] ruleSetNames = new String[0];
36
37 if (directoryPath != null) {
38 File file;
39
40 directoryPath = directoryPath.trim();
41 file = new File(directoryPath + File.separator + RULE_SET_LIST_FILE_NAME);
42
43 if (file.exists()) {
44 BufferedReader inputStream = null;
45
46 try {
47 String ruleSetNameList;
48 StringTokenizer parser;
49 int index;
50
51 inputStream = new BufferedReader(new FileReader(file));
52 ruleSetNameList = inputStream.readLine();
53 parser = new StringTokenizer(ruleSetNameList, ",");
54 ruleSetNames = new String[parser.countTokens()];
55 index = 0;
56
57 while (parser.hasMoreTokens()) {
58 ruleSetNames[index] = parser.nextToken().trim();
59 index++;
60 }
61
62 } catch (FileNotFoundException exception) {
63 // Should not reach here because the file was already tested for existence.
64 String message;
65 PMDException pmdException;
66
67 message = Resources.getString("RESOURCE_RuleSetListFileNotFound");
68 pmdException = new PMDException(message, exception);
69 pmdException.fillInStackTrace();
70 throw pmdException;
71 } catch (IOException exception) {
72 String message;
73 PMDException pmdException;
74
75 message = Resources.getString("RESOURCE_RuleSetListFileIOError");
76 pmdException = new PMDException(message, exception);
77 pmdException.fillInStackTrace();
78 throw pmdException;
79 } finally {
80 if (inputStream != null) {
81 try {
82 inputStream.close();
83 } catch (IOException exception) {
84 // Ignore because the file is closed anyway.
85 inputStream = null;
86 }
87 }
88 }
89 }
90 }
91
92 return ruleSetNames;
93 }
94
95 /***
96 ********************************************************************************
97 *
98 * @param directoryPath
99 *
100 * @return
101 */
102 public static final void saveIncludedRuleSetNames(String directoryPath, String[] ruleSetNames) throws PMDException {
103 if ((directoryPath != null) && (ruleSetNames != null)) {
104 File file;
105
106 directoryPath = directoryPath.trim();
107 file = new File(directoryPath + File.separator + RULE_SET_LIST_FILE_NAME);
108
109 if (file.exists()) {
110 file.delete();
111 } else {
112 File directory = new File(directoryPath);
113
114 directory.mkdirs();
115 }
116
117 PrintWriter outputStream = null;
118
119 try {
120 StringBuffer buffer;
121
122 outputStream = new PrintWriter(new FileWriter(file));
123 buffer = new StringBuffer(100);
124
125 for (int n = 0; n < ruleSetNames.length; n++) {
126 buffer.append(ruleSetNames[n]);
127 buffer.append(',');
128 }
129
130 if (buffer.length() > 0) {
131 buffer.setLength(buffer.length() - 1);
132 }
133
134 outputStream.println(buffer.toString());
135 } catch (FileNotFoundException exception) {
136 String message = Resources.getString("RESOURCE_RuleSetListFileNotFound");
137 PMDException pmdException = new PMDException(message, exception);
138 pmdException.fillInStackTrace();
139 throw pmdException;
140 } catch (IOException exception) {
141 String message;
142 PMDException pmdException;
143
144 message = Resources.getString("RESOURCE_RuleSetListFileIOError");
145 pmdException = new PMDException(message, exception);
146 pmdException.fillInStackTrace();
147 throw pmdException;
148 } finally {
149 if (outputStream != null) {
150 outputStream.close();
151 }
152 }
153 }
154 }
155 }
This page was automatically generated by Maven