1
2
3
4 package net.sourceforge.pmd;
5
6 import java.io.InputStream;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import net.sourceforge.pmd.util.ResourceLoader;
11 import net.sourceforge.pmd.util.StringUtil;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public class RuleSetReferenceId {
76 private final boolean external;
77 private final String ruleSetFileName;
78 private final boolean allRules;
79 private final String ruleName;
80 private final RuleSetReferenceId externalRuleSetReferenceId;
81
82
83
84
85
86
87 public RuleSetReferenceId(final String id) {
88 this(id, null);
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103 public RuleSetReferenceId(final String id, final RuleSetReferenceId externalRuleSetReferenceId) {
104 if (externalRuleSetReferenceId != null && !externalRuleSetReferenceId.isExternal()) {
105 throw new IllegalArgumentException("Cannot pair with non-external <" + externalRuleSetReferenceId + ">.");
106 }
107 if (id != null && id.indexOf(',') >= 0) {
108 throw new IllegalArgumentException("A single RuleSetReferenceId cannot contain ',' (comma) characters: "
109 + id);
110 }
111
112
113 if (StringUtil.isEmpty(id) || isFullRuleSetName(id)) {
114
115 external = true;
116 ruleSetFileName = id;
117 allRules = true;
118 ruleName = null;
119 } else {
120
121 final int separatorIndex = Math.max(id.lastIndexOf('/'), id.lastIndexOf('\\'));
122 if (separatorIndex >= 0 && separatorIndex != id.length() - 1) {
123 final String name = id.substring(0, separatorIndex);
124 external = true;
125 if (isFullRuleSetName(name)) {
126
127 ruleSetFileName = name;
128 } else {
129
130 int index = name.indexOf('-');
131 if (index >= 0) {
132
133 ruleSetFileName = "rulesets/" + name.substring(0, index) + "/" + name.substring(index + 1)
134 + ".xml";
135 } else {
136
137 if (name.matches("[0-9]+.*")) {
138 ruleSetFileName = "rulesets/releases/" + name + ".xml";
139 } else {
140
141 ruleSetFileName = name;
142 }
143 }
144 }
145
146
147 allRules = false;
148 ruleName = id.substring(separatorIndex + 1);
149 } else {
150
151 int index = id.indexOf('-');
152 if (index >= 0) {
153
154 external = true;
155 ruleSetFileName = "rulesets/" + id.substring(0, index) + "/" + id.substring(index + 1) + ".xml";
156 allRules = true;
157 ruleName = null;
158 } else {
159
160 if (id.matches("[0-9]+.*")) {
161 external = true;
162 ruleSetFileName = "rulesets/releases/" + id + ".xml";
163 allRules = true;
164 ruleName = null;
165 } else {
166
167 external = externalRuleSetReferenceId != null ? true : false;
168 ruleSetFileName = externalRuleSetReferenceId != null ? externalRuleSetReferenceId
169 .getRuleSetFileName() : null;
170 allRules = false;
171 ruleName = id;
172 }
173 }
174 }
175 }
176
177 if (this.external && this.ruleName != null && !this.ruleName.equals(id) && externalRuleSetReferenceId != null) {
178 throw new IllegalArgumentException("Cannot pair external <" + this + "> with external <"
179 + externalRuleSetReferenceId + ">.");
180 }
181 this.externalRuleSetReferenceId = externalRuleSetReferenceId;
182 }
183
184 private static boolean isFullRuleSetName(String name) {
185 return name.endsWith(".xml");
186 }
187
188
189
190
191
192
193
194 public static List<RuleSetReferenceId> parse(String referenceString) {
195 List<RuleSetReferenceId> references = new ArrayList<RuleSetReferenceId>();
196 if (referenceString.indexOf(',') == -1) {
197 references.add(new RuleSetReferenceId(referenceString));
198 } else {
199 for (String name : referenceString.split(",")) {
200 references.add(new RuleSetReferenceId(name));
201 }
202 }
203 return references;
204 }
205
206
207
208
209
210 public boolean isExternal() {
211 return external;
212 }
213
214
215
216
217
218 public boolean isAllRules() {
219 return allRules;
220 }
221
222
223
224
225
226 public String getRuleSetFileName() {
227 return ruleSetFileName;
228 }
229
230
231
232
233
234
235 public String getRuleName() {
236 return ruleName;
237 }
238
239
240
241
242
243
244
245
246
247
248
249 public InputStream getInputStream(ClassLoader classLoader) throws RuleSetNotFoundException {
250 if (externalRuleSetReferenceId == null) {
251 InputStream in = StringUtil.isEmpty(ruleSetFileName) ? null : ResourceLoader.loadResourceAsStream(
252 ruleSetFileName, classLoader);
253 if (in == null) {
254 throw new RuleSetNotFoundException(
255 "Can't find resource "
256 + ruleSetFileName
257 + ". Make sure the resource is a valid file or URL or is on the CLASSPATH. Here's the current classpath: "
258 + System.getProperty("java.class.path"));
259 }
260 return in;
261 } else {
262 return externalRuleSetReferenceId.getInputStream(classLoader);
263 }
264 }
265
266
267
268
269
270
271
272 public String toString() {
273 if (ruleSetFileName != null) {
274 if (allRules) {
275 return ruleSetFileName;
276 } else {
277 return ruleSetFileName + "/" + ruleName;
278 }
279
280 } else {
281 if (allRules) {
282 return "anonymous all Rule";
283 } else {
284 return ruleName;
285 }
286 }
287 }
288 }