1 |
| package net.sourceforge.pmd.properties; |
2 |
| |
3 |
| import net.sourceforge.pmd.PropertyDescriptor; |
4 |
| import net.sourceforge.pmd.Rule; |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| public abstract class AbstractPMDProperty implements PropertyDescriptor { |
13 |
| |
14 |
| private String name; |
15 |
| private String description; |
16 |
| private Object defaultValue; |
17 |
| private boolean isRequired = false; |
18 |
| private int maxValueCount = 1; |
19 |
| private float uiOrder; |
20 |
| |
21 |
| protected char multiValueDelimiter = '|'; |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
604
| protected AbstractPMDProperty(String theName, String theDescription, Object theDefault, float theUIOrder) {
|
31 |
604
| name = theName;
|
32 |
604
| description = theDescription;
|
33 |
604
| defaultValue = theDefault;
|
34 |
604
| uiOrder = theUIOrder;
|
35 |
| } |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
180
| protected void multiValueDelimiter(char aDelimiter) {
|
42 |
180
| multiValueDelimiter = aDelimiter;
|
43 |
| } |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
0
| public char multiValueDelimiter() {
|
51 |
0
| return multiValueDelimiter;
|
52 |
| } |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
1096
| public String name() {
|
60 |
1096
| return name;
|
61 |
| } |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
0
| public String description() {
|
69 |
0
| return description;
|
70 |
| } |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
67
| public Object defaultValue() {
|
78 |
67
| return defaultValue;
|
79 |
| } |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
1059
| public int maxValueCount() {
|
87 |
1059
| return maxValueCount;
|
88 |
| } |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
299
| protected void maxValueCount(int theCount) {
|
96 |
299
| maxValueCount = theCount;
|
97 |
| } |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
0
| public boolean isRequired() {
|
105 |
0
| return isRequired;
|
106 |
| } |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
0
| public float uiOrder() {
|
114 |
0
| return uiOrder;
|
115 |
| } |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
98
| protected String asString(Object value) {
|
125 |
98
| return value == null ? "" : value.toString();
|
126 |
| } |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
43
| public String asDelimitedString(Object values) {
|
136 |
| |
137 |
0
| if (values == null) return "";
|
138 |
| |
139 |
43
| if (values instanceof Object[]) {
|
140 |
10
| Object[] valueSet = (Object[])values;
|
141 |
0
| if (valueSet.length == 0) return "";
|
142 |
0
| if (valueSet.length == 1) return asString(valueSet[0]);
|
143 |
| |
144 |
10
| StringBuffer sb = new StringBuffer();
|
145 |
10
| sb.append(asString(valueSet[0]));
|
146 |
10
| for (int i=1; i<valueSet.length; i++) {
|
147 |
66
| sb.append(multiValueDelimiter);
|
148 |
66
| sb.append(asString(valueSet[i]));
|
149 |
| } |
150 |
10
| return sb.toString();
|
151 |
| } |
152 |
| |
153 |
33
| return asString(values);
|
154 |
| } |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
0
| public int compareTo(Object otherProperty) {
|
163 |
0
| float otherOrder = ((PropertyDescriptor)otherProperty).uiOrder();
|
164 |
0
| return (int) (otherOrder - uiOrder);
|
165 |
| } |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
| |
172 |
| |
173 |
14
| public String errorFor(Object value) {
|
174 |
| |
175 |
14
| String typeError = typeErrorFor(value);
|
176 |
0
| if (typeError != null) return typeError;
|
177 |
14
| return valueErrorFor(value);
|
178 |
| } |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
10
| protected String valueErrorFor(Object value) {
|
186 |
| |
187 |
10
| return null;
|
188 |
| } |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
7
| protected boolean isArray(Object value) {
|
196 |
7
| return value != null && value.getClass().getComponentType() != null;
|
197 |
| } |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
14
| protected String typeErrorFor(Object value) {
|
205 |
| |
206 |
0
| if (value == null && !isRequired) return null;
|
207 |
| |
208 |
14
| if (maxValueCount > 1) {
|
209 |
7
| if (!isArray(value)) {
|
210 |
0
| return "Value is not an array of type: " + type();
|
211 |
| } |
212 |
| |
213 |
7
| Class arrayType = value.getClass().getComponentType();
|
214 |
7
| if (arrayType == null || !arrayType.isAssignableFrom(type())) {
|
215 |
0
| return "Value is not an array of type: " + type();
|
216 |
| } |
217 |
7
| return null;
|
218 |
| } |
219 |
| |
220 |
7
| if (!type().isAssignableFrom(value.getClass())) {
|
221 |
0
| return "" + value + " is not an instance of " + type();
|
222 |
| } |
223 |
| |
224 |
7
| return null;
|
225 |
| } |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
0
| public String propertyErrorFor(Rule rule) {
|
234 |
0
| String strValue = rule.getStringProperty(name());
|
235 |
0
| if (strValue == null && !isRequired()) return null;
|
236 |
0
| Object realValue = valueFrom(strValue);
|
237 |
0
| return errorFor(realValue);
|
238 |
| } |
239 |
| |
240 |
| |
241 |
| |
242 |
| |
243 |
| |
244 |
| |
245 |
0
| public Object[][] choices() {
|
246 |
0
| return null;
|
247 |
| } |
248 |
| |
249 |
| |
250 |
| |
251 |
| |
252 |
| |
253 |
| |
254 |
0
| public int preferredRowCount() {
|
255 |
0
| return 1;
|
256 |
| } |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
0
| public static final boolean areEqual(Object value, Object otherValue) {
|
265 |
0
| if (value == otherValue) return true;
|
266 |
0
| if (value == null) return false;
|
267 |
0
| if (otherValue == null) return false;
|
268 |
| |
269 |
0
| return value.equals(otherValue);
|
270 |
| } |
271 |
| } |