1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.properties.converter.collection; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.io.StreamTokenizer; |
24 |
|
import java.io.StringReader; |
25 |
|
import java.lang.reflect.Array; |
26 |
|
import java.lang.reflect.ParameterizedType; |
27 |
|
import java.lang.reflect.Type; |
28 |
|
import java.util.ArrayList; |
29 |
|
import java.util.Collection; |
30 |
|
|
31 |
|
import javax.inject.Inject; |
32 |
|
|
33 |
|
import org.apache.commons.lang3.StringUtils; |
34 |
|
import org.xwiki.component.util.ReflectionUtils; |
35 |
|
import org.xwiki.properties.ConverterManager; |
36 |
|
import org.xwiki.properties.converter.AbstractConverter; |
37 |
|
import org.xwiki.properties.converter.ConversionException; |
38 |
|
|
39 |
|
|
40 |
|
@link |
41 |
|
|
42 |
|
@param |
43 |
|
@version |
44 |
|
@since |
45 |
|
|
|
|
| 84.1% |
Uncovered Elements: 17 (107) |
Complexity: 30 |
Complexity Density: 0.44 |
|
46 |
|
public abstract class AbstractCollectionConverter<T extends Collection> extends AbstractConverter<T> |
47 |
|
{ |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
protected static final char QUOTECHAR = '"'; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
protected static final String QUOTESTRING = "\""; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
@Inject |
62 |
|
private ConverterManager converterManager; |
63 |
|
|
64 |
|
|
65 |
|
@see |
66 |
|
|
67 |
|
private String delimiters = ", "; |
68 |
|
|
69 |
|
|
70 |
|
@return |
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
72 |
6 |
public ConverterManager getConverterManager()... |
73 |
|
{ |
74 |
6 |
return this.converterManager; |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
@param |
81 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
82 |
0 |
public void setDelimiters(String delimiter)... |
83 |
|
{ |
84 |
0 |
this.delimiters = delimiter; |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
|
@return |
89 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
90 |
28 |
public String getDelimiters()... |
91 |
|
{ |
92 |
28 |
return this.delimiters; |
93 |
|
} |
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
95 |
26 |
@Override... |
96 |
|
protected <G extends T> G convertToType(Type targetType, Object value) |
97 |
|
{ |
98 |
26 |
Type elementType = null; |
99 |
26 |
if (targetType instanceof ParameterizedType) { |
100 |
14 |
elementType = ((ParameterizedType) targetType).getActualTypeArguments()[0]; |
101 |
|
} |
102 |
|
|
103 |
26 |
if (value instanceof Iterable) { |
104 |
1 |
return fromIterable(targetType, (Iterable) value, elementType); |
105 |
19 |
} else if (value.getClass().isArray()) { |
106 |
1 |
return fromArray(targetType, value, elementType); |
107 |
|
} else { |
108 |
18 |
return parseElements(targetType, value.toString(), elementType); |
109 |
|
} |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
@param |
114 |
|
@param |
115 |
|
@param |
116 |
|
@param |
117 |
|
@return |
118 |
|
@throws |
119 |
|
@throws |
120 |
|
@since |
121 |
|
@since |
122 |
|
@since |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
124 |
1 |
protected <G extends T> G fromIterable(Type targetType, Iterable<?> values, Type elementType)... |
125 |
|
{ |
126 |
1 |
T collection = newCollection(targetType); |
127 |
|
|
128 |
1 |
for (Object value : values) { |
129 |
3 |
collection.add(this.converterManager.convert(elementType, value)); |
130 |
|
} |
131 |
|
|
132 |
1 |
return (G) collection; |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
@param |
137 |
|
@param |
138 |
|
@param |
139 |
|
@param |
140 |
|
@return |
141 |
|
@throws |
142 |
|
@throws |
143 |
|
@since |
144 |
|
@since |
145 |
|
@since |
146 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
147 |
1 |
protected <G extends T> G fromArray(Type targetType, Object values, Type elementType)... |
148 |
|
{ |
149 |
1 |
T collection = newCollection(targetType); |
150 |
|
|
151 |
4 |
for (int i = 0; i < Array.getLength(values); ++i) { |
152 |
3 |
Object value = Array.get(values, i); |
153 |
|
|
154 |
3 |
collection.add(this.converterManager.convert(elementType, value)); |
155 |
|
} |
156 |
|
|
157 |
1 |
return (G) collection; |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
@param |
174 |
|
@param |
175 |
|
@param |
176 |
|
@param |
177 |
|
@return |
178 |
|
@throws |
179 |
|
@throws |
180 |
|
|
|
|
| 84% |
Uncovered Elements: 4 (25) |
Complexity: 8 |
Complexity Density: 0.47 |
|
181 |
18 |
protected <G extends T> G parseElements(Type targetType, String value, Type elementType)... |
182 |
|
{ |
183 |
18 |
String cleanedValue = cleanValue(value); |
184 |
|
|
185 |
18 |
try { |
186 |
|
|
187 |
18 |
StreamTokenizer st = createStreamTokenizer(cleanedValue); |
188 |
|
|
189 |
|
|
190 |
18 |
T collection = newCollection(targetType); |
191 |
18 |
while (true) { |
192 |
69 |
int ttype = st.nextToken(); |
193 |
69 |
if (ttype == StreamTokenizer.TT_WORD || ttype > 0) { |
194 |
51 |
if (st.sval != null) { |
195 |
51 |
Object objValue = st.sval; |
196 |
51 |
if (elementType != null && elementType != String.class) { |
197 |
33 |
objValue = this.converterManager.convert(elementType, objValue); |
198 |
|
} |
199 |
|
|
200 |
51 |
collection.add(objValue); |
201 |
|
} |
202 |
18 |
} else if (ttype == StreamTokenizer.TT_EOF) { |
203 |
18 |
break; |
204 |
|
} else { |
205 |
0 |
throw new ConversionException("Encountered token of type " + ttype + " parsing elements."); |
206 |
|
} |
207 |
|
} |
208 |
|
|
209 |
|
|
210 |
18 |
return (G) collection; |
211 |
|
} catch (IOException e) { |
212 |
0 |
throw new ConversionException("Error converting from String: " + e.getMessage(), e); |
213 |
|
} |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
|
@param |
218 |
|
@param |
219 |
|
@return@link |
220 |
|
|
|
|
| 50% |
Uncovered Elements: 4 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
221 |
13 |
protected <G extends T> T newCollection(Type targetType)... |
222 |
|
{ |
223 |
13 |
Class<G> targetClass = ReflectionUtils.getTypeClass(targetType); |
224 |
13 |
if (targetClass.isAssignableFrom(ArrayList.class)) { |
225 |
13 |
return (G) new ArrayList(); |
226 |
|
} else { |
227 |
0 |
try { |
228 |
0 |
return targetClass.newInstance(); |
229 |
|
} catch (Exception e) { |
230 |
0 |
throw new ConversionException("Failed to create new instance of target type [" + targetType + "]", e); |
231 |
|
} |
232 |
|
} |
233 |
|
} |
234 |
|
|
235 |
|
|
236 |
|
@param |
237 |
|
@return |
238 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
239 |
18 |
private String cleanValue(String value)... |
240 |
|
{ |
241 |
|
|
242 |
18 |
String cleanedValue = value.trim(); |
243 |
18 |
if (cleanedValue.startsWith("{") && cleanedValue.endsWith("}")) { |
244 |
0 |
cleanedValue = cleanedValue.substring(1, cleanedValue.length() - 1); |
245 |
|
} |
246 |
|
|
247 |
18 |
return cleanedValue; |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
|
@link |
252 |
|
|
253 |
|
@param |
254 |
|
@return@link |
255 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
256 |
18 |
protected StreamTokenizer createStreamTokenizer(String value)... |
257 |
|
{ |
258 |
|
|
259 |
18 |
StreamTokenizer st = new StreamTokenizer(new StringReader(value)); |
260 |
|
|
261 |
|
|
262 |
18 |
st.ordinaryChars(0, 255); |
263 |
18 |
st.wordChars(0, 255); |
264 |
|
|
265 |
|
|
266 |
18 |
st.quoteChar('"'); |
267 |
18 |
st.quoteChar('\''); |
268 |
|
|
269 |
|
|
270 |
18 |
for (char c : getDelimiters().toCharArray()) { |
271 |
36 |
st.whitespaceChars(c, c); |
272 |
|
} |
273 |
|
|
274 |
18 |
return st; |
275 |
|
} |
276 |
|
|
|
|
| 76.2% |
Uncovered Elements: 5 (21) |
Complexity: 5 |
Complexity Density: 0.38 |
|
277 |
2 |
@Override... |
278 |
|
protected String convertToString(T value) |
279 |
|
{ |
280 |
2 |
StringBuilder sb = new StringBuilder(); |
281 |
|
|
282 |
2 |
for (Object element : value) { |
283 |
6 |
if (sb.length() > 0) { |
284 |
4 |
sb.append(getDelimiters()); |
285 |
|
} |
286 |
|
|
287 |
6 |
String elementString = getConverterManager().convert(String.class, element); |
288 |
|
|
289 |
6 |
if (elementString != null) { |
290 |
6 |
boolean containsDelimiter = StringUtils.contains(elementString, getDelimiters()); |
291 |
|
|
292 |
6 |
if (containsDelimiter) { |
293 |
0 |
sb.append(QUOTESTRING); |
294 |
|
} |
295 |
6 |
sb.append(elementString.replace("\\", "\\\\").replace(QUOTESTRING, "\\\"").replace("'", "\\'")); |
296 |
6 |
if (containsDelimiter) { |
297 |
0 |
sb.append(QUOTESTRING); |
298 |
|
} |
299 |
|
} |
300 |
|
} |
301 |
|
|
302 |
2 |
return sb.toString(); |
303 |
|
} |
304 |
|
} |