1 |
| package net.sourceforge.pmd.properties; |
2 |
| |
3 |
| import net.sourceforge.pmd.util.StringUtil; |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| public class StringProperty extends AbstractPMDProperty { |
13 |
| |
14 |
| private int preferredRowCount; |
15 |
| |
16 |
| public static final char defaultDelimiter = '|'; |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
95
| public StringProperty(String theName, String theDescription, String theDefaultValue, float theUIOrder) {
|
26 |
95
| this(theName, theDescription, theDefaultValue, theUIOrder, defaultDelimiter);
|
27 |
| |
28 |
95
| maxValueCount(1);
|
29 |
| } |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
75
| public StringProperty(String theName, String theDescription, String[] theValues, float theUIOrder, char aMultiValueDelimiter) {
|
40 |
75
| super(theName, theDescription, theValues, theUIOrder);
|
41 |
| |
42 |
75
| maxValueCount(Integer.MAX_VALUE);
|
43 |
75
| multiValueDelimiter(aMultiValueDelimiter);
|
44 |
| } |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
102
| protected StringProperty(String theName, String theDescription, Object theDefaultValue, float theUIOrder, char aMultiValueDelimiter) {
|
55 |
102
| super(theName, theDescription, theDefaultValue, theUIOrder);
|
56 |
| |
57 |
102
| maxValueCount(Integer.MAX_VALUE);
|
58 |
102
| multiValueDelimiter(aMultiValueDelimiter);
|
59 |
| } |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
3
| public Class type() {
|
67 |
3
| return String.class;
|
68 |
| } |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
21
| public Object valueFrom(String valueString) {
|
77 |
| |
78 |
16
| if (maxValueCount() == 1) return valueString;
|
79 |
| |
80 |
5
| return StringUtil.substringsOf(valueString, multiValueDelimiter);
|
81 |
| } |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
11
| private boolean containsDelimiter(String value) {
|
89 |
11
| return value.indexOf(multiValueDelimiter) >= 0;
|
90 |
| } |
91 |
| |
92 |
0
| private final String illegalCharMsg() {
|
93 |
0
| return "Value cannot contain the \"" + multiValueDelimiter + "\" character";
|
94 |
| } |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
2
| protected String valueErrorFor(Object value) {
|
102 |
| |
103 |
2
| if (maxValueCount() == 1) {
|
104 |
1
| String testValue = (String)value;
|
105 |
1
| if (!containsDelimiter(testValue)) return null;
|
106 |
0
| return illegalCharMsg();
|
107 |
| } |
108 |
| |
109 |
1
| String[] values = (String[])value;
|
110 |
1
| for (int i=0; i<values.length; i++) {
|
111 |
10
| if (!containsDelimiter(values[i])) continue;
|
112 |
0
| return illegalCharMsg();
|
113 |
| } |
114 |
| |
115 |
1
| return null;
|
116 |
| } |
117 |
| |
118 |
0
| public int preferredRowCount() {
|
119 |
0
| return preferredRowCount;
|
120 |
| } |
121 |
| } |