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