1
2
3
4 package net.sourceforge.pmd.lang.rule.properties;
5
6 import java.util.Map;
7
8 import net.sourceforge.pmd.PropertyDescriptorFactory;
9 import net.sourceforge.pmd.lang.rule.properties.factories.BasicPropertyDescriptorFactory;
10 import net.sourceforge.pmd.util.StringUtil;
11
12
13
14
15
16
17
18 public class StringMultiProperty extends AbstractProperty<String[]> {
19
20 public static final PropertyDescriptorFactory FACTORY = new BasicPropertyDescriptorFactory<StringMultiProperty>(
21 String[].class) {
22
23 public StringMultiProperty createWith(Map<String, String> valuesById) {
24 char delimiter = delimiterIn(valuesById);
25 return new StringMultiProperty(nameIn(valuesById), descriptionIn(valuesById), StringUtil.substringsOf(
26 defaultValueIn(valuesById), delimiter), 0.0f, delimiter);
27 }
28 };
29
30
31
32
33
34
35
36
37
38
39
40 public StringMultiProperty(String theName, String theDescription, String[] theDefaults, float theUIOrder,
41 char delimiter) {
42 super(theName, theDescription, theDefaults, theUIOrder, delimiter);
43
44 checkDefaults(theDefaults, delimiter);
45 }
46
47
48
49
50
51
52 private static void checkDefaults(String[] defaultValue, char delim) {
53
54 if (defaultValue == null) {
55 return;
56 }
57
58 for (int i = 0; i < defaultValue.length; i++) {
59 if (defaultValue[i].indexOf(delim) >= 0) {
60 throw new IllegalArgumentException("Cannot include the delimiter in the set of defaults");
61 }
62 }
63 }
64
65
66
67
68
69 public Class<String[]> type() {
70 return String[].class;
71 }
72
73
74
75
76
77
78 public String[] valueFrom(String valueString) {
79 return StringUtil.substringsOf(valueString, multiValueDelimiter());
80 }
81
82
83
84
85
86 private boolean containsDelimiter(String value) {
87 return value.indexOf(multiValueDelimiter()) >= 0;
88 }
89
90
91
92
93 private final String illegalCharMsg() {
94 return "Value cannot contain the '" + multiValueDelimiter() + "' character";
95 }
96
97
98
99
100
101
102 protected String valueErrorFor(Object value) {
103
104 if (value == null) {
105 return "missing value";
106 }
107
108 String testValue = (String) value;
109 if (containsDelimiter(testValue)) {
110 return illegalCharMsg();
111 }
112
113
114
115 return null;
116 }
117
118
119
120
121
122 @Override
123 public boolean isMultiValue() {
124 return true;
125 }
126 }