001: package org.acm.seguin.pmd;
002:
003: import org.acm.seguin.pmd.swingui.Resources;
004:
005: import java.io.BufferedReader;
006: import java.io.File;
007: import java.io.FileNotFoundException;
008: import java.io.FileReader;
009: import java.io.FileWriter;
010: import java.io.IOException;
011: import java.io.PrintWriter;
012: import java.util.StringTokenizer;
013:
014: /**
015: * Reads and writes a list of included rule sets. Used by the PMD Viewer to select the
016: * rule sets to be used during analysis. The PMD Viewer provides the editing capability
017: * to include or exclude rule sets.
018: *
019: * @author Donald A. Leckie
020: * @since September 11, 2002
021: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:58 $
022: */
023: public class RuleSetList {
024:
025: private static final String RULE_SET_LIST_FILE_NAME = "Included_Rule_Set_Names.txt";
026:
027: /**
028: ********************************************************************************
029: *
030: * @param directoryPath
031: *
032: * @return
033: */
034: public static final String[] getIncludedRuleSetNames(
035: String directoryPath) throws PMDException {
036: String[] ruleSetNames = new String[0];
037:
038: if (directoryPath != null) {
039: File file;
040:
041: directoryPath = directoryPath.trim();
042: file = new File(directoryPath + File.separator
043: + RULE_SET_LIST_FILE_NAME);
044:
045: if (file.exists()) {
046: BufferedReader inputStream = null;
047:
048: try {
049: String ruleSetNameList;
050: StringTokenizer parser;
051: int index;
052:
053: inputStream = new BufferedReader(new FileReader(
054: file));
055: ruleSetNameList = inputStream.readLine();
056: parser = new StringTokenizer(ruleSetNameList, ",");
057: ruleSetNames = new String[parser.countTokens()];
058: index = 0;
059:
060: while (parser.hasMoreTokens()) {
061: ruleSetNames[index] = parser.nextToken().trim();
062: index++;
063: }
064:
065: } catch (FileNotFoundException exception) {
066: // Should not reach here because the file was already tested for existence.
067: String message;
068: PMDException pmdException;
069:
070: message = Resources
071: .getString("RESOURCE_RuleSetListFileNotFound");
072: pmdException = new PMDException(message, exception);
073: pmdException.fillInStackTrace();
074: throw pmdException;
075: } catch (IOException exception) {
076: String message;
077: PMDException pmdException;
078:
079: message = Resources
080: .getString("RESOURCE_RuleSetListFileIOError");
081: pmdException = new PMDException(message, exception);
082: pmdException.fillInStackTrace();
083: throw pmdException;
084: } finally {
085: if (inputStream != null) {
086: try {
087: inputStream.close();
088: } catch (IOException exception) {
089: // Ignore because the file is closed anyway.
090: inputStream = null;
091: }
092: }
093: }
094: }
095: }
096:
097: return ruleSetNames;
098: }
099:
100: /**
101: ********************************************************************************
102: *
103: * @param directoryPath
104: *
105: * @return
106: */
107: public static final void saveIncludedRuleSetNames(
108: String directoryPath, String[] ruleSetNames)
109: throws PMDException {
110: if ((directoryPath != null) && (ruleSetNames != null)) {
111: File file;
112:
113: directoryPath = directoryPath.trim();
114: file = new File(directoryPath + File.separator
115: + RULE_SET_LIST_FILE_NAME);
116:
117: if (file.exists()) {
118: file.delete();
119: } else {
120: File directory = new File(directoryPath);
121:
122: directory.mkdirs();
123: }
124:
125: PrintWriter outputStream = null;
126:
127: try {
128: StringBuffer buffer;
129:
130: outputStream = new PrintWriter(new FileWriter(file));
131: buffer = new StringBuffer(100);
132:
133: for (int n = 0; n < ruleSetNames.length; n++) {
134: buffer.append(ruleSetNames[n]);
135: buffer.append(',');
136: }
137:
138: if (buffer.length() > 0) {
139: buffer.setLength(buffer.length() - 1);
140: }
141:
142: outputStream.println(buffer.toString());
143: } catch (FileNotFoundException exception) {
144: String message = Resources
145: .getString("RESOURCE_RuleSetListFileNotFound");
146: PMDException pmdException = new PMDException(message,
147: exception);
148: pmdException.fillInStackTrace();
149: throw pmdException;
150: } catch (IOException exception) {
151: String message;
152: PMDException pmdException;
153:
154: message = Resources
155: .getString("RESOURCE_RuleSetListFileIOError");
156: pmdException = new PMDException(message, exception);
157: pmdException.fillInStackTrace();
158: throw pmdException;
159: } finally {
160: if (outputStream != null) {
161: outputStream.close();
162: }
163: }
164: }
165: }
166: }
|