1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.archetype.ui.generation;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.commons.lang3.StringUtils;
28 import org.apache.maven.archetype.catalog.Archetype;
29
30
31
32
33
34 public class ArchetypeSelectorUtils {
35 private ArchetypeSelectorUtils() {
36
37 }
38
39 private static String extractGroupIdFromFilter(String filter) {
40 return StringUtils.contains(filter, ':') ? StringUtils.substringBefore(filter, ":") : null;
41 }
42
43 private static String extractArtifactIdFromFilter(String filter) {
44
45 return StringUtils.contains(filter, ':') ? StringUtils.substringAfter(filter, ":") : filter;
46 }
47
48
49
50
51
52
53
54
55 public static Map<String, List<Archetype>> getFilteredArchetypesByCatalog(
56 Map<String, List<Archetype>> archetypesPerCatalog, String filter) {
57 if (archetypesPerCatalog == null || archetypesPerCatalog.isEmpty()) {
58 return Collections.emptyMap();
59 }
60 Map<String, List<Archetype>> filtered = new LinkedHashMap<>(archetypesPerCatalog.size());
61
62 for (Map.Entry<String, List<Archetype>> entry : archetypesPerCatalog.entrySet()) {
63 List<Archetype> archetypes = new ArrayList<>();
64
65 for (Archetype archetype : entry.getValue()) {
66 String groupId = ArchetypeSelectorUtils.extractGroupIdFromFilter(filter);
67 String artifactId = ArchetypeSelectorUtils.extractArtifactIdFromFilter(filter);
68
69 if (((groupId == null) || StringUtils.contains(archetype.getGroupId(), groupId))
70 && StringUtils.contains(archetype.getArtifactId(), artifactId)) {
71 archetypes.add(archetype);
72 }
73 }
74
75 if (!archetypes.isEmpty()) {
76 filtered.put(entry.getKey(), archetypes);
77 }
78 }
79
80 return filtered;
81 }
82 }