1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.extension.repository.internal; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Collection; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.LinkedHashSet; |
26 |
|
import java.util.List; |
27 |
|
import java.util.regex.Pattern; |
28 |
|
|
29 |
|
import org.apache.commons.lang3.StringUtils; |
30 |
|
import org.xwiki.extension.Extension; |
31 |
|
import org.xwiki.extension.internal.converter.ExtensionIdConverter; |
32 |
|
import org.xwiki.extension.repository.result.AggregatedIterableResult; |
33 |
|
import org.xwiki.extension.repository.result.CollectionIterableResult; |
34 |
|
import org.xwiki.extension.repository.result.IterableResult; |
35 |
|
import org.xwiki.extension.repository.search.ExtensionQuery; |
36 |
|
import org.xwiki.extension.repository.search.ExtensionQuery.COMPARISON; |
37 |
|
import org.xwiki.extension.repository.search.ExtensionQuery.Filter; |
38 |
|
import org.xwiki.extension.repository.search.ExtensionQuery.SortClause; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
@version |
44 |
|
@since |
45 |
|
|
|
|
| 77.5% |
Uncovered Elements: 34 (151) |
Complexity: 44 |
Complexity Density: 0.53 |
|
46 |
|
public final class RepositoryUtils |
47 |
|
{ |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
public static final String SEARCH_PATTERN_SUFFIXNPREFIX = ".*"; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
56 |
0 |
private RepositoryUtils()... |
57 |
|
{ |
58 |
|
} |
59 |
|
|
60 |
|
|
61 |
|
@param |
62 |
|
@param |
63 |
|
@param |
64 |
|
@param |
65 |
|
@return |
66 |
|
@param@link |
67 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
68 |
0 |
public static <E extends Extension> CollectionIterableResult<E> searchInCollection(String pattern, int offset,... |
69 |
|
int nb, Collection<E> extensions) |
70 |
|
{ |
71 |
0 |
return searchInCollection(pattern, offset, nb, extensions, false); |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
@param |
76 |
|
@param |
77 |
|
@param |
78 |
|
@param |
79 |
|
@param |
80 |
|
@return |
81 |
|
@since |
82 |
|
@param@link |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
84 |
6 |
public static <E extends Extension> CollectionIterableResult<E> searchInCollection(String pattern, int offset,... |
85 |
|
int nb, Collection<E> extensions, boolean forceUnique) |
86 |
|
{ |
87 |
6 |
ExtensionQuery query = new ExtensionQuery(pattern); |
88 |
|
|
89 |
6 |
query.setOffset(offset); |
90 |
6 |
query.setLimit(nb); |
91 |
|
|
92 |
6 |
return searchInCollection(query, extensions, forceUnique); |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
@param |
97 |
|
@param |
98 |
|
@param |
99 |
|
@return |
100 |
|
@since |
101 |
|
@param@link |
102 |
|
|
|
|
| 60% |
Uncovered Elements: 4 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
103 |
6 |
public static <E extends Extension> CollectionIterableResult<E> searchInCollection(ExtensionQuery query,... |
104 |
|
Collection<E> extensions, boolean forceUnique) |
105 |
|
{ |
106 |
6 |
List<E> result; |
107 |
|
|
108 |
|
|
109 |
6 |
if (StringUtils.isEmpty(query.getQuery())) { |
110 |
0 |
result = extensions instanceof List ? (List<E>) extensions : new ArrayList<E>(extensions); |
111 |
|
} else { |
112 |
6 |
result = filter(query.getQuery(), query.getFilters(), extensions, forceUnique); |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
6 |
sort(result, query.getSortClauses()); |
117 |
|
|
118 |
|
|
119 |
6 |
return RepositoryUtils.getIterableResult(query.getOffset(), query.getLimit(), result); |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
|
@param |
124 |
|
@param |
125 |
|
@param |
126 |
|
@return |
127 |
|
@param@link |
128 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
129 |
61 |
public static <E> CollectionIterableResult<E> getIterableResult(int offset, int nb, Collection<E> elements)... |
130 |
|
{ |
131 |
61 |
if (nb == 0 || offset >= elements.size()) { |
132 |
8 |
return new CollectionIterableResult<E>(elements.size(), offset, Collections.<E>emptyList()); |
133 |
|
} |
134 |
|
|
135 |
53 |
List<E> list; |
136 |
53 |
if (elements instanceof List) { |
137 |
44 |
list = (List<E>) elements; |
138 |
|
} else { |
139 |
9 |
list = new ArrayList<E>(elements); |
140 |
|
} |
141 |
|
|
142 |
53 |
return getIterableResultFromList(offset, nb, list); |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
@param |
147 |
|
@param |
148 |
|
@param |
149 |
|
@return |
150 |
|
@param@link |
151 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
152 |
53 |
private static <E> CollectionIterableResult<E> getIterableResultFromList(int offset, int nb, List<E> elements)... |
153 |
|
{ |
154 |
53 |
int fromIndex = offset; |
155 |
53 |
if (fromIndex < 0) { |
156 |
4 |
fromIndex = 0; |
157 |
|
} |
158 |
|
|
159 |
53 |
int toIndex; |
160 |
53 |
if (nb > 0) { |
161 |
24 |
toIndex = nb + fromIndex; |
162 |
24 |
if (toIndex > elements.size()) { |
163 |
10 |
toIndex = elements.size(); |
164 |
|
} |
165 |
|
} else { |
166 |
29 |
toIndex = elements.size(); |
167 |
|
} |
168 |
|
|
169 |
53 |
return new CollectionIterableResult<E>(elements.size(), offset, elements.subList(fromIndex, toIndex)); |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
|
@param |
174 |
|
@param |
175 |
|
@param |
176 |
|
@param |
177 |
|
@return |
178 |
|
@since |
179 |
|
@param@link |
180 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
181 |
6 |
private static <E extends Extension> List<E> filter(String pattern, Collection<Filter> filters,... |
182 |
|
Collection<E> extensions, boolean forceUnique) |
183 |
|
{ |
184 |
6 |
List<E> result = new ArrayList<E>(extensions.size()); |
185 |
|
|
186 |
6 |
Pattern patternMatcher = |
187 |
|
Pattern.compile(SEARCH_PATTERN_SUFFIXNPREFIX + pattern.toLowerCase() + SEARCH_PATTERN_SUFFIXNPREFIX); |
188 |
|
|
189 |
6 |
for (E extension : extensions) { |
190 |
2446 |
if (matches(patternMatcher, filters, extension)) { |
191 |
61 |
result.add(extension); |
192 |
|
} |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
6 |
if (forceUnique && result.size() > 1) { |
197 |
5 |
result = new ArrayList<>(new LinkedHashSet<>(result)); |
198 |
|
} |
199 |
|
|
200 |
6 |
return result; |
201 |
|
} |
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
@param |
207 |
|
@param |
208 |
|
@param |
209 |
|
@return |
210 |
|
@since |
211 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
212 |
2944 |
public static boolean matches(Pattern patternMatcher, Collection<Filter> filters, Extension extension)... |
213 |
|
{ |
214 |
2944 |
if (matches(patternMatcher, extension.getId().getId(), extension.getDescription(), extension.getSummary(), |
215 |
|
extension.getName(), ExtensionIdConverter.toStringList(extension.getExtensionFeatures()))) { |
216 |
508 |
for (Filter filter : filters) { |
217 |
72 |
if (!matches(filter, extension)) { |
218 |
65 |
return false; |
219 |
|
} |
220 |
|
} |
221 |
|
|
222 |
443 |
return true; |
223 |
|
} |
224 |
|
|
225 |
2436 |
return false; |
226 |
|
} |
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
@param |
232 |
|
@param |
233 |
|
@return |
234 |
|
@since |
235 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
236 |
0 |
public static boolean matches(Collection<Filter> filters, Extension extension)... |
237 |
|
{ |
238 |
0 |
if (filters != null) { |
239 |
0 |
for (Filter filter : filters) { |
240 |
0 |
if (!matches(filter, extension)) { |
241 |
0 |
return false; |
242 |
|
} |
243 |
|
} |
244 |
|
} |
245 |
|
|
246 |
0 |
return true; |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
@param |
251 |
|
@param |
252 |
|
@return |
253 |
|
@since |
254 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
255 |
72 |
public static boolean matches(Filter filter, Extension extension)... |
256 |
|
{ |
257 |
72 |
return matches(filter, extension.<Object>get(filter.getField())); |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
|
@param |
262 |
|
@param |
263 |
|
@return |
264 |
|
@since |
265 |
|
|
|
|
| 88.5% |
Uncovered Elements: 3 (26) |
Complexity: 7 |
Complexity Density: 0.5 |
|
266 |
72 |
public static boolean matches(Filter filter, Object element)... |
267 |
|
{ |
268 |
72 |
if (element == null) { |
269 |
15 |
return filter.getValue() == null; |
270 |
57 |
} else if (filter.getValue() == null) { |
271 |
0 |
return false; |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
57 |
String filterValue = String.valueOf(filter.getValue()); |
276 |
57 |
String elementValue = String.valueOf(element); |
277 |
|
|
278 |
57 |
if (filter.getComparison() == COMPARISON.MATCH) { |
279 |
15 |
Pattern patternMatcher = createPatternMatcher(filterValue); |
280 |
|
|
281 |
15 |
if (matches(patternMatcher, elementValue)) { |
282 |
3 |
return true; |
283 |
|
} |
284 |
42 |
} else if (filter.getComparison() == COMPARISON.EQUAL) { |
285 |
42 |
if (filterValue.equals(elementValue)) { |
286 |
4 |
return true; |
287 |
|
} |
288 |
|
} |
289 |
|
|
290 |
50 |
return false; |
291 |
|
} |
292 |
|
|
293 |
|
|
294 |
|
|
295 |
|
|
296 |
|
@param |
297 |
|
@param |
298 |
|
@return |
299 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
300 |
2944 |
public static boolean matches(Pattern patternMatcher, Object... elements)... |
301 |
|
{ |
302 |
2944 |
if (patternMatcher == null) { |
303 |
402 |
return true; |
304 |
|
} |
305 |
|
|
306 |
2542 |
for (Object element : elements) { |
307 |
12394 |
if (matches(patternMatcher, element)) { |
308 |
106 |
return true; |
309 |
|
} |
310 |
|
} |
311 |
|
|
312 |
2436 |
return false; |
313 |
|
} |
314 |
|
|
315 |
|
|
316 |
|
@param |
317 |
|
@param |
318 |
|
@return |
319 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
320 |
12409 |
public static boolean matches(Pattern patternMatcher, Object element)... |
321 |
|
{ |
322 |
12409 |
if (element != null) { |
323 |
9567 |
if (patternMatcher.matcher(element.toString().toLowerCase()).matches()) { |
324 |
109 |
return true; |
325 |
|
} |
326 |
|
} |
327 |
|
|
328 |
12300 |
return false; |
329 |
|
} |
330 |
|
|
331 |
|
|
332 |
|
@param |
333 |
|
@return@link@link |
334 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
335 |
61 |
public static Pattern createPatternMatcher(String pattern)... |
336 |
|
{ |
337 |
61 |
return StringUtils.isEmpty(pattern) ? null : Pattern.compile(RepositoryUtils.SEARCH_PATTERN_SUFFIXNPREFIX |
338 |
|
+ Pattern.quote(pattern.toLowerCase()) + RepositoryUtils.SEARCH_PATTERN_SUFFIXNPREFIX); |
339 |
|
} |
340 |
|
|
341 |
|
|
342 |
|
|
343 |
|
|
344 |
|
@param |
345 |
|
@param |
346 |
|
@since |
347 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
348 |
52 |
public static void sort(List<? extends Extension> extensions, Collection<SortClause> sortClauses)... |
349 |
|
{ |
350 |
52 |
Collections.sort(extensions, new SortClauseComparator(sortClauses)); |
351 |
|
} |
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
|
356 |
|
@param |
357 |
|
@param |
358 |
|
@return |
359 |
|
@since |
360 |
|
@param@link |
361 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
362 |
0 |
public static <E extends Extension> IterableResult<E> appendSearchResults(IterableResult<E> previousSearchResult,... |
363 |
|
IterableResult<E> result) |
364 |
|
{ |
365 |
0 |
AggregatedIterableResult<E> newResult; |
366 |
|
|
367 |
0 |
if (previousSearchResult instanceof AggregatedIterableResult) { |
368 |
0 |
newResult = ((AggregatedIterableResult<E>) previousSearchResult); |
369 |
0 |
} else if (previousSearchResult != null) { |
370 |
0 |
newResult = new AggregatedIterableResult<E>(previousSearchResult.getOffset()); |
371 |
0 |
newResult.addSearchResult(previousSearchResult); |
372 |
|
} else { |
373 |
0 |
return result; |
374 |
|
} |
375 |
|
|
376 |
0 |
newResult.addSearchResult(result); |
377 |
|
|
378 |
0 |
return newResult; |
379 |
|
} |
380 |
|
} |